#include "collapse.h"
#include<bits/stdc++.h>
using namespace std;
struct dsurol{
int par[100100],dep[100100],ans;
stack<pair<int,int>>stk;
dsurol(){memset(par,0,sizeof par);memset(dep,0,sizeof dep);ans=0;}
void init(int n){
ans=n;
}
int abp(int n){
return par[n]?abp(par[n]):n;
}
void merge(int a,int b){
a=abp(++a);
b=abp(++b);
if(a-b){
ans--;
if(dep[a]<dep[b])
swap(a,b);
par[b]=a;
stk.push({b,dep[a]});
dep[a]=max(dep[a],dep[b]+1);
}
}
void rerol(){
auto[b,prvd]=stk.top();
stk.pop();
dep[par[b]]=prvd;
par[b]=0;
ans++;
}
void rolbak(int n){
while(stk.size()>n)
rerol();
}
} dsu;
vector<pair<int,int>>whattoad[1<<19];
void add(int i,int l,int r,int ll,int rr,pair<int,int>K){
if(ll<=l&&r<=rr)
return whattoad[i].push_back(K);
if(ll>r||l>rr) return;
add(i*2,l,l+r>>1,ll,rr,K);
add(i*2+1,l+r+2>>1,r,ll,rr,K);
}
vector<int>ans;
void solve(int i,int l,int r){
int k=dsu.stk.size();
for(auto&[a,b]:whattoad[i])
dsu.merge(a,b);
if(l==r)
ans.push_back(dsu.ans);
else solve(i*2,l,l+r>>1),
solve(i*2+1,l+r+2>>1,r);
dsu.rolbak(k);
}
map<pair<int,int>,int> mp;
std::vector<int> simulateCollapse(
int N,
std::vector<int> T,
std::vector<int> X,
std::vector<int> Y,
std::vector<int> W,
std::vector<int> P) {
dsu.init(N);
int K=T.size();
int c=P[0];
for(int i=0;i<K;i++){
int t=T[i];
if(X[i]>Y[i])swap(X[i],Y[i]);
if(X[i]<=c&&Y[i]>c)continue;
if(!t){
mp[{X[i],Y[i]}]=i+1;
} else {
add(1,1,K,mp[{X[i],Y[i]}],i,{X[i],Y[i]});
mp.erase({X[i],Y[i]});
}
}
for(auto[k,y]:mp)
add(1,1,K,y,K,k);
solve(1,1,K);
vector<int>v;
for(auto i:W)
v.push_back(ans[i]);
return v;
}
Compilation message
collapse.cpp: In member function 'void dsurol::rolbak(int)':
collapse.cpp:34:25: warning: comparison of integer expressions of different signedness: 'std::stack<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | while(stk.size()>n)
| ~~~~~~~~~~^~
collapse.cpp: In function 'void add(int, int, int, int, int, std::pair<int, int>)':
collapse.cpp:43:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | add(i*2,l,l+r>>1,ll,rr,K);
| ~^~
collapse.cpp:44:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | add(i*2+1,l+r+2>>1,r,ll,rr,K);
| ~~~^~
collapse.cpp: In function 'void solve(int, int, int)':
collapse.cpp:53:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | else solve(i*2,l,l+r>>1),
| ~^~
collapse.cpp:54:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | solve(i*2+1,l+r+2>>1,r);
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
13656 KB |
Output is correct |
2 |
Incorrect |
4 ms |
13656 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16088 KB |
Output is correct |
2 |
Correct |
19 ms |
16080 KB |
Output is correct |
3 |
Correct |
47 ms |
22032 KB |
Output is correct |
4 |
Correct |
19 ms |
16196 KB |
Output is correct |
5 |
Correct |
55 ms |
22732 KB |
Output is correct |
6 |
Correct |
21 ms |
16084 KB |
Output is correct |
7 |
Correct |
76 ms |
29528 KB |
Output is correct |
8 |
Correct |
69 ms |
24292 KB |
Output is correct |
9 |
Correct |
21 ms |
16344 KB |
Output is correct |
10 |
Correct |
19 ms |
16344 KB |
Output is correct |
11 |
Correct |
20 ms |
16888 KB |
Output is correct |
12 |
Correct |
60 ms |
24928 KB |
Output is correct |
13 |
Correct |
90 ms |
27964 KB |
Output is correct |
14 |
Correct |
80 ms |
30060 KB |
Output is correct |
15 |
Correct |
79 ms |
29664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
15736 KB |
Output is correct |
2 |
Incorrect |
17 ms |
15572 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
13656 KB |
Output is correct |
2 |
Incorrect |
4 ms |
13656 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |