Submission #135262

# Submission time Handle Problem Language Result Execution time Memory
135262 2019-07-23T22:43:52 Z amiratou Werewolf (IOI18_werewolf) C++14
49 / 100
1664 ms 30984 KB
#pragma GCC optimize("O3")
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
#define INF (int)(1e9)

vector<vector<int> > vec;
int A[200005],idx=0,to_id[200005];
ii st[800005];
bool vis[200005];
void build(int node,int l,int r){
	if(l==r){
		st[node]=ii(A[l],A[l]);
		return ;
	}
	int med=((l+r)>>1);
	build(node<<1,l,med),build((node<<1)+1,med+1,r);
	st[node].fi=min(st[node<<1].fi,st[(node<<1)+1].fi);
	st[node].se=max(st[node<<1].se,st[(node<<1)+1].se);

}
ii query(int node,int l,int r,int i,int j){
	if(l>j||r<i)
		return ii(INF,-1);
	if(l>=i&&r<=j)
		return st[node];
	int med=((l+r)>>1);
	ii q1=query(node<<1,l,med,i,j),q2=query((node<<1)+1,med+1,r,i,j);
	return ii(min(q1.fi,q2.fi),max(q1.se,q2.se));
}
int D,LE,RE;
int vis_dfs[5001],v=1;
int dp[3001][2][2];
int dfs(int node,int changed,int trans){
	//cerr<<node<<" "<<changed<<" "<<trans<<"\n";
	if(node==D&&!changed&&trans)return 1;
	if(node==D)return changed;
	if(dp[node][changed][trans]!=-1)
		return dp[node][changed][trans];
	vis_dfs[node]=1;
	int ans=0;
	for(auto child:vec[node]){
		if(vis_dfs[child]==1)continue;
		if(child<LE&&!changed){
			if(trans)ans|=dfs(child,1,0);
			else continue;
		}
		if(child>RE&&changed)continue;
		if(child<=RE&&changed)ans|=dfs(child,1,0);
		if(child>=LE&&!changed)ans|=dfs(child,0,((child>=LE&&child<=RE)));
		if(trans&&child<=RE&&!changed)ans|=dfs(child,1,0);
		if(ans){vis_dfs[node]=0; return dp[node][changed][trans]=1;}
	}
	vis_dfs[node]=0;
	return dp[node][changed][trans]=0;
}
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 M = X.size();
	int Q = S.size();
	vec.resize(N);
	for (int i = 0; i < X.size(); ++i)
	{
		vec[X[i]].pb(Y[i]);
		vec[Y[i]].pb(X[i]);
	}
	if(N<=3000||M<=6000||Q<=3000){
		vector<int> res(Q);
		for (int i =0; i < Q; ++i)
		{
			memset(dp,-1,sizeof dp);
			D=E[i];
			LE=L[i],RE=R[i];
			//cerr<<S[i]<<","<<E[i]<<"\n";
			//cerr<<L[i]<<","<<R[i]<<"\n";
			if(S[i]<LE||E[i]>RE)res[i]=0;
			else res[i]=dfs(S[i],0,(S[i]>=L[i]&&S[i]<=R[i]));
		}	
		return res;
	}
	int start=0;
	for (int i = 0; i < N; ++i)
		if(vec[i].size()==1){
			start=i;
			break;
		}
	queue<int> AA;
	AA.push(start);
	vis[start]=1;
	while(!AA.empty()){
		int front=AA.front();
		A[idx]=front;
		to_id[front]=idx++;
		AA.pop();
		for(auto child:vec[front])
			if(!vis[child])
				vis[child]=1,AA.push(child);
	}
	build(1,0,idx);
	vector<int> res(Q);
	fill(res.begin(),res.end(),0);
	for (int i = 0; i < Q; ++i) {
		if(S[i]==E[i]){
			if(S[i]<=R[i]&&S[i]>=L[i])res[i]=1;
			else res[i]=0;
			continue;
		}
		if(S[i]<L[i]||E[i]>R[i]){res[i]=0;continue;}
		if(to_id[S[i]]<to_id[E[i]]){
			int l=to_id[S[i]],r=to_id[E[i]];
			while(l<=r){
				int med=((l+r)>>1);
				if(med!=to_id[S[i]]){
					ii q=query(1,0,idx,to_id[S[i]],med-1);
					if(q.fi<L[i]){r=med-1;continue;}
				}
				if(med!=to_id[E[i]]){
					ii q=query(1,0,idx,med+1,to_id[E[i]]);
					if(q.se>R[i]){l=med+1;continue;}
				}
				if(A[med]<=R[i]&&A[med]>=L[i])res[i]=1;
				else if(A[med]>=L[i]){
					if(A[med]==(E[i]))res[i]=0;
					else if(A[med+1]<=R[i]&&A[med+1]>=L[i])res[i]=1;
					else res[i]=0;
				}
				else if(A[med]<=R[i]){
					if(A[med]==S[i])res[i]=0;
					else if(A[med-1]<=R[i]&&A[med-1]>=L[i])res[i]=1;
					else res[i]=0;
				}
				break;
			}
		}
		else{
			int l=to_id[E[i]],r=to_id[S[i]];
			while(l<=r){
				int med=((l+r)>>1);
				if(med!=to_id[E[i]]){
					ii q=query(1,0,idx,to_id[E[i]],med-1);
					if(q.se>R[i]){r=med-1;continue;}
				}
				if(med!=to_id[S[i]]){
					ii q=query(1,0,idx,med+1,to_id[S[i]]);
					if(q.fi<L[i]){l=med+1;continue;}
				}
				if(A[med]<=R[i]&&A[med]>=L[i])res[i]=1;
				else if(A[med]<=R[i]){
					if(A[med]==(S[i]))res[i]=0;
					else if(A[med+1]<=R[i]&&A[med+1]>=L[i])res[i]=1;
					else res[i]=0;
				}
				else if(A[med]>=L[i]){
					if(A[med]==E[i])res[i]=0;
					else if(A[med-1]<=R[i]&&A[med-1]>=L[i])res[i]=1;
					else res[i]=0;
				}
				break;
			}		
		}}
	return res;
}

