Submission #368847

# Submission time Handle Problem Language Result Execution time Memory
368847 2021-02-20T07:39:44 Z kig9981 Collapse (JOI18_collapse) C++17
100 / 100
12160 ms 379592 KB
#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=500;
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]) {
      |              ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8684 KB Output is correct
2 Correct 9 ms 8428 KB Output is correct
3 Correct 14 ms 8684 KB Output is correct
4 Correct 17 ms 8940 KB Output is correct
5 Correct 28 ms 9228 KB Output is correct
6 Correct 66 ms 15852 KB Output is correct
7 Correct 15 ms 8428 KB Output is correct
8 Correct 12 ms 8684 KB Output is correct
9 Correct 32 ms 10092 KB Output is correct
10 Correct 80 ms 14188 KB Output is correct
11 Correct 97 ms 16108 KB Output is correct
12 Correct 74 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 11744 KB Output is correct
2 Correct 65 ms 14180 KB Output is correct
3 Correct 303 ms 25616 KB Output is correct
4 Correct 133 ms 21472 KB Output is correct
5 Correct 641 ms 70904 KB Output is correct
6 Correct 638 ms 109792 KB Output is correct
7 Correct 928 ms 123416 KB Output is correct
8 Correct 677 ms 80220 KB Output is correct
9 Correct 58 ms 12660 KB Output is correct
10 Correct 112 ms 15332 KB Output is correct
11 Correct 748 ms 92516 KB Output is correct
12 Correct 755 ms 91268 KB Output is correct
13 Correct 1006 ms 120240 KB Output is correct
14 Correct 994 ms 121788 KB Output is correct
15 Correct 898 ms 118652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 11648 KB Output is correct
2 Correct 78 ms 14188 KB Output is correct
3 Correct 106 ms 17760 KB Output is correct
4 Correct 144 ms 22624 KB Output is correct
5 Correct 1010 ms 135348 KB Output is correct
6 Correct 747 ms 136932 KB Output is correct
7 Correct 2625 ms 277120 KB Output is correct
8 Correct 5761 ms 377288 KB Output is correct
9 Correct 56 ms 12512 KB Output is correct
10 Correct 859 ms 136648 KB Output is correct
11 Correct 9572 ms 359592 KB Output is correct
12 Correct 12160 ms 378776 KB Output is correct
13 Correct 9896 ms 368036 KB Output is correct
14 Correct 12034 ms 379492 KB Output is correct
15 Correct 9436 ms 356044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8684 KB Output is correct
2 Correct 9 ms 8428 KB Output is correct
3 Correct 14 ms 8684 KB Output is correct
4 Correct 17 ms 8940 KB Output is correct
5 Correct 28 ms 9228 KB Output is correct
6 Correct 66 ms 15852 KB Output is correct
7 Correct 15 ms 8428 KB Output is correct
8 Correct 12 ms 8684 KB Output is correct
9 Correct 32 ms 10092 KB Output is correct
10 Correct 80 ms 14188 KB Output is correct
11 Correct 97 ms 16108 KB Output is correct
12 Correct 74 ms 16364 KB Output is correct
13 Correct 43 ms 11744 KB Output is correct
14 Correct 65 ms 14180 KB Output is correct
15 Correct 303 ms 25616 KB Output is correct
16 Correct 133 ms 21472 KB Output is correct
17 Correct 641 ms 70904 KB Output is correct
18 Correct 638 ms 109792 KB Output is correct
19 Correct 928 ms 123416 KB Output is correct
20 Correct 677 ms 80220 KB Output is correct
21 Correct 58 ms 12660 KB Output is correct
22 Correct 112 ms 15332 KB Output is correct
23 Correct 748 ms 92516 KB Output is correct
24 Correct 755 ms 91268 KB Output is correct
25 Correct 1006 ms 120240 KB Output is correct
26 Correct 994 ms 121788 KB Output is correct
27 Correct 898 ms 118652 KB Output is correct
28 Correct 63 ms 11648 KB Output is correct
29 Correct 78 ms 14188 KB Output is correct
30 Correct 106 ms 17760 KB Output is correct
31 Correct 144 ms 22624 KB Output is correct
32 Correct 1010 ms 135348 KB Output is correct
33 Correct 747 ms 136932 KB Output is correct
34 Correct 2625 ms 277120 KB Output is correct
35 Correct 5761 ms 377288 KB Output is correct
36 Correct 56 ms 12512 KB Output is correct
37 Correct 859 ms 136648 KB Output is correct
38 Correct 9572 ms 359592 KB Output is correct
39 Correct 12160 ms 378776 KB Output is correct
40 Correct 9896 ms 368036 KB Output is correct
41 Correct 12034 ms 379492 KB Output is correct
42 Correct 9436 ms 356044 KB Output is correct
43 Correct 1566 ms 74008 KB Output is correct
44 Correct 4592 ms 378664 KB Output is correct
45 Correct 1999 ms 95324 KB Output is correct
46 Correct 5613 ms 377064 KB Output is correct
47 Correct 64 ms 12388 KB Output is correct
48 Correct 100 ms 16504 KB Output is correct
49 Correct 758 ms 108772 KB Output is correct
50 Correct 1161 ms 139556 KB Output is correct
51 Correct 3258 ms 105436 KB Output is correct
52 Correct 5669 ms 192600 KB Output is correct
53 Correct 5018 ms 190712 KB Output is correct
54 Correct 7204 ms 241472 KB Output is correct
55 Correct 6200 ms 233084 KB Output is correct
56 Correct 7505 ms 281952 KB Output is correct
57 Correct 8273 ms 322684 KB Output is correct
58 Correct 10388 ms 334584 KB Output is correct
59 Correct 8635 ms 355292 KB Output is correct
60 Correct 12010 ms 379592 KB Output is correct