Submission #145637

# Submission time Handle Problem Language Result Execution time Memory
145637 2019-08-20T14:47:15 Z TadijaSebez Collapse (JOI18_collapse) C++11
100 / 100
7133 ms 23668 KB
#include "collapse.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=100050;
const int S=317;
struct DSU
{
	int p[N],z[N],n,cmp,S[N],c;
	void init(int _n){ n=_n;for(int i=1;i<=n;i++) p[i]=i,z[i]=1;cmp=n;c=0;}
	DSU(){}
	int Find(int x){ return p[x]==x?x:Find(p[x]);}
	void Union(int x, int y, bool bck)
	{
		x=Find(x);y=Find(y);
		if(x==y){ if(bck) S[++c]=0;return;}
		cmp--;
		if(z[x]>z[y]) swap(x,y);
		p[x]=y;
		z[y]+=z[x];
		S[++c]=x;
	}
	void Undo()
	{
		if(!S[c]) c--;
		else
		{
			cmp++;
			int x=S[c--];
            z[p[x]]-=z[x];
            p[x]=x;
		}
	}
} DS;
int n,m,q;
int t[N],x[N],y[N],rng[N],w[N],p[N],ans[N];
map<pair<int,int>,int> last;
vector<int> Qs[N];
vector<int> Solve()
{
	for(int i=1;i<=m;i++)
	{
		if(t[i]==0) rng[i]=m,last[{x[i],y[i]}]=i;
		else rng[last[{x[i],y[i]}]]=i-1;
	}
	for(int l=1;l<=m;l+=S)
	{
		int r=min(m,l+S-1);
		vector<int> add,now;
		for(int i=1;i<=r;i++)
		{
			if(t[i]==1 || rng[i]<l) continue;
			if(rng[i]<=r || i>=l) add.pb(i);
			else now.pb(i);
		}
		vector<int> qs;
		for(int i=l;i<=r;i++) for(int Q:Qs[i]) qs.pb(Q);
		sort(now.begin(),now.end(),[&](int i, int j){ return y[i]<y[j];});
		sort(qs.begin(),qs.end(),[&](int i, int j){ return p[i]<p[j];});
        int e_ptr=0,q_ptr=0;
        DS.init(n);
        for(int i=1;i<=n;i++)
		{
			while(e_ptr<now.size() && y[now[e_ptr]]==i) DS.Union(x[now[e_ptr]],y[now[e_ptr]],0),e_ptr++;
			while(q_ptr<qs.size() && p[qs[q_ptr]]==i)
			{
				int qid=qs[q_ptr];
				int cnt=0;
				for(int e:add) if(w[qid]>=e && w[qid]<=rng[e] && y[e]<=p[qid]) DS.Union(x[e],y[e],1),cnt++;
				ans[qid]+=DS.cmp-(n-i);
				while(cnt--) DS.Undo();
				q_ptr++;
			}
		}
		sort(now.begin(),now.end(),[&](int i, int j){ return x[i]>x[j];});
		reverse(qs.begin(),qs.end());
		e_ptr=q_ptr=0;
		DS.init(n);
		for(int i=n;i>=1;i--)
		{
			while(e_ptr<now.size() && x[now[e_ptr]]==i) DS.Union(x[now[e_ptr]],y[now[e_ptr]],0),e_ptr++;
			while(q_ptr<qs.size() && p[qs[q_ptr]]==i-1)
			{
				int qid=qs[q_ptr];
				int cnt=0;
				for(int e:add) if(w[qid]>=e && w[qid]<=rng[e] && x[e]>p[qid]) DS.Union(x[e],y[e],1),cnt++;
				ans[qid]+=DS.cmp-p[qid];
				while(cnt--) DS.Undo();
				q_ptr++;
			}
		}
	}
	vector<int> ret;
	for(int i=1;i<=q;i++) ret.pb(ans[i]);
	return ret;
}
vector<int> simulateCollapse(int _n, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P)
{
	n=_n;m=X.size();q=W.size();
	for(int i=1;i<=m;i++) t[i]=T[i-1],x[i]=X[i-1]+1,y[i]=Y[i-1]+1;
	for(int i=1;i<=q;i++) w[i]=W[i-1]+1,p[i]=P[i-1]+1,Qs[w[i]].pb(i);
	for(int i=1;i<=m;i++) if(x[i]>y[i]) swap(x[i],y[i]);
	return Solve();
}

Compilation message

collapse.cpp: In function 'std::vector<int> Solve()':
collapse.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(e_ptr<now.size() && y[now[e_ptr]]==i) DS.Union(x[now[e_ptr]],y[now[e_ptr]],0),e_ptr++;
          ~~~~~^~~~~~~~~~~
collapse.cpp:65:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(q_ptr<qs.size() && p[qs[q_ptr]]==i)
          ~~~~~^~~~~~~~~~
collapse.cpp:81:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(e_ptr<now.size() && x[now[e_ptr]]==i) DS.Union(x[now[e_ptr]],y[now[e_ptr]],0),e_ptr++;
          ~~~~~^~~~~~~~~~~
collapse.cpp:82:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(q_ptr<qs.size() && p[qs[q_ptr]]==i-1)
          ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 6 ms 2936 KB Output is correct
