답안 #338362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338362 2020-12-23T04:00:00 Z GioChkhaidze Collapse (JOI18_collapse) C++14
100 / 100
5898 ms 23164 KB
#pragma GCC diagnostic warning "-std=c++11"
#include <bits/stdc++.h>
#include "collapse.h"

#define pb push_back
#define F first
#define S second

using namespace std;

const int N=1e5+5;
const int Sq=777;

bool f[N],fr[N],Tt[N];
int n,m,q,x,y,l,r,a,b,sz,sq,id,ids,cmp,idx,res;
int Xx[N],Yy[N],Pp[N],Ww[N],p[N],last[N],curl[N],ed[N];
  
vector < int > s,ans,Qu[Sq],U[N];
vector < pair < pair < int , int > , int > > rec;

map < pair < int , int > , int > fid;

void roll() {
	while (rec.size()) {
		x=rec.back().F.S;
		p[x]=rec.back().F.F;
		cmp=rec.back().S;
		rec.pop_back();
	}
}

int P(int x,bool tp) {
	if (p[x]==x) return x;
	y=P(p[x],tp);
	if (tp) rec.pb({{p[x],x},cmp});
	return p[x]=y;
}

void Uni(int a,int b,bool tp) {
	a=P(a,tp),b=P(b,tp);
	if (a==b) return ;
	if (tp) rec.pb({{p[b],b},cmp});
	p[b]=a;
	--cmp;
}

bool comp(int a,int b) {
	return (Pp[a]<Pp[b]);
}

void Solve() {
	sz=0;
	fid.clear();
	for (int i=0; i<m; i++) {
		if (Xx[i]>Yy[i]) swap(Xx[i],Yy[i]);	
		if (!fid[{Xx[i],Yy[i]}]) fid[{Xx[i],Yy[i]}]=++sz;
		ed[i]=fid[{Xx[i],Yy[i]}];
	}
	
	for (int i=0; i<q; i++) 
		Qu[Ww[i]/sq].pb(i);

	for (int i=0; i*sq<m; i++) {
		if (!Qu[i].size()) continue;
		sort(Qu[i].begin(),Qu[i].end(),comp);		
		l=i*sq,r=min((i+1)*sq,m);
		
		for (int j=0; j<n; j++) 
			p[j]=j,U[j].clear();
	
		for (int j=1; j<=sz; j++) 
			f[j]=fr[j]=false,last[j]=-1;

		s.clear();
		for (int j=l; j<r; j++) 
			if (!f[ed[j]]) f[ed[j]]=true,s.pb(ed[j]);	

		for (int j=l-1; j>=0; j--) { 
			idx=ed[j]; 
			if (fr[idx]) continue;
			fr[idx]=true;
			last[idx]=j;
			if (f[idx]) continue; 
			if (!Tt[j]) U[Yy[j]].pb(Xx[j]); 
		}
		
		cmp=0;
		ids=-1;
		for (int k=0; k<Qu[i].size(); k++) {
			res=Qu[i][k];
			x=Pp[res]; 
			
			while (ids+1<=x) {
				++ids,++cmp;
				for (int j=0; j<U[ids].size(); j++) {
					a=U[ids][j],b=ids;
					Uni(a,b,0);
				}				
			}
			
			for (int j=0; j<s.size(); j++) 
				curl[s[j]]=last[s[j]];

			for (int j=l; j<=Ww[res]; j++) 
				curl[ed[j]]=j;
			
			for (int j=0; j<s.size(); j++) {
				id=curl[s[j]];
				if (id!=-1 && !Tt[id] && Yy[id]<=x) 
					Uni(Xx[id],Yy[id],1);
			}

			ans[res]+=cmp;
			for (int j=0; j<s.size(); j++) 
				curl[s[j]]=-1;
				
			roll();
		}
		
		Qu[i].clear();
	}
}

vector<int> simulateCollapse(
	int N,
	vector<int> T,
	vector<int> X,
	vector<int> Y,
	vector<int> W,
	vector<int> P
) {
	n=N,m=T.size(),sq=sqrt(m),q=W.size();
	for (int i=0; i<m; i++) 
		Tt[i]=T[i],Xx[i]=X[i],Yy[i]=Y[i];
	
	ans.resize(q,0);	
	for (int i=0; i<q; i++) 
		Ww[i]=W[i],Pp[i]=P[i];
	
	Solve();
	
	for (int i=0; i<m; i++) 
		Xx[i]=n-1-Xx[i],Yy[i]=n-1-Yy[i];
	
	for (int i=0; i<q; i++) 
		Pp[i]=n-1-(Pp[i]+1);

	Solve();
	return ans;
}

Compilation message

collapse.cpp:1:32: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
    1 | #pragma GCC diagnostic warning "-std=c++11"
      |                                ^~~~~~~~~~~~
