Submission #368833

# Submission time Handle Problem Language Result Execution time Memory
368833 2021-02-20T07:32:44 Z kig9981 Collapse (JOI18_collapse) C++17
35 / 100
15000 ms 480016 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=300, SZ=1<<17;
vector<int> adj[100000], BB, tree[2*SZ];
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 14 ms 14828 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 16 ms 14956 KB Output is correct
4 Correct 17 ms 15104 KB Output is correct
5 Correct 27 ms 15340 KB Output is correct
6 Correct 56 ms 20076 KB Output is correct
7 Correct 17 ms 14672 KB Output is correct
8 Correct 16 ms 14828 KB Output is correct
9 Correct 40 ms 16108 KB Output is correct
10 Correct 61 ms 18540 KB Output is correct
11 Correct 68 ms 20076 KB Output is correct
12 Correct 75 ms 20204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 17804 KB Output is correct
2 Correct 66 ms 20324 KB Output is correct
3 Correct 293 ms 31396 KB Output is correct
4 Correct 151 ms 27616 KB Output is correct
5 Correct 516 ms 61996 KB Output is correct
6 Correct 447 ms 77920 KB Output is correct
7 Correct 752 ms 90256 KB Output is correct
8 Correct 534 ms 69008 KB Output is correct
9 Correct 52 ms 18656 KB Output is correct
10 Correct 82 ms 21612 KB Output is correct
11 Correct 454 ms 72164 KB Output is correct
12 Correct 616 ms 76304 KB Output is correct
13 Correct 787 ms 88700 KB Output is correct
14 Correct 772 ms 90588 KB Output is correct
15 Correct 706 ms 87904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 18020 KB Output is correct
2 Correct 86 ms 20248 KB Output is correct
3 Correct 111 ms 24052 KB Output is correct
4 Correct 181 ms 28864 KB Output is correct
5 Correct 540 ms 82416 KB Output is correct
6 Correct 533 ms 94308 KB Output is correct
7 Correct 3832 ms 318256 KB Output is correct
8 Correct 7833 ms 478292 KB Output is correct
9 Correct 76 ms 19572 KB Output is correct
10 Correct 613 ms 89568 KB Output is correct
11 Correct 13349 ms 450340 KB Output is correct
12 Execution timed out 15132 ms 480016 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14828 KB Output is correct
2 Correct 13 ms 14592 KB Output is correct
3 Correct 16 ms 14956 KB Output is correct
4 Correct 17 ms 15104 KB Output is correct
5 Correct 27 ms 15340 KB Output is correct
6 Correct 56 ms 20076 KB Output is correct
7 Correct 17 ms 14672 KB Output is correct
8 Correct 16 ms 14828 KB Output is correct
9 Correct 40 ms 16108 KB Output is correct
10 Correct 61 ms 18540 KB Output is correct
11 Correct 68 ms 20076 KB Output is correct
12 Correct 75 ms 20204 KB Output is correct
13 Correct 57 ms 17804 KB Output is correct
14 Correct 66 ms 20324 KB Output is correct
15 Correct 293 ms 31396 KB Output is correct
16 Correct 151 ms 27616 KB Output is correct
17 Correct 516 ms 61996 KB Output is correct
18 Correct 447 ms 77920 KB Output is correct
19 Correct 752 ms 90256 KB Output is correct
20 Correct 534 ms 69008 KB Output is correct
21 Correct 52 ms 18656 KB Output is correct
22 Correct 82 ms 21612 KB Output is correct
23 Correct 454 ms 72164 KB Output is correct
24 Correct 616 ms 76304 KB Output is correct
25 Correct 787 ms 88700 KB Output is correct
26 Correct 772 ms 90588 KB Output is correct
27 Correct 706 ms 87904 KB Output is correct
28 Correct 53 ms 18020 KB Output is correct
29 Correct 86 ms 20248 KB Output is correct
30 Correct 111 ms 24052 KB Output is correct
31 Correct 181 ms 28864 KB Output is correct
32 Correct 540 ms 82416 KB Output is correct
33 Correct 533 ms 94308 KB Output is correct
34 Correct 3832 ms 318256 KB Output is correct
35 Correct 7833 ms 478292 KB Output is correct
36 Correct 76 ms 19572 KB Output is correct
37 Correct 613 ms 89568 KB Output is correct
38 Correct 13349 ms 450340 KB Output is correct
39 Execution timed out 15132 ms 480016 KB Time limit exceeded
40 Halted 0 ms 0 KB -