#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mt make_tuple
#define ll long long
const int N=100050;
int X1[N],X2[N],Y1[N],Y2[N];
vector<tuple<int,int,int>> events;
int ls[N],rs[N],pri[N],val[N],idx[N],tsz,root,sum[N],tag[N],deg[N],sz[N],L[N],R[N];
bool ban[N];
int Make(int v, int i)
{
tsz++;
pri[tsz]=rand()+(rand()<<15);
val[tsz]=v;
sz[tsz]=1;
deg[tsz]=0;
L[tsz]=R[tsz]=v;
idx[tsz]=i;
sum[tsz]=tag[tsz]=ls[tsz]=rs[tsz]=0;
return tsz;
}
void upd(int x, int f){ if(x) sum[x]=sz[x],deg[x]+=f,tag[x]+=f;}
void push(int x){ if(tag[x]) upd(ls[x],tag[x]),upd(rs[x],tag[x]),tag[x]=0;}
void pull(int x)
{
if(!x) return;
push(x);
sz[x]=sz[ls[x]]+1+sz[rs[x]];
sum[x]=sum[ls[x]]+(deg[x]!=0)+sum[rs[x]];
L[x]=ls[x]?L[ls[x]]:val[x];
R[x]=rs[x]?R[rs[x]]:val[x];
}
void Split(int x, int &l, int &r, int v)
{
if(!x) l=r=0;
else
{
push(x);
if(val[x]<v) l=x,Split(rs[x],rs[l],r,v);
else r=x,Split(ls[x],l,ls[r],v);
pull(x);
}
}
void Split3(int x, int &a, int &b, int &c, int l, int r)
{
Split(x,a,b,l);
Split(b,b,c,r+1);
}
void Merge(int &x, int l, int r)
{
if(!l || !r) x=l^r;
else
{
if(pri[l]>pri[r]) push(l),x=l,Merge(rs[x],rs[l],r);
else push(r),x=r,Merge(ls[x],l,ls[r]);
pull(x);
}
}
void Merge3(int &x, int a, int b, int c)
{
Merge(x,a,b);
Merge(x,x,c);
}
void Ins(int &x, int v, int i)
{
int a,b,c;
Split(x,a,c,v);
b=Make(v,i);
Merge3(x,a,b,c);
}
void Del(int &x, int v)
{
int a,b,c;
Split3(x,a,b,c,v,v);
Merge(x,a,c);
}
void Mark(int &x, int l, int r)
{
int a,b,c;
Split3(x,a,b,c,l,r);
upd(b,1);
Merge3(x,a,b,c);
}
int Get(int &x, int l, int r)
{
int a,b,c;
Split3(x,a,b,c,l,r);
int ans=sum[b];
Merge3(x,a,b,c);
return ans;
}
int SZ(int &x, int l, int r)
{
int a,b,c;
Split3(x,a,b,c,l,r);
int ans=sz[b];
Merge3(x,a,b,c);
return ans;
}
int Pre(int x, int v)
{
if(!x) return 0;
push(x);
if(val[x]<=v)
{
if(rs[x] && L[rs[x]]<=v) return Pre(rs[x],v);
else return x;
}
else return Pre(ls[x],v);
}
void init(){ tsz=root=0;}
int D[N],q;
void DegSweep()
{
init();
for(int i=1;i<=q;i++)
{
if(X1[i]==X2[i]) events.pb(mt(X1[i],2,i));
else events.pb(mt(X1[i],1,i)),events.pb(mt(X2[i],3,i));
}
sort(events.begin(),events.end());
for(auto e:events)
{
int x,t,i;tie(x,t,i)=e;
if(t==1) Ins(root,Y1[i],i);
if(t==2)
{
int a,b,c;
Split3(root,a,b,c,Y1[i],Y2[i]);
D[i]=sz[b];
Merge3(root,a,b,c);
Mark(root,Y1[i],Y2[i]);
}
if(t==3)
{
int a,b,c;
Split3(root,a,b,c,Y1[i],Y1[i]);
D[i]=deg[b];
Merge(root,a,c);
}
}
}
queue<int> Q;
const int H=2*N;
const int M=2*H;
struct Compress
{
vector<int> all;
Compress(){}
void Add(int x){ all.pb(x);}
int Find(int x){ return lower_bound(all.begin(),all.end(),x)-all.begin()+1;}
void Build(){ sort(all.begin(),all.end());all.erase(unique(all.begin(),all.end()),all.end());}
} xs,ys;
struct SegmentTree
{
set<pair<int,int>> node[M];
SegmentTree(){}
void Add(int x, int l, int r, int i)
{
for(l+=H,r+=H;l<=r;l>>=1,r>>=1)
{
if(l%2==1) node[l++].insert({x,i});
if(r%2==0) node[r--].insert({x,i});
}
}
void Take(int i, int l, int r, bool one)
{
for(i+=H;i;i>>=1)
{
for(auto it=node[i].lower_bound({l,-1e9});it!=node[i].end() && it->first<=r;)
{
int j=it->second;
if(ban[j]){ node[i].erase(it++);continue;}
if(one)
{
D[j]--;
if(D[j]<=1) Q.push(j),ban[j]=1,node[i].erase(it++);
else it++;
}
else
{
Q.push(j);
ban[j]=1;
node[i].erase(it++);
}
}
}
}
} ST[2];
void Build()
{
DegSweep();
for(int i=1;i<=q;i++) xs.Add(X1[i]),xs.Add(X2[i]),ys.Add(Y1[i]),ys.Add(Y2[i]);
xs.Build();ys.Build();
for(int i=1;i<=q;i++)
{
if(D[i]<=1) Q.push(i),ban[i]=1;
else
{
if(X1[i]==X2[i]) ST[0].Add(xs.Find(X1[i]),ys.Find(Y1[i]),ys.Find(Y2[i]),i);
else ST[1].Add(ys.Find(Y1[i]),xs.Find(X1[i]),xs.Find(X2[i]),i);
}
}
while(Q.size())
{
int i=Q.front();
Q.pop();
if(X1[i]==X2[i]) ST[1].Take(xs.Find(X1[i]),ys.Find(Y1[i]),ys.Find(Y2[i]),1);
else ST[0].Take(ys.Find(Y1[i]),xs.Find(X1[i]),xs.Find(X2[i]),1);
}
}
int CntCmp()
{
int ans=0;
for(int i=1;i<=q;i++) if(!ban[i])
{
ans++;
Q.push(i);
ban[i]=1;
while(Q.size())
{
int j=Q.front();
Q.pop();
if(X1[j]==X2[j]) ST[1].Take(xs.Find(X1[j]),ys.Find(Y1[j]),ys.Find(Y2[j]),0);
else ST[0].Take(ys.Find(Y1[j]),xs.Find(X1[j]),xs.Find(X2[j]),0);
}
}
return ans;
}
int main()
{
srand(time(0));
int n,m;
scanf("%i %i %i",&n,&m,&q);
for(int i=1;i<=q;i++)
{
scanf("%i %i %i %i",&X1[i],&Y1[i],&X2[i],&Y2[i]);
}
q++;X1[q]=X2[q]=0;Y1[q]=0;Y2[q]=m;
q++;X1[q]=X2[q]=n;Y1[q]=0;Y2[q]=m;
q++;Y1[q]=Y2[q]=0;X1[q]=0;X2[q]=n;
q++;Y1[q]=Y2[q]=m;X1[q]=0;X2[q]=n;
Build();
events.clear();
for(int i=1;i<=q;i++) if(!ban[i])
{
if(X1[i]==X2[i]) events.pb(mt(X1[i],2,i));
else events.pb(mt(X1[i],1,i)),events.pb(mt(X2[i],3,i));
}
sort(events.begin(),events.end());
ll a=0,b=0;
init();
for(auto e:events)
{
int x,t,i;tie(x,t,i)=e;
if(t==1) Ins(root,Y1[i],i);
if(t==2)
{
int node=Pre(root,Y2[i]);
assert(node!=0 && val[node]>=Y1[i]);
a+=Get(root,Y1[i],val[node]-1);
assert(SZ(root,Y1[i],val[node]-1)>0);
if(deg[node]==0) b++;
Mark(root,Y1[i],Y2[i]);
}
if(t==3) Del(root,Y1[i]);
}
ll ans=a-b;
ans+=CntCmp();
printf("%lld\n",ans);
return 0;
}
Compilation message
2014_ho_t5.cpp: In function 'int main()':
2014_ho_t5.cpp:235:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&n,&m,&q);
~~~~~^~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:238:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i %i",&X1[i],&Y1[i],&X2[i],&Y2[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
38012 KB |
Output is correct |
2 |
Correct |
36 ms |
38140 KB |
Output is correct |
3 |
Correct |
36 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
36 ms |
38012 KB |
Output is correct |
6 |
Correct |
40 ms |
38008 KB |
Output is correct |
7 |
Correct |
39 ms |
38136 KB |
Output is correct |
8 |
Correct |
49 ms |
38520 KB |
Output is correct |
9 |
Correct |
42 ms |
38136 KB |
Output is correct |
10 |
Correct |
47 ms |
38524 KB |
Output is correct |
11 |
Correct |
43 ms |
38520 KB |
Output is correct |
12 |
Correct |
44 ms |
38256 KB |
Output is correct |
13 |
Correct |
43 ms |
38516 KB |
Output is correct |
14 |
Correct |
46 ms |
38264 KB |
Output is correct |
15 |
Correct |
41 ms |
38364 KB |
Output is correct |
16 |
Correct |
36 ms |
38008 KB |
Output is correct |
17 |
Correct |
37 ms |
38084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
38012 KB |
Output is correct |
2 |
Correct |
36 ms |
38140 KB |
Output is correct |
3 |
Correct |
36 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
36 ms |
38012 KB |
Output is correct |
6 |
Correct |
40 ms |
38008 KB |
Output is correct |
7 |
Correct |
39 ms |
38136 KB |
Output is correct |
8 |
Correct |
49 ms |
38520 KB |
Output is correct |
9 |
Correct |
42 ms |
38136 KB |
Output is correct |
10 |
Correct |
47 ms |
38524 KB |
Output is correct |
11 |
Correct |
43 ms |
38520 KB |
Output is correct |
12 |
Correct |
44 ms |
38256 KB |
Output is correct |
13 |
Correct |
43 ms |
38516 KB |
Output is correct |
14 |
Correct |
46 ms |
38264 KB |
Output is correct |
15 |
Correct |
41 ms |
38364 KB |
Output is correct |
16 |
Correct |
36 ms |
38008 KB |
Output is correct |
17 |
Correct |
37 ms |
38084 KB |
Output is correct |
18 |
Correct |
40 ms |
38004 KB |
Output is correct |
19 |
Correct |
42 ms |
38008 KB |
Output is correct |
20 |
Correct |
37 ms |
38008 KB |
Output is correct |
21 |
Correct |
47 ms |
38520 KB |
Output is correct |
22 |
Correct |
41 ms |
38236 KB |
Output is correct |
23 |
Correct |
43 ms |
38472 KB |
Output is correct |
24 |
Correct |
47 ms |
38600 KB |
Output is correct |
25 |
Correct |
43 ms |
38520 KB |
Output is correct |
26 |
Correct |
38 ms |
38136 KB |
Output is correct |
27 |
Correct |
44 ms |
38520 KB |
Output is correct |
28 |
Correct |
44 ms |
38648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
38264 KB |
Output is correct |
2 |
Correct |
45 ms |
38264 KB |
Output is correct |
3 |
Correct |
44 ms |
38236 KB |
Output is correct |
4 |
Correct |
48 ms |
38392 KB |
Output is correct |
5 |
Correct |
125 ms |
43320 KB |
Output is correct |
6 |
Correct |
436 ms |
57724 KB |
Output is correct |
7 |
Correct |
677 ms |
65976 KB |
Output is correct |
8 |
Correct |
851 ms |
72756 KB |
Output is correct |
9 |
Correct |
301 ms |
50876 KB |
Output is correct |
10 |
Correct |
257 ms |
50784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
38136 KB |
Output is correct |
2 |
Correct |
44 ms |
38492 KB |
Output is correct |
3 |
Correct |
162 ms |
45000 KB |
Output is correct |
4 |
Correct |
36 ms |
38008 KB |
Output is correct |
5 |
Correct |
45 ms |
38520 KB |
Output is correct |
6 |
Correct |
2800 ms |
124772 KB |
Output is correct |
7 |
Correct |
120 ms |
45496 KB |
Output is correct |
8 |
Correct |
1721 ms |
126668 KB |
Output is correct |
9 |
Correct |
1820 ms |
118156 KB |
Output is correct |
10 |
Correct |
1904 ms |
116580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
38012 KB |
Output is correct |
2 |
Correct |
36 ms |
38140 KB |
Output is correct |
3 |
Correct |
36 ms |
38008 KB |
Output is correct |
4 |
Correct |
46 ms |
38008 KB |
Output is correct |
5 |
Correct |
36 ms |
38012 KB |
Output is correct |
6 |
Correct |
40 ms |
38008 KB |
Output is correct |
7 |
Correct |
39 ms |
38136 KB |
Output is correct |
8 |
Correct |
49 ms |
38520 KB |
Output is correct |
9 |
Correct |
42 ms |
38136 KB |
Output is correct |
10 |
Correct |
47 ms |
38524 KB |
Output is correct |
11 |
Correct |
43 ms |
38520 KB |
Output is correct |
12 |
Correct |
44 ms |
38256 KB |
Output is correct |
13 |
Correct |
43 ms |
38516 KB |
Output is correct |
14 |
Correct |
46 ms |
38264 KB |
Output is correct |
15 |
Correct |
41 ms |
38364 KB |
Output is correct |
16 |
Correct |
36 ms |
38008 KB |
Output is correct |
17 |
Correct |
37 ms |
38084 KB |
Output is correct |
18 |
Correct |
40 ms |
38004 KB |
Output is correct |
19 |
Correct |
42 ms |
38008 KB |
Output is correct |
20 |
Correct |
37 ms |
38008 KB |
Output is correct |
21 |
Correct |
47 ms |
38520 KB |
Output is correct |
22 |
Correct |
41 ms |
38236 KB |
Output is correct |
23 |
Correct |
43 ms |
38472 KB |
Output is correct |
24 |
Correct |
47 ms |
38600 KB |
Output is correct |
25 |
Correct |
43 ms |
38520 KB |
Output is correct |
26 |
Correct |
38 ms |
38136 KB |
Output is correct |
27 |
Correct |
44 ms |
38520 KB |
Output is correct |
28 |
Correct |
44 ms |
38648 KB |
Output is correct |
29 |
Correct |
41 ms |
38264 KB |
Output is correct |
30 |
Correct |
45 ms |
38264 KB |
Output is correct |
31 |
Correct |
44 ms |
38236 KB |
Output is correct |
32 |
Correct |
48 ms |
38392 KB |
Output is correct |
33 |
Correct |
125 ms |
43320 KB |
Output is correct |
34 |
Correct |
436 ms |
57724 KB |
Output is correct |
35 |
Correct |
677 ms |
65976 KB |
Output is correct |
36 |
Correct |
851 ms |
72756 KB |
Output is correct |
37 |
Correct |
301 ms |
50876 KB |
Output is correct |
38 |
Correct |
257 ms |
50784 KB |
Output is correct |
39 |
Correct |
36 ms |
38136 KB |
Output is correct |
40 |
Correct |
44 ms |
38492 KB |
Output is correct |
41 |
Correct |
162 ms |
45000 KB |
Output is correct |
42 |
Correct |
36 ms |
38008 KB |
Output is correct |
43 |
Correct |
45 ms |
38520 KB |
Output is correct |
44 |
Correct |
2800 ms |
124772 KB |
Output is correct |
45 |
Correct |
120 ms |
45496 KB |
Output is correct |
46 |
Correct |
1721 ms |
126668 KB |
Output is correct |
47 |
Correct |
1820 ms |
118156 KB |
Output is correct |
48 |
Correct |
1904 ms |
116580 KB |
Output is correct |
49 |
Correct |
42 ms |
38392 KB |
Output is correct |
50 |
Correct |
46 ms |
38264 KB |
Output is correct |
51 |
Correct |
41 ms |
38392 KB |
Output is correct |
52 |
Correct |
1038 ms |
76752 KB |
Output is correct |
53 |
Correct |
233 ms |
47204 KB |
Output is correct |
54 |
Correct |
1212 ms |
79720 KB |
Output is correct |
55 |
Correct |
2899 ms |
120024 KB |
Output is correct |
56 |
Correct |
702 ms |
66016 KB |
Output is correct |
57 |
Correct |
2927 ms |
126064 KB |
Output is correct |
58 |
Correct |
2600 ms |
120008 KB |
Output is correct |
59 |
Correct |
1801 ms |
116060 KB |
Output is correct |
60 |
Correct |
2033 ms |
117176 KB |
Output is correct |
61 |
Correct |
2234 ms |
115812 KB |
Output is correct |
62 |
Correct |
2763 ms |
125916 KB |
Output is correct |
63 |
Correct |
2776 ms |
124744 KB |
Output is correct |
64 |
Correct |
300 ms |
50716 KB |
Output is correct |
65 |
Correct |
2915 ms |
125676 KB |
Output is correct |
66 |
Correct |
2833 ms |
124596 KB |
Output is correct |
67 |
Correct |
2001 ms |
96576 KB |
Output is correct |
68 |
Correct |
1890 ms |
94328 KB |
Output is correct |
69 |
Correct |
2072 ms |
102212 KB |
Output is correct |
70 |
Correct |
2074 ms |
102048 KB |
Output is correct |
71 |
Correct |
946 ms |
67472 KB |
Output is correct |
72 |
Correct |
1423 ms |
72852 KB |
Output is correct |