Compilation message

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:68:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < X.size(); ++i)
                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 190 ms 888 KB Output is correct
11 Correct 153 ms 888 KB Output is correct
12 Correct 26 ms 1016 KB Output is correct
13 Correct 162 ms 888 KB Output is correct
14 Correct 141 ms 892 KB Output is correct
15 Correct 342 ms 1192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1416 ms 27408 KB Output is correct
2 Correct 594 ms 27512 KB Output is correct
3 Correct 560 ms 27516 KB Output is correct
4 Correct 936 ms 27484 KB Output is correct
5 Correct 1113 ms 27512 KB Output is correct
6 Correct 1524 ms 27352 KB Output is correct
7 Correct 1664 ms 27524 KB Output is correct
8 Correct 509 ms 27384 KB Output is correct
9 Correct 544 ms 27384 KB Output is correct
10 Correct 596 ms 27384 KB Output is correct
11 Correct 876 ms 27512 KB Output is correct
12 Correct 1172 ms 27412 KB Output is correct
13 Correct 1186 ms 27408 KB Output is correct
14 Correct 1216 ms 27512 KB Output is correct
15 Correct 1195 ms 27384 KB Output is correct
16 Correct 1206 ms 27512 KB Output is correct
17 Correct 1647 ms 27384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 190 ms 888 KB Output is correct
11 Correct 153 ms 888 KB Output is correct
12 Correct 26 ms 1016 KB Output is correct
13 Correct 162 ms 888 KB Output is correct
14 Correct 141 ms 892 KB Output is correct
15 Correct 342 ms 1192 KB Output is correct
16 Correct 1416 ms 27408 KB Output is correct
17 Correct 594 ms 27512 KB Output is correct
18 Correct 560 ms 27516 KB Output is correct
19 Correct 936 ms 27484 KB Output is correct
20 Correct 1113 ms 27512 KB Output is correct
21 Correct 1524 ms 27352 KB Output is correct
22 Correct 1664 ms 27524 KB Output is correct
23 Correct 509 ms 27384 KB Output is correct
24 Correct 544 ms 27384 KB Output is correct
25 Correct 596 ms 27384 KB Output is correct
26 Correct 876 ms 27512 KB Output is correct
27 Correct 1172 ms 27412 KB Output is correct
28 Correct 1186 ms 27408 KB Output is correct
29 Correct 1216 ms 27512 KB Output is correct
30 Correct 1195 ms 27384 KB Output is correct
31 Correct 1206 ms 27512 KB Output is correct
32 Correct 1647 ms 27384 KB Output is correct
33 Incorrect 1322 ms 30984 KB Output isn't correct
34 Halted 0 ms 0 KB -