collapse.cpp: In function 'void Solve()':
collapse.cpp:89:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |   for (int k=0; k<Qu[i].size(); k++) {
      |                 ~^~~~~~~~~~~~~
collapse.cpp:95:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for (int j=0; j<U[ids].size(); j++) {
      |                   ~^~~~~~~~~~~~~~
collapse.cpp:101:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |    for (int j=0; j<s.size(); j++)
      |                  ~^~~~~~~~~
collapse.cpp:107:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |    for (int j=0; j<s.size(); j++) {
      |                  ~^~~~~~~~~
collapse.cpp:114:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |    for (int j=0; j<s.size(); j++)
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3180 KB Output is correct
2 Correct 5 ms 2924 KB Output is correct
3 Correct 5 ms 2924 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 14 ms 3180 KB Output is correct
6 Correct 41 ms 3692 KB Output is correct
7 Correct 5 ms 3052 KB Output is correct
8 Correct 6 ms 3052 KB Output is correct
9 Correct 26 ms 3436 KB Output is correct
10 Correct 34 ms 3564 KB Output is correct
11 Correct 53 ms 3820 KB Output is correct
12 Correct 52 ms 3744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 6392 KB Output is correct
2 Correct 47 ms 6380 KB Output is correct
3 Correct 369 ms 10272 KB Output is correct
4 Correct 68 ms 6892 KB Output is correct
5 Correct 1058 ms 11244 KB Output is correct
6 Correct 290 ms 7632 KB Output is correct
7 Correct 2116 ms 18688 KB Output is correct
8 Correct 1175 ms 14628 KB Output is correct
9 Correct 49 ms 7268 KB Output is correct
10 Correct 56 ms 7660 KB Output is correct
11 Correct 168 ms 8044 KB Output is correct
12 Correct 1377 ms 17132 KB Output is correct
13 Correct 2587 ms 19948 KB Output is correct
14 Correct 3271 ms 22732 KB Output is correct
15 Correct 3058 ms 23164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 6372 KB Output is correct
2 Correct 59 ms 6360 KB Output is correct
3 Correct 61 ms 6636 KB Output is correct
4 Correct 66 ms 6508 KB Output is correct
5 Correct 150 ms 6764 KB Output is correct
6 Correct 336 ms 7020 KB Output is correct
7 Correct 2120 ms 15340 KB Output is correct
8 Correct 3704 ms 18552 KB Output is correct
9 Correct 72 ms 6756 KB Output is correct
10 Correct 205 ms 7916 KB Output is correct
11 Correct 5651 ms 22972 KB Output is correct
12 Correct 5883 ms 22724 KB Output is correct
13 Correct 5898 ms 22644 KB Output is correct
14 Correct 5661 ms 22764 KB Output is correct
15 Correct 5506 ms 22844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3180 KB Output is correct
2 Correct 5 ms 2924 KB Output is correct
3 Correct 5 ms 2924 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 14 ms 3180 KB Output is correct
6 Correct 41 ms 3692 KB Output is correct
7 Correct 5 ms 3052 KB Output is correct
8 Correct 6 ms 3052 KB Output is correct
9 Correct 26 ms 3436 KB Output is correct
10 Correct 34 ms 3564 KB Output is correct
11 Correct 53 ms 3820 KB Output is correct
12 Correct 52 ms 3744 KB Output is correct
13 Correct 43 ms 6392 KB Output is correct
14 Correct 47 ms 6380 KB Output is correct
15 Correct 369 ms 10272 KB Output is correct
16 Correct 68 ms 6892 KB Output is correct
17 Correct 1058 ms 11244 KB Output is correct
18 Correct 290 ms 7632 KB Output is correct
19 Correct 2116 ms 18688 KB Output is correct
20 Correct 1175 ms 14628 KB Output is correct
21 Correct 49 ms 7268 KB Output is correct
22 Correct 56 ms 7660 KB Output is correct
23 Correct 168 ms 8044 KB Output is correct
24 Correct 1377 ms 17132 KB Output is correct
25 Correct 2587 ms 19948 KB Output is correct
26 Correct 3271 ms 22732 KB Output is correct
27 Correct 3058 ms 23164 KB Output is correct
28 Correct 43 ms 6372 KB Output is correct
29 Correct 59 ms 6360 KB Output is correct
30 Correct 61 ms 6636 KB Output is correct
31 Correct 66 ms 6508 KB Output is correct
32 Correct 150 ms 6764 KB Output is correct
33 Correct 336 ms 7020 KB Output is correct
34 Correct 2120 ms 15340 KB Output is correct
35 Correct 3704 ms 18552 KB Output is correct
36 Correct 72 ms 6756 KB Output is correct
37 Correct 205 ms 7916 KB Output is correct
38 Correct 5651 ms 22972 KB Output is correct
39 Correct 5883 ms 22724 KB Output is correct
40 Correct 5898 ms 22644 KB Output is correct
41 Correct 5661 ms 22764 KB Output is correct
42 Correct 5506 ms 22844 KB Output is correct
43 Correct 1127 ms 15688 KB Output is correct
44 Correct 2838 ms 20232 KB Output is correct
45 Correct 1260 ms 16236 KB Output is correct
46 Correct 3704 ms 20620 KB Output is correct
47 Correct 73 ms 7576 KB Output is correct
48 Correct 83 ms 7660 KB Output is correct
49 Correct 211 ms 8172 KB Output is correct
50 Correct 628 ms 9836 KB Output is correct
51 Correct 1688 ms 17148 KB Output is correct
52 Correct 2613 ms 19240 KB Output is correct
53 Correct 2871 ms 18812 KB Output is correct
54 Correct 3364 ms 20204 KB Output is correct
55 Correct 3607 ms 19888 KB Output is correct
56 Correct 4401 ms 20820 KB Output is correct
57 Correct 5057 ms 21756 KB Output is correct
58 Correct 4766 ms 21968 KB Output is correct
59 Correct 5330 ms 22764 KB Output is correct
60 Correct 5604 ms 22712 KB Output is correct