3 Correct 7 ms 2936 KB Output is correct
4 Correct 7 ms 2936 KB Output is correct
5 Correct 12 ms 3320 KB Output is correct
6 Correct 42 ms 3704 KB Output is correct
7 Correct 7 ms 3064 KB Output is correct
8 Correct 7 ms 3064 KB Output is correct
9 Correct 15 ms 3576 KB Output is correct
10 Correct 30 ms 3576 KB Output is correct
11 Correct 66 ms 3704 KB Output is correct
12 Correct 54 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7140 KB Output is correct
2 Correct 39 ms 7272 KB Output is correct
3 Correct 298 ms 14556 KB Output is correct
4 Correct 61 ms 7564 KB Output is correct
5 Correct 377 ms 15220 KB Output is correct
6 Correct 425 ms 8292 KB Output is correct
7 Correct 3396 ms 21972 KB Output is correct
8 Correct 453 ms 18676 KB Output is correct
9 Correct 43 ms 8296 KB Output is correct
10 Correct 49 ms 8340 KB Output is correct
11 Correct 249 ms 8596 KB Output is correct
12 Correct 693 ms 19700 KB Output is correct
13 Correct 2609 ms 21136 KB Output is correct
14 Correct 6604 ms 23568 KB Output is correct
15 Correct 4829 ms 23668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 7140 KB Output is correct
2 Correct 44 ms 7208 KB Output is correct
3 Correct 63 ms 7400 KB Output is correct
4 Correct 69 ms 7548 KB Output is correct
5 Correct 319 ms 7404 KB Output is correct
6 Correct 547 ms 8216 KB Output is correct
7 Correct 2312 ms 17880 KB Output is correct
8 Correct 4465 ms 21444 KB Output is correct
9 Correct 58 ms 8296 KB Output is correct
10 Correct 431 ms 8556 KB Output is correct
11 Correct 5310 ms 22444 KB Output is correct
12 Correct 7133 ms 22532 KB Output is correct
13 Correct 5828 ms 22324 KB Output is correct
14 Correct 7101 ms 22552 KB Output is correct
15 Correct 5422 ms 22384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 6 ms 2936 KB Output is correct
3 Correct 7 ms 2936 KB Output is correct
4 Correct 7 ms 2936 KB Output is correct
5 Correct 12 ms 3320 KB Output is correct
6 Correct 42 ms 3704 KB Output is correct
7 Correct 7 ms 3064 KB Output is correct
8 Correct 7 ms 3064 KB Output is correct
9 Correct 15 ms 3576 KB Output is correct
10 Correct 30 ms 3576 KB Output is correct
11 Correct 66 ms 3704 KB Output is correct
12 Correct 54 ms 3704 KB Output is correct
13 Correct 36 ms 7140 KB Output is correct
14 Correct 39 ms 7272 KB Output is correct
15 Correct 298 ms 14556 KB Output is correct
16 Correct 61 ms 7564 KB Output is correct
17 Correct 377 ms 15220 KB Output is correct
18 Correct 425 ms 8292 KB Output is correct
19 Correct 3396 ms 21972 KB Output is correct
20 Correct 453 ms 18676 KB Output is correct
21 Correct 43 ms 8296 KB Output is correct
22 Correct 49 ms 8340 KB Output is correct
23 Correct 249 ms 8596 KB Output is correct
24 Correct 693 ms 19700 KB Output is correct
25 Correct 2609 ms 21136 KB Output is correct
26 Correct 6604 ms 23568 KB Output is correct
27 Correct 4829 ms 23668 KB Output is correct
28 Correct 36 ms 7140 KB Output is correct
29 Correct 44 ms 7208 KB Output is correct
30 Correct 63 ms 7400 KB Output is correct
31 Correct 69 ms 7548 KB Output is correct
32 Correct 319 ms 7404 KB Output is correct
33 Correct 547 ms 8216 KB Output is correct
34 Correct 2312 ms 17880 KB Output is correct
35 Correct 4465 ms 21444 KB Output is correct
36 Correct 58 ms 8296 KB Output is correct
37 Correct 431 ms 8556 KB Output is correct
38 Correct 5310 ms 22444 KB Output is correct
39 Correct 7133 ms 22532 KB Output is correct
40 Correct 5828 ms 22324 KB Output is correct
41 Correct 7101 ms 22552 KB Output is correct
42 Correct 5422 ms 22384 KB Output is correct
43 Correct 447 ms 17060 KB Output is correct
44 Correct 3486 ms 21032 KB Output is correct
45 Correct 523 ms 17536 KB Output is correct
46 Correct 4563 ms 21352 KB Output is correct
47 Correct 58 ms 8296 KB Output is correct
48 Correct 68 ms 8396 KB Output is correct
49 Correct 340 ms 8844 KB Output is correct
50 Correct 528 ms 10232 KB Output is correct
51 Correct 845 ms 18424 KB Output is correct
52 Correct 2097 ms 19324 KB Output is correct
53 Correct 1787 ms 18824 KB Output is correct
54 Correct 3319 ms 20084 KB Output is correct
55 Correct 2795 ms 20024 KB Output is correct
56 Correct 3758 ms 20848 KB Output is correct
57 Correct 4742 ms 21536 KB Output is correct
58 Correct 5844 ms 21816 KB Output is correct
59 Correct 5293 ms 22524 KB Output is correct
60 Correct 7039 ms 22524 KB Output is correct