Submission #783063

# Submission time Handle Problem Language Result Execution time Memory
783063 2023-07-14T14:43:40 Z kshitij_sodani Collapse (JOI18_collapse) C++14
100 / 100
3917 ms 33604 KB
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;


#include "collapse.h"
int cur[100001];
int vis[100001];
int par[100001];
int ss[100001];
int find(int no){
	if(par[no]==no){
		return no;
	}
	return find(par[no]);
}
vector<pair<int,int>> kk[3];
vector<pair<int,int>> pre[100001];
vector<pair<int,int>> pre2[100001];
vector<pair<int,int>> pre3[100001];
vector<int> pre4[100001];
int co=0;
void merge(int aa,int bb,int i){
	/*if(i==0){
		cout<<aa<<":"<<bb<<endl;
	}*/
	int x=find(aa);
	int y=find(bb);
	if(x==y){
		kk[i].pb({-1,-1});
	}
	else{
		if(ss[x]<ss[y]){
			swap(x,y);
		}
		co--;
		par[y]=x;
		ss[x]+=ss[y];
		kk[i].pb({x,y});
	}
}
void rollback(int i){
	assert(kk[i].size()>0);

	if(kk[i].back().a==-1){
		kk[i].pop_back();
	}
	else{
		pair<int,int> no=kk[i].back();
		co++;
		par[no.b]=no.b;
		ss[no.a]-=ss[no.b];
		kk[i].pop_back();
	}
}

