답안 #165354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
165354 2019-11-26T14:08:01 Z tneluccus 늑대인간 (IOI18_werewolf) C++14
100 / 100
923 ms 97392 KB
#include<bits/stdc++.h>
using namespace std;
#define N cac
const int N=2e5+2;
vector<int> head[N];
int posL[N],bit[N],posR[N];
pair<int,int> idxL[N],idxR[N];
vector<int> adj[N];
vector<pair<int,int> > queryL[N],queryR[N],edgeL[N],edgeR[N];
vector<pair<pair<int,int>,pair<int,bool> > > lis[N]; 
void upd(int pos){
	while(pos<N){
		bit[pos]++;
		pos+=(pos&-pos);
	}
}
int get(int pos){
	int sum=0;
	while(pos){
		sum+=bit[pos];
		pos-=(pos&-pos);
	}
	return sum;
}
int getsum(int x,int y){
	return get(y)-get(x-1);
}
int findd(int x){
	if(head[x].size()>1){
		return x;
	}
	if(head[x][0]==x){
		return x;
	}
	int y=findd(head[x][0]);
	head[x][0]=y;
	return y;
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	if(head[x].size()<head[y].size()){
		swap(x,y);
	}
	for(int i:head[y]){
		head[x].push_back(i);
	}
	head[y].clear();
	head[y].push_back(x);
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
#undef N
vector<int> check_validity(int N,vector<int> X,vector<int> Y,vector<int> S,vector<int> E,vector<int> L,vector<int> R){
	int i,j,k,l,n=N,m=X.size(),q=S.size();
	vector<int> ans(q);
	for(i=0;i<m;i++){
		edgeL[min(X[i],Y[i])].push_back({X[i],Y[i]});
		edgeR[max(X[i],Y[i])].push_back({X[i],Y[i]});
	}
	for(i=0;i<n;i++){
		head[i].push_back(i);
	}
	for(i=0;i<q;i++){
		queryL[L[i]].push_back({S[i],i});
		queryR[R[i]].push_back({E[i],i});
	}
	for(i=n-1;i>-1;i--){
		for(auto x:edgeL[i]){
			if(!samee(x.first,x.second)){
				unionn(x.first,x.second);
			}
		}
		for(auto x:queryL[i]){
			j=findd(x.first);
//			cout<<x.second<<'\n';
//			for(int y:head[j]){
//				cout<<y<<' ';
//			}
//			cout<<'\n';
			idxL[x.second]={head[j][0],head[j].back()};
		}
	}
	j=findd(1);
	assert(head[j].size()==n);
	for(i=0;i<n;i++){
		posL[head[j][i]]=i;
	}
	for(i=0;i<n;i++){
		head[i].clear();
		head[i].push_back(i);
	}
	for(i=0;i<n;i++){
		for(auto x:edgeR[i]){
			if(!samee(x.first,x.second)){
				unionn(x.first,x.second);
			}
		}
		for(auto x:queryR[i]){
			j=findd(x.first);
//			cout<<x.second<<'\n';
//			for(int y:head[j]){
//				cout<<y<<' ';
//			}
//			cout<<'\n';
			idxR[x.second]={head[j][0],head[j].back()};
		}
	}
	j=findd(1);
	assert(head[j].size()==n);
	for(i=0;i<n;i++){
		posR[head[j][i]]=i;
	}
	for(i=0;i<q;i++){
		idxL[i]={posL[idxL[i].first],posL[idxL[i].second]};
		idxR[i]={posR[idxR[i].first],posR[idxR[i].second]};
		assert(idxL[i].first<=idxL[i].second);
		assert(idxR[i].first<=idxR[i].second);
		lis[idxR[i].second].push_back({idxL[i],{i,true}});
		if(idxR[i].first){
			lis[idxR[i].first-1].push_back({idxL[i],{i,false}});
		}
	}
	for(i=0;i<n;i++){
		upd(posL[head[j][i]]+1);
		for(auto x:lis[i]){
			if(!x.second.second){
				ans[x.second.first]-=getsum(x.first.first+1,x.first.second+1);
			}
			else{
				ans[x.second.first]+=getsum(x.first.first+1,x.first.second+1);
			}
		}
	}
	for(i=0;i<q;i++){
//		cout<<ans[i]<<endl;
		if(ans[i]>0){
			ans[i]=1;
		}
	}
	return ans;
}
//int main(){
//	vector<int> oh=check_validity({6},{5,1,1,3,3,5},{1,2,3,4,0,2},{4,4,5,3},{2,2,4,4},{1,2,3,3},{2,2,4,4});
//	cout<<oh.size()<<" ANS"<<endl;
//	for(int i:oh){
//		cout<<i<<'\n';
//	}
//}

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from werewolf.cpp:1:
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  assert(head[j].size()==n);
         ~~~~~~~~~~~~~~^~~
werewolf.cpp:110:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  assert(head[j].size()==n);
         ~~~~~~~~~~~~~~^~~
werewolf.cpp:55:10: warning: unused variable 'k' [-Wunused-variable]
  int i,j,k,l,n=N,m=X.size(),q=S.size();
          ^
werewolf.cpp:55:12: warning: unused variable 'l' [-Wunused-variable]
  int i,j,k,l,n=N,m=X.size(),q=S.size();
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 33272 KB Output is correct
2 Correct 33 ms 33272 KB Output is correct
3 Correct 33 ms 33272 KB Output is correct
4 Correct 33 ms 33272 KB Output is correct
5 Correct 34 ms 33272 KB Output is correct
6 Correct 35 ms 33400 KB Output is correct
7 Correct 33 ms 33272 KB Output is correct
8 Correct 33 ms 33272 KB Output is correct
9 Correct 33 ms 33272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 33272 KB Output is correct
2 Correct 33 ms 33272 KB Output is correct
3 Correct 33 ms 33272 KB Output is correct
4 Correct 33 ms 33272 KB Output is correct
5 Correct 34 ms 33272 KB Output is correct
6 Correct 35 ms 33400 KB Output is correct
7 Correct 33 ms 33272 KB Output is correct
8 Correct 33 ms 33272 KB Output is correct
9 Correct 33 ms 33272 KB Output is correct
10 Correct 43 ms 34120 KB Output is correct
11 Correct 43 ms 34040 KB Output is correct
12 Correct 40 ms 34168 KB Output is correct
13 Correct 41 ms 34168 KB Output is correct
14 Correct 39 ms 34148 KB Output is correct
15 Correct 40 ms 34228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 923 ms 91836 KB Output is correct
2 Correct 706 ms 87148 KB Output is correct
3 Correct 724 ms 89500 KB Output is correct
4 Correct 782 ms 92012 KB Output is correct
5 Correct 805 ms 92396 KB Output is correct
6 Correct 898 ms 94804 KB Output is correct
7 Correct 862 ms 96448 KB Output is correct
8 Correct 653 ms 87112 KB Output is correct
9 Correct 633 ms 88676 KB Output is correct
10 Correct 655 ms 93812 KB Output is correct
11 Correct 665 ms 94956 KB Output is correct
12 Correct 747 ms 94700 KB Output is correct
13 Correct 766 ms 92364 KB Output is correct
14 Correct 728 ms 92388 KB Output is correct
15 Correct 757 ms 92636 KB Output is correct
16 Correct 727 ms 92528 KB Output is correct
17 Correct 815 ms 96680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 33272 KB Output is correct
2 Correct 33 ms 33272 KB Output is correct
3 Correct 33 ms 33272 KB Output is correct
4 Correct 33 ms 33272 KB Output is correct
5 Correct 34 ms 33272 KB Output is correct
6 Correct 35 ms 33400 KB Output is correct
7 Correct 33 ms 33272 KB Output is correct
8 Correct 33 ms 33272 KB Output is correct
9 Correct 33 ms 33272 KB Output is correct
10 Correct 43 ms 34120 KB Output is correct
11 Correct 43 ms 34040 KB Output is correct
12 Correct 40 ms 34168 KB Output is correct
13 Correct 41 ms 34168 KB Output is correct
14 Correct 39 ms 34148 KB Output is correct
15 Correct 40 ms 34228 KB Output is correct
16 Correct 923 ms 91836 KB Output is correct
17 Correct 706 ms 87148 KB Output is correct
18 Correct 724 ms 89500 KB Output is correct
19 Correct 782 ms 92012 KB Output is correct
20 Correct 805 ms 92396 KB Output is correct
21 Correct 898 ms 94804 KB Output is correct
22 Correct 862 ms 96448 KB Output is correct
23 Correct 653 ms 87112 KB Output is correct
24 Correct 633 ms 88676 KB Output is correct
25 Correct 655 ms 93812 KB Output is correct
26 Correct 665 ms 94956 KB Output is correct
27 Correct 747 ms 94700 KB Output is correct
28 Correct 766 ms 92364 KB Output is correct
29 Correct 728 ms 92388 KB Output is correct
30 Correct 757 ms 92636 KB Output is correct
31 Correct 727 ms 92528 KB Output is correct
32 Correct 815 ms 96680 KB Output is correct
33 Correct 854 ms 92612 KB Output is correct
34 Correct 399 ms 79224 KB Output is correct
35 Correct 786 ms 89976 KB Output is correct
36 Correct 901 ms 93548 KB Output is correct
37 Correct 816 ms 90028 KB Output is correct
38 Correct 864 ms 92524 KB Output is correct
39 Correct 809 ms 90088 KB Output is correct
40 Correct 919 ms 97392 KB Output is correct
41 Correct 808 ms 89716 KB Output is correct
42 Correct 745 ms 92532 KB Output is correct
43 Correct 868 ms 93036 KB Output is correct
44 Correct 745 ms 89736 KB Output is correct
45 Correct 739 ms 88884 KB Output is correct
46 Correct 810 ms 89452 KB Output is correct
47 Correct 771 ms 92648 KB Output is correct
48 Correct 773 ms 92776 KB Output is correct
49 Correct 747 ms 92788 KB Output is correct
50 Correct 732 ms 92564 KB Output is correct
51 Correct 810 ms 96196 KB Output is correct
52 Correct 802 ms 96228 KB Output is correct