#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
const int BC=1000;
vector<int> adj[100000], BB;
vector<tuple<int,int,int>> qry;
vector<pair<int,int>> E, L[100000], R[100000];
map<pair<int,int>,int> idx;
int N, ans[100000], B[100000], LL[100000], color[100000], cnt[100000], RR[100000];
bool EX[100000];
void flip_edge(int c, int i)
{
BB.push_back(c);
EX[c]^=1;
}
int find_(int a)
{
while(color[a]!=a) a=color[a];
return a;
}
void union_(int a, int b)
{
a=find_(a); b=find_(b);
if(a==b) return;
if(cnt[a]<cnt[b]) swap(a,b);
cnt[color[b]=a]+=cnt[b];
BB.push_back(b);
}
void restore(int sz)
{
while(BB.size()>sz) {
cnt[color[BB.back()]]-=cnt[BB.back()];
color[BB.back()]=BB.back();
BB.pop_back();
}
}
void solve(int s, int e)
{
int m=(s+e)>>1, bsz=BB.size(), bsz2;
for(auto[v,r]: L[s]) if(r>=e) union_(E[v].first,E[v].second);
for(auto[v,l]: R[e]) if(l<=s) union_(E[v].first,E[v].second);
if(s==e) {
ans[get<2>(qry[m])]=N-BB.size();
restore(bsz);
return;
}
bsz2=BB.size();
for(int i=m;++i<e;) for(auto[v,l]: R[i]) if(l<=s) union_(E[v].first,E[v].second);
solve(s,m);
restore(bsz2);
for(int i=m;i>s;i--) for(auto[v,r]: L[i]) if(r>=e) union_(E[v].first,E[v].second);
solve(m+1,e);
restore(bsz);
}
vector<int> simulateCollapse(int N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P)
{
int C=T.size(), Q=W.size(), w, p;
::N=N;
for(int i=0;i<C;i++) {
if(X[i]>Y[i]) swap(X[i],Y[i]);
if(idx.count({X[i],Y[i]})==0) {
idx[{X[i],Y[i]}]=E.size();
E.emplace_back(X[i],Y[i]);
}
B[i]=idx[{X[i],Y[i]}];
adj[X[i]].push_back(i);
adj[Y[i]].push_back(i);
}
for(int i=0;i<Q;i++) qry.emplace_back(W[i],P[i],i);
sort(qry.begin(),qry.end(),[&](tuple<int,int,int> a, tuple<int,int,int> b) {
if(get<0>(a)/BC!=get<0>(b)/BC) return get<0>(a)/BC<get<0>(b)/BC;
return get<1>(a)<get<1>(b);
});
memset(LL,w=p=-1,sizeof(LL));
memset(RR,-1,sizeof(RR));
for(int i=0;i<Q;i++) {
auto[cw,cp,j]=qry[i];
while(w<cw) {
int c=B[++w];
auto[a,b]=E[c];
if(a<=p && p<b) continue;
flip_edge(c,i);
}
while(w>cw) {
int c=B[w--];
auto[a,b]=E[c];
if(a<=p && p<b) continue;
flip_edge(c,i);
}
while(p<cp) {
int c=++p;
for(auto t: adj[c]) if(t<=w) flip_edge(B[t],i);
}
while(p>cp) {
int c=p--;
for(auto t: adj[c]) if(t<=w) flip_edge(B[t],i);
}
for(auto v: BB) if(RR[v]!=i) {
RR[v]=i;
if(LL[v]>-1 && !EX[v]) {
L[LL[v]].emplace_back(v,i-1);
R[i-1].emplace_back(v,LL[v]);
LL[v]=-1;
}
else if(LL[v]==-1 && EX[v]) LL[v]=i;
}
BB.clear();
}
for(int i=0;i<E.size();i++) if(EX[i]) {
L[LL[i]].emplace_back(i,Q-1);
R[Q-1].emplace_back(i,LL[i]);
}
for(int i=0;i<N;i++) cnt[color[i]=i]=1;
solve(0,Q-1);
return vector<int>(ans,ans+Q);
}
Compilation message
collapse.cpp: In function 'void restore(int)':
collapse.cpp:44:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | while(BB.size()>sz) {
| ~~~~~~~~~^~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:124:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i=0;i<E.size();i++) if(EX[i]) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
8684 KB |
Output is correct |
2 |
Correct |
10 ms |
8556 KB |
Output is correct |
3 |
Correct |
12 ms |
8684 KB |
Output is correct |
4 |
Correct |
12 ms |
9068 KB |
Output is correct |
5 |
Correct |
28 ms |
9196 KB |
Output is correct |
6 |
Correct |
87 ms |
21996 KB |
Output is correct |
7 |
Correct |
8 ms |
8428 KB |
Output is correct |
8 |
Correct |
10 ms |
8684 KB |
Output is correct |
9 |
Correct |
40 ms |
10220 KB |
Output is correct |
10 |
Correct |
83 ms |
17920 KB |
Output is correct |
11 |
Correct |
108 ms |
22380 KB |
Output is correct |
12 |
Correct |
101 ms |
22380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
11744 KB |
Output is correct |
2 |
Correct |
63 ms |
14180 KB |
Output is correct |
3 |
Correct |
436 ms |
26512 KB |
Output is correct |
4 |
Correct |
129 ms |
21472 KB |
Output is correct |
5 |
Correct |
1037 ms |
97060 KB |
Output is correct |
6 |
Correct |
1269 ms |
207340 KB |
Output is correct |
7 |
Correct |
1571 ms |
223456 KB |
Output is correct |
8 |
Correct |
906 ms |
110416 KB |
Output is correct |
9 |
Correct |
47 ms |
12512 KB |
Output is correct |
10 |
Correct |
82 ms |
15460 KB |
Output is correct |
11 |
Correct |
1002 ms |
138468 KB |
Output is correct |
12 |
Correct |
1100 ms |
122972 KB |
Output is correct |
13 |
Correct |
1796 ms |
213100 KB |
Output is correct |
14 |
Correct |
1722 ms |
217748 KB |
Output is correct |
15 |
Correct |
1595 ms |
210908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
11892 KB |
Output is correct |
2 |
Correct |
71 ms |
14216 KB |
Output is correct |
3 |
Correct |
111 ms |
17888 KB |
Output is correct |
4 |
Correct |
140 ms |
22752 KB |
Output is correct |
5 |
Correct |
942 ms |
135464 KB |
Output is correct |
6 |
Correct |
1304 ms |
260544 KB |
Output is correct |
7 |
Correct |
2571 ms |
338664 KB |
Output is correct |
8 |
Correct |
4134 ms |
391900 KB |
Output is correct |
9 |
Correct |
59 ms |
12512 KB |
Output is correct |
10 |
Correct |
1555 ms |
262020 KB |
Output is correct |
11 |
Correct |
6305 ms |
386768 KB |
Output is correct |
12 |
Correct |
8048 ms |
393308 KB |
Output is correct |
13 |
Correct |
6684 ms |
388976 KB |
Output is correct |
14 |
Correct |
8303 ms |
391644 KB |
Output is correct |
15 |
Correct |
6337 ms |
376932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
8684 KB |
Output is correct |
2 |
Correct |
10 ms |
8556 KB |
Output is correct |
3 |
Correct |
12 ms |
8684 KB |
Output is correct |
4 |
Correct |
12 ms |
9068 KB |
Output is correct |
5 |
Correct |
28 ms |
9196 KB |
Output is correct |
6 |
Correct |
87 ms |
21996 KB |
Output is correct |
7 |
Correct |
8 ms |
8428 KB |
Output is correct |
8 |
Correct |
10 ms |
8684 KB |
Output is correct |
9 |
Correct |
40 ms |
10220 KB |
Output is correct |
10 |
Correct |
83 ms |
17920 KB |
Output is correct |
11 |
Correct |
108 ms |
22380 KB |
Output is correct |
12 |
Correct |
101 ms |
22380 KB |
Output is correct |
13 |
Correct |
48 ms |
11744 KB |
Output is correct |
14 |
Correct |
63 ms |
14180 KB |
Output is correct |
15 |
Correct |
436 ms |
26512 KB |
Output is correct |
16 |
Correct |
129 ms |
21472 KB |
Output is correct |
17 |
Correct |
1037 ms |
97060 KB |
Output is correct |
18 |
Correct |
1269 ms |
207340 KB |
Output is correct |
19 |
Correct |
1571 ms |
223456 KB |
Output is correct |
20 |
Correct |
906 ms |
110416 KB |
Output is correct |
21 |
Correct |
47 ms |
12512 KB |
Output is correct |
22 |
Correct |
82 ms |
15460 KB |
Output is correct |
23 |
Correct |
1002 ms |
138468 KB |
Output is correct |
24 |
Correct |
1100 ms |
122972 KB |
Output is correct |
25 |
Correct |
1796 ms |
213100 KB |
Output is correct |
26 |
Correct |
1722 ms |
217748 KB |
Output is correct |
27 |
Correct |
1595 ms |
210908 KB |
Output is correct |
28 |
Correct |
51 ms |
11892 KB |
Output is correct |
29 |
Correct |
71 ms |
14216 KB |
Output is correct |
30 |
Correct |
111 ms |
17888 KB |
Output is correct |
31 |
Correct |
140 ms |
22752 KB |
Output is correct |
32 |
Correct |
942 ms |
135464 KB |
Output is correct |
33 |
Correct |
1304 ms |
260544 KB |
Output is correct |
34 |
Correct |
2571 ms |
338664 KB |
Output is correct |
35 |
Correct |
4134 ms |
391900 KB |
Output is correct |
36 |
Correct |
59 ms |
12512 KB |
Output is correct |
37 |
Correct |
1555 ms |
262020 KB |
Output is correct |
38 |
Correct |
6305 ms |
386768 KB |
Output is correct |
39 |
Correct |
8048 ms |
393308 KB |
Output is correct |
40 |
Correct |
6684 ms |
388976 KB |
Output is correct |
41 |
Correct |
8303 ms |
391644 KB |
Output is correct |
42 |
Correct |
6337 ms |
376932 KB |
Output is correct |
43 |
Correct |
1473 ms |
88708 KB |
Output is correct |
44 |
Correct |
3042 ms |
392624 KB |
Output is correct |
45 |
Correct |
1871 ms |
128624 KB |
Output is correct |
46 |
Correct |
4196 ms |
390236 KB |
Output is correct |
47 |
Correct |
75 ms |
12416 KB |
Output is correct |
48 |
Correct |
98 ms |
16484 KB |
Output is correct |
49 |
Correct |
1230 ms |
171248 KB |
Output is correct |
50 |
Correct |
1848 ms |
252932 KB |
Output is correct |
51 |
Correct |
2578 ms |
146908 KB |
Output is correct |
52 |
Correct |
5229 ms |
289880 KB |
Output is correct |
53 |
Correct |
3920 ms |
292912 KB |
Output is correct |
54 |
Correct |
5645 ms |
320476 KB |
Output is correct |
55 |
Correct |
5033 ms |
317208 KB |
Output is correct |
56 |
Correct |
5791 ms |
352492 KB |
Output is correct |
57 |
Correct |
6148 ms |
368664 KB |
Output is correct |
58 |
Correct |
7665 ms |
370396 KB |
Output is correct |
59 |
Correct |
6568 ms |
377120 KB |
Output is correct |
60 |
Correct |
8310 ms |
393000 KB |
Output is correct |