vector<int> simulateCollapse(
	int n,
	vector<int> tt,
	vector<int> aa,
	vector<int> bb,
	vector<int> ww,
	vector<int> pp
) {
	int m=aa.size();
	int q=ww.size();
	vector<int> cc;
	map<pair<int,int>,int> ss2;
	int ind=0;
	const int ee=300;
	for(int j=0;j<m;j++){
		cur[j]=0;
	}
	for(int i=0;i<q;i++){
		pre3[(ww[i]-(ww[i]%ee))].pb({ww[i],i});
	}
	for(int i=0;i<m;i++){
		if(aa[i]>bb[i]){
			swap(aa[i],bb[i]);
		}
		if(ss2.find({aa[i],bb[i]})!=ss2.end()){
			cc.pb(ss2[{aa[i],bb[i]}]);
		}
		else{
			ss2[{aa[i],bb[i]}]=ind;
			cc.pb(ind);
			cur[ind]=0;
			ind++;
			pre2[aa[i]].pb({ss2[{aa[i],bb[i]}],bb[i]});
			pre[bb[i]].pb({ss2[{aa[i],bb[i]}],aa[i]});
		}
	}

	vector<int> ans;
	for(int i=0;i<q;i++){
		ans.pb(0);
	}
	int kk2=-1;
	for(int i=0;i<m;i+=ee){
		for(int j=kk2+1;j<i;j++){
			cur[cc[j]]^=1;
		}
		kk2=i-1;
		for(int j=0;j<n;j++){
			pre4[j].clear();
		}
		for(auto j:pre3[i]){
			pre4[pp[j.b]].pb(j.b);
		}
		for(int j=0;j<m;j++){
			vis[j]=0;
		}
		co=n;
		for(int j=0;j<3;j++){
			kk[j].clear();
		}
		for(int j=0;j<n;j++){
			par[j]=j;
			ss[j]=1;
		}
		vector<int> ll;
		for(int j=i;j<i+ee and j<m;j++){
			if(vis[cc[j]]==0){
				ll.pb(j);
			}
			vis[cc[j]]=1;
		}
		for(int j=0;j<3;j++){
			kk[j].clear();
		}
		for(int ii=n-1;ii>=0;ii--){
			for(auto j:pre2[ii]){
				if(vis[j.a]==0 and cur[j.a]==1){
					merge(ii,j.b,0);
					//cout<<ii<<"::"<<j.b<<endl;
				}
			}
		}
	
		for(int ii=0;ii<n;ii++){
			for(auto j:pre2[ii]){
				if(vis[j.a]==0 and cur[j.a]==1){
				//	cout<<11<<endl;
					rollback(0);
				}
			}
			for(auto j:pre[ii]){
				if(vis[j.a]==0 and cur[j.a]==1){
					merge(ii,j.b,1);
				}
			}
			for(auto j:pre4[ii]){
				int w=ww[j];
				for(int x=i;x<=w;x++){
					cur[cc[x]]^=1;
				}
				for(auto x:ll){
				//	cout<<x<<",,"<<cur[x]<<":"<<par[x]<<endl;
					if(cur[cc[x]]==1 and (bb[x]<=ii or aa[x]>ii)){
					//	cout<<aa[x]<<":"<<bb[x]<<":"<<x<<endl;
						merge(aa[x],bb[x],2);
					}
				}
				for(int x=i;x<=w;x++){
					cur[cc[x]]^=1;
				}
				ans[j]=co;
				while(kk[2].size()){
					rollback(2);
				}
			}
		}

	}




	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 10068 KB Output is correct
2 Correct 7 ms 9812 KB Output is correct
3 Correct 6 ms 9896 KB Output is correct
4 Correct 6 ms 9844 KB Output is correct
5 Correct 12 ms 10092 KB Output is correct
6 Correct 17 ms 10708 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 13 ms 10580 KB Output is correct
10 Correct 20 ms 10668 KB Output is correct
11 Correct 25 ms 10896 KB Output is correct
12 Correct 23 ms 10968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 13292 KB Output is correct
2 Correct 31 ms 13300 KB Output is correct
3 Correct 100 ms 18180 KB Output is correct
4 Correct 46 ms 13768 KB Output is correct
5 Correct 183 ms 18640 KB Output is correct
6 Correct 142 ms 14248 KB Output is correct
7 Correct 710 ms 29196 KB Output is correct
8 Correct 395 ms 23668 KB Output is correct
9 Correct 30 ms 14816 KB Output is correct
10 Correct 35 ms 14880 KB Output is correct
11 Correct 156 ms 14716 KB Output is correct
12 Correct 1030 ms 25484 KB Output is correct
13 Correct 1873 ms 27768 KB Output is correct
14 Correct 3774 ms 32108 KB Output is correct
15 Correct 2849 ms 31748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 13304 KB Output is correct
2 Correct 29 ms 13044 KB Output is correct
3 Correct 37 ms 13120 KB Output is correct
4 Correct 48 ms 13040 KB Output is correct
5 Correct 164 ms 13012 KB Output is correct
6 Correct 159 ms 13388 KB Output is correct
7 Correct 494 ms 23884 KB Output is correct
8 Correct 1186 ms 28092 KB Output is correct
9 Correct 36 ms 16328 KB Output is correct
10 Correct 220 ms 15444 KB Output is correct
11 Correct 2968 ms 30556 KB Output is correct
12 Correct 3907 ms 31104 KB Output is correct
13 Correct 3043 ms 30656 KB Output is correct
14 Correct 3917 ms 31120 KB Output is correct
15 Correct 3051 ms 30556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 10068 KB Output is correct
2 Correct 7 ms 9812 KB Output is correct
3 Correct 6 ms 9896 KB Output is correct
4 Correct 6 ms 9844 KB Output is correct
5 Correct 12 ms 10092 KB Output is correct
6 Correct 17 ms 10708 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 13 ms 10580 KB Output is correct
10 Correct 20 ms 10668 KB Output is correct
11 Correct 25 ms 10896 KB Output is correct
12 Correct 23 ms 10968 KB Output is correct
13 Correct 25 ms 13292 KB Output is correct
14 Correct 31 ms 13300 KB Output is correct
15 Correct 100 ms 18180 KB Output is correct
16 Correct 46 ms 13768 KB Output is correct
17 Correct 183 ms 18640 KB Output is correct
18 Correct 142 ms 14248 KB Output is correct
19 Correct 710 ms 29196 KB Output is correct
20 Correct 395 ms 23668 KB Output is correct
21 Correct 30 ms 14816 KB Output is correct
22 Correct 35 ms 14880 KB Output is correct
23 Correct 156 ms 14716 KB Output is correct
24 Correct 1030 ms 25484 KB Output is correct
25 Correct 1873 ms 27768 KB Output is correct
26 Correct 3774 ms 32108 KB Output is correct
27 Correct 2849 ms 31748 KB Output is correct
28 Correct 24 ms 13304 KB Output is correct
29 Correct 29 ms 13044 KB Output is correct
30 Correct 37 ms 13120 KB Output is correct
31 Correct 48 ms 13040 KB Output is correct
32 Correct 164 ms 13012 KB Output is correct
33 Correct 159 ms 13388 KB Output is correct
34 Correct 494 ms 23884 KB Output is correct
35 Correct 1186 ms 28092 KB Output is correct
36 Correct 36 ms 16328 KB Output is correct
37 Correct 220 ms 15444 KB Output is correct
38 Correct 2968 ms 30556 KB Output is correct
39 Correct 3907 ms 31104 KB Output is correct
40 Correct 3043 ms 30656 KB Output is correct
41 Correct 3917 ms 31120 KB Output is correct
42 Correct 3051 ms 30556 KB Output is correct
43 Correct 295 ms 22724 KB Output is correct
44 Correct 726 ms 29512 KB Output is correct
45 Correct 414 ms 23604 KB Output is correct
46 Correct 1176 ms 30280 KB Output is correct
47 Correct 37 ms 17124 KB Output is correct
48 Correct 41 ms 15980 KB Output is correct
49 Correct 193 ms 16380 KB Output is correct
50 Correct 306 ms 18644 KB Output is correct
51 Correct 990 ms 26768 KB Output is correct
52 Correct 1740 ms 28616 KB Output is correct
53 Correct 1367 ms 27760 KB Output is correct
54 Correct 2323 ms 30472 KB Output is correct
55 Correct 1857 ms 29572 KB Output is correct
56 Correct 2263 ms 30588 KB Output is correct
57 Correct 2723 ms 31892 KB Output is correct
58 Correct 3392 ms 32424 KB Output is correct
59 Correct 3127 ms 33092 KB Output is correct
60 Correct 3881 ms 33604 KB Output is correct