Submission #207956

# Submission time Handle Problem Language Result Execution time Memory
207956 2020-03-09T14:27:09 Z red1108 Werewolf (IOI18_werewolf) C++17
100 / 100
1644 ms 174076 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> pii;

int n, m;
//1 이 N->1, 2가 1->N순서
vector<int> tree1[200010], tree2[200010], gr[200010];
pii rn1[200010], rn2[200010];
int mi[20][200010], ma[20][200010], par1[20][200010], par2[20][200010];
int anc[200010], ord;
int nh[200010], root[200010];
int findanc(int x){
	if(x==anc[x]) return x;
	return anc[x]=findanc(anc[x]);
}
void dfs1(int x, int par){
	//printf("dfs1(%d %d)\n", x, par);
	mi[0][x] = par;
	par1[0][x]=par;
	rn1[x].fi = ++ord;
	for(auto i:tree1[x]) if(i!=par){
		dfs1(i,x);
	}
	rn1[x].se = ord;
}
void dfs2(int x, int par){
	//printf("dfs2(%d %d)\n", x, par);
	ma[0][x] = par;
	par2[0][x]=par;
	rn2[x].fi = ++ord;
	nh[rn1[x].fi] = ord;
	for(auto i:tree2[x]) if(i!=par){
		dfs2(i,x);
	}
	rn2[x].se = ord;
}
struct Node{
	int sum, l, r;
	Node(){l=r=sum=0;}
};
vector<Node> pst;
void gang(int bef, int cur, int l, int r, int i){
	pst[cur] = pst[bef];
	pst[cur].sum++;
	if(l==r) return ;
	int m = (l+r)/2;
	if(i<=m){
		pst[cur].l = pst.size();
		pst.push_back(Node());
		gang(pst[bef].l, pst[cur].l, l, m, i);
	}
	else{
		pst[cur].r = pst.size();
		pst.push_back(Node());
		gang(pst[bef].r,pst[cur].r, m+1, r, i);
	}
}
int get(int bef, int cur, int l, int r, int s, int e){
	if(r<s||e<l||s>e) return 0;
	if(s<=l&&r<=e) return pst[cur].sum - pst[bef].sum;
	return get(pst[bef].l, pst[cur].l, l, (l+r)/2, s, e)
			+ get(pst[bef].r, pst[cur].r, (l+r)/2+1, r, s, e);
}
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R) {
	n = N;
	m = X.size();
	for(int i=0;i<m;i++){
		int a = X[i], b =  Y[i]; a++;b++;
		gr[a].push_back(b);
		gr[b].push_back(a);
	}
	for(int i=1;i<=n;i++) anc[i]=i;
	for(int i=n;i>=1;i--){
		for(auto j:gr[i]){
			if(j<i) continue;
			int a = findanc(j), b = findanc(i);
			if(a==b) continue;
			anc[a]=b;
			tree1[a].push_back(b);
			tree1[b].push_back(a);
		}
	}
	for(int i=1;i<=n;i++) anc[i]=i;
	for(int i=1;i<=n;i++){
		for(auto j:gr[i]){
			if(j>i) continue;
			int a = findanc(j), b = findanc(i);
			if(a==b) continue;
			anc[a]=b;
			tree2[a].push_back(b);
			tree2[b].push_back(a);
		}
	}
	ord=0;dfs1(1, 0);
	ord=0;dfs2(n, 0);
	ma[0][n] = 2*n;
	//cout<<"phase1"<<endl;
	pst.push_back(Node());
	//for(int i=1;i<=n;i++) printf("nh[%d]=%d\n", i, nh[i]);
	for(int i=1;i<=n;i++){
		root[i]=pst.size();
		pst.push_back(Node());
		gang(root[i-1], root[i], 1, n, nh[i]);
	}
	//cout<<"phase2"<<endl;
	for(int i=1;i<20;i++){
		for(int j=1;j<=n;j++){
			par1[i][j] = par1[i-1][par1[i-1][j]];
			par2[i][j] = par2[i-1][par2[i-1][j]];
			mi[i][j] = min(mi[i-1][j], mi[i-1][par1[i-1][j]]);
			ma[i][j] = max(ma[i-1][j], ma[i-1][par2[i-1][j]]);
		}
	}
	//for(int i=1;i<=n;i++) printf("i=%d (%d %d)\n", i, rn1[i].fi, rn2[i].fi);
	int Q = S.size();
	std::vector<int> A(Q);
	for (int i = 0; i < Q; ++i) {
		S[i]++;E[i]++;L[i]++;R[i]++;
		int x = S[i], y = E[i];
		for(int j=19;j>=0;j--){
			if(mi[j][x]>=L[i]) x = par1[j][x];
			if(ma[j][y]<=R[i]) y = par2[j][y];
		}
		//printf("x = %d y = %d\n", x, y);
		int sum = get(root[rn1[x].fi-1], root[rn1[x].se], 1, n, rn2[y].fi, rn2[y].se);
		if(sum) A[i]=1;
		else A[i]=0;
	}
	return A;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14968 KB Output is correct
2 Correct 14 ms 14968 KB Output is correct
3 Correct 14 ms 14840 KB Output is correct
4 Correct 14 ms 14840 KB Output is correct
5 Correct 14 ms 14968 KB Output is correct
6 Correct 14 ms 14968 KB Output is correct
7 Correct 14 ms 14968 KB Output is correct
8 Correct 14 ms 14968 KB Output is correct
9 Correct 14 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14968 KB Output is correct
2 Correct 14 ms 14968 KB Output is correct
3 Correct 14 ms 14840 KB Output is correct
4 Correct 14 ms 14840 KB Output is correct
5 Correct 14 ms 14968 KB Output is correct
6 Correct 14 ms 14968 KB Output is correct
7 Correct 14 ms 14968 KB Output is correct
8 Correct 14 ms 14968 KB Output is correct
9 Correct 14 ms 14968 KB Output is correct
10 Correct 22 ms 17132 KB Output is correct
11 Correct 22 ms 17136 KB Output is correct
12 Correct 22 ms 17008 KB Output is correct
13 Correct 22 ms 17132 KB Output is correct
14 Correct 21 ms 17132 KB Output is correct
15 Correct 24 ms 17136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1044 ms 155428 KB Output is correct
2 Correct 1267 ms 167316 KB Output is correct
3 Correct 1259 ms 165008 KB Output is correct
4 Correct 1158 ms 164048 KB Output is correct
5 Correct 1074 ms 164032 KB Output is correct
6 Correct 1054 ms 163964 KB Output is correct
7 Correct 944 ms 163848 KB Output is correct
8 Correct 1226 ms 167420 KB Output is correct
9 Correct 908 ms 164988 KB Output is correct
10 Correct 685 ms 164092 KB Output is correct
11 Correct 730 ms 163940 KB Output is correct
12 Correct 791 ms 163836 KB Output is correct
13 Correct 1225 ms 168528 KB Output is correct
14 Correct 1250 ms 168536 KB Output is correct
15 Correct 1186 ms 168700 KB Output is correct
16 Correct 1179 ms 168700 KB Output is correct
17 Correct 981 ms 163836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14968 KB Output is correct
2 Correct 14 ms 14968 KB Output is correct
3 Correct 14 ms 14840 KB Output is correct
4 Correct 14 ms 14840 KB Output is correct
5 Correct 14 ms 14968 KB Output is correct
6 Correct 14 ms 14968 KB Output is correct
7 Correct 14 ms 14968 KB Output is correct
8 Correct 14 ms 14968 KB Output is correct
9 Correct 14 ms 14968 KB Output is correct
10 Correct 22 ms 17132 KB Output is correct
11 Correct 22 ms 17136 KB Output is correct
12 Correct 22 ms 17008 KB Output is correct
13 Correct 22 ms 17132 KB Output is correct
14 Correct 21 ms 17132 KB Output is correct
15 Correct 24 ms 17136 KB Output is correct
16 Correct 1044 ms 155428 KB Output is correct
17 Correct 1267 ms 167316 KB Output is correct
18 Correct 1259 ms 165008 KB Output is correct
19 Correct 1158 ms 164048 KB Output is correct
20 Correct 1074 ms 164032 KB Output is correct
21 Correct 1054 ms 163964 KB Output is correct
22 Correct 944 ms 163848 KB Output is correct
23 Correct 1226 ms 167420 KB Output is correct
24 Correct 908 ms 164988 KB Output is correct
25 Correct 685 ms 164092 KB Output is correct
26 Correct 730 ms 163940 KB Output is correct
27 Correct 791 ms 163836 KB Output is correct
28 Correct 1225 ms 168528 KB Output is correct
29 Correct 1250 ms 168536 KB Output is correct
30 Correct 1186 ms 168700 KB Output is correct
31 Correct 1179 ms 168700 KB Output is correct
32 Correct 981 ms 163836 KB Output is correct
33 Correct 1308 ms 165224 KB Output is correct
34 Correct 418 ms 46964 KB Output is correct
35 Correct 1538 ms 167676 KB Output is correct
36 Correct 1231 ms 164604 KB Output is correct
37 Correct 1447 ms 166780 KB Output is correct
38 Correct 1315 ms 165244 KB Output is correct
39 Correct 1365 ms 173948 KB Output is correct
40 Correct 1159 ms 173308 KB Output is correct
41 Correct 1209 ms 166440 KB Output is correct
42 Correct 764 ms 164476 KB Output is correct
43 Correct 1644 ms 171388 KB Output is correct
44 Correct 1419 ms 166780 KB Output is correct
45 Correct 1114 ms 174076 KB Output is correct
46 Correct 1196 ms 173740 KB Output is correct
47 Correct 1181 ms 168828 KB Output is correct
48 Correct 1154 ms 168700 KB Output is correct
49 Correct 1172 ms 168956 KB Output is correct
50 Correct 1149 ms 168704 KB Output is correct
51 Correct 1053 ms 173948 KB Output is correct
52 Correct 1014 ms 173820 KB Output is correct