YbtOJ 864「扫描线」藏宝地图

题目链接:YbtOJ #864

小 A 有一张藏宝地图,可以视作一张 $10^6\times 10^6$ 的网格图,左上角为 $(1,1)$,右下角为 $(w,h)$。

地图中有 $k$ 个无交的矩形障碍框(障碍框在格边上)。

有 $n$ 个探险家和 $m$ 个宝藏,分别处于网格图中的某个格子内。

探险家每一步只能选择向右/向下走或返回 初始 位置,且不能越过障碍框或走出地图边界。求每个探险家能找到的宝藏的数量。

$0\le n,m,k\le2\times10^5$,所有的坐标范围在 $[1,10^6]$ 范围内。

Tutorial

一目了然,不言而明。

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define W while
#define I inline
#define RI register int
#define LL long long
#define Cn const
#define CI Cn int&
#define gc getchar
#define D isdigit(c=gc())
#define pc(c) putchar((c))
using namespace std;
namespace Debug{
Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
namespace FastIO{
Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;}
Ts I void read(Ty& x,Ar&... y){read(x),read(y...);}
Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);}
Tp I void writeln(Cn Ty& x){write(x),pc('\n');}
}using namespace FastIO;
Cn int N=2e5+10,M=1e6;
int k,n,m,Ans[N],o[M+5];
#define PA pair<int,int>
#define PPA pair<PA,int>
#define MP make_pair
#define fi first
#define se second
vector<PPA> v1[M+5],v2[M+5];
vector<PA> v4[M+5];
vector<int> v3[M+5];
#define pb push_back
class SegmentTree{
private:
int T[(M<<2)+10],tg[(M<<2)+10];
#define mid (l+r>>1)
#define PT CI x=1,CI l=1,CI r=M
#define LT x<<1,l,mid
#define RT x<<11,mid+1,r
#define PU(x) (T[x]=T[x<<1]+T[x<<11])
I void AP(CI x){T[x]=0,tg[x]=1;}
I void PD(CI x){tg[x]&&(AP(x<<1),AP(x<<11),tg[x]=0);}
public:
I int Q(CI L,CI R,PT){
if(L<=l&&r<=R) return T[x];
RI X=0;return PD(x),L<=mid&&(X+=Q(L,R,LT)),R>mid&&(X+=Q(L,R,RT)),X;
}
I void U(CI p,CI v,PT){
if(l==r) return void(T[x]+=v);
PD(x),p<=mid?U(p,v,LT):U(p,v,RT),PU(x);
}
I void A(CI L,CI R,PT){
if(L<=l&&r<=R) return AP(x);
PD(x),L<=mid&&(A(L,R,LT),0),R>mid&&(A(L,R,RT),0),PU(x);
}
}T;
set<int> S;
I int O(CI x){return *S.lower_bound(x);}
int main(){
freopen("treasure.in","r",stdin),freopen("treasure.out","w",stdout);
RI i,x1,y1,x2,y2,t;for(read(k),i=1;i<=k;i++) read(x1,y1,x2,y2),v1[y2].pb(MP(MP(x1,x2),i)),v2[y1-1].pb(MP(MP(x1,x2),i));
for(read(m),i=1;i<=m;i++) read(x1,y1),v3[y1].pb(x1);for(read(n),i=1;i<=n;i++) read(x1,y1),v4[y1].pb(MP(x1,i));
for(S.insert(M+1),i=M;i;i--){
for(auto j:v1[i]) t=T.Q(j.fi.fi,min(O(j.fi.fi),M)),o[j.se]=T.Q(j.fi.se+1,min(O(j.fi.se+1),M)),T.A(j.fi.fi,j.fi.se),j.fi.fi>1&&(T.U(j.fi.fi-1,t),S.insert(j.fi.fi-1),0),S.insert(j.fi.se);
for(auto j:v2[i]) T.A(j.fi.fi,j.fi.se),j.fi.fi>1&&(T.U(j.fi.fi-1,-o[j.se]),S.erase(j.fi.fi-1),0),S.erase(j.fi.se);
for(auto j:v3[i]) T.U(j,1);
for(auto j:v4[i]) Ans[j.se]=T.Q(j.fi,min(O(j.fi),M));
}for(i=1;i<=n;i++) writeln(Ans[i]);return 0;
}