답안 #332551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332551 2020-12-02T20:45:33 Z pit4h 늑대인간 (IOI18_werewolf) C++14
100 / 100
1559 ms 117240 KB
#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+2, MAXM = 4e5+2, Log = 20;

vector<int> ans;
int Find(int x, vector<int>& f) {
	if(f[x] == x) return x;
	f[x] = Find(f[x], f);
	return f[x];
}
bool Union(int x, int y, vector<int>& f, vector<int>& cnt, vector<int>& mx, bool dir) {
	x = Find(x, f); y = Find(y, f);
	if(x==y) return false;
	if(cnt[x] < cnt[y]) swap(x, y);
	cnt[x] += cnt[y];
	cnt[y] = 0;
	f[y] = x;
	if(!dir) {
		mx[x] = max(mx[x], mx[y]);
	}
	else {
		mx[x] = min(mx[x], mx[y]);
	}
	return true;
}
struct Query {
	int l, r, id;
};
const int base = (1<<19);
int tree[2*base+1];
void ins(int x, int val) {
	x += base;
	while(x) {
		tree[x]+=val;
		x /= 2;
	}
}
int qry(int l, int r) {
	l+=base; r+=base;
	int res = tree[l]; if(l==r) return res;
	res += tree[r];
	while(l/2 != r/2) {
		if(l%2==0) res += tree[l+1];
		if(r%2==1) res += tree[r-1];
		l /= 2;
		r /= 2;
	}
	return res;
}
vector<int> *occ[MAXN];
struct Form {
	vector<vector<int>> g, anc;
	vector<vector<Query>> queries;
	vector<int> pre, range, subt;	
	int N, nr=0;
	Form *other;
	Form(int _N) {
		N = _N;
		g.resize(N); anc.resize(Log, vector<int>(N));
		pre.resize(N); range.resize(N); subt.resize(N);
		queries.resize(N);
	}
	void build_graph(vector<int>& X, vector<int>& Y, vector<int>& order) {
		int M = X.size();
		vector<int> f(N), cnt(N), mx(N); for(int i=0; i<N; ++i) f[i] = mx[i] = i, cnt[i]=1;
		vector<vector<int>> G(N);
		for(int i=0; i<M; ++i) {
			if( (X[i] < Y[i]) != (order[0] < order.back()) ) {
				swap(X[i], Y[i]);	
			}
			G[Y[i]].push_back(X[i]);
		}
		for(int i: order) {
			for(int j: G[i]) {
				int repj = mx[Find(j, f)];
				if(Union(i, j, f, cnt, mx, (order[0] > order.back()) )) {
					g[i].push_back(repj);
				}
			}
		}		
	}
	void build_anc() {
		for(int j=1; j<Log; ++j) {
			for(int i=0; i<N; ++i) {
				anc[j][i] = anc[j-1][anc[j-1][i]];
			}
		}
	}
	void dfs(int x, int par) {
		anc[0][x] = par;
		pre[x] = nr; nr++;
		range[x] = pre[x];
		subt[x] = 1;
		for(int i: g[x]) {
			dfs(i, x);
			range[x] = max(range[x], range[i]);
			subt[x] += subt[i];
		}		
		if(x==par) {
			build_anc();
		}
	}
	int lowest_ancestor(int x, int h) {
		for(int i=Log-1; i>=0; --i) {
			if(x==h) break;
			if(anc[i][x]==h || (x < h) == (anc[i][x] < h)) {
				x = anc[i][x];
			}
		}
		return x;
	}
	void solve(int x, int p, bool keep) {
		int big_child = -1;
		for(int i: g[x]) {
			if(big_child==-1 || subt[i] > subt[big_child]) {
				big_child = i;
			}
		}	
		for(int i: g[x]) {
			if(i!=big_child) {
				solve(i, x, 0);
			}
		}
		if(big_child==-1) {
			occ[x] = new vector<int>();
		}
		else {
			solve(big_child, x, 1);
			occ[x] = occ[big_child];
		}
		for(int i: g[x]) {
			if(i!=big_child) {
				for(int j: *occ[i]) {
					occ[x] -> push_back(j);
					ins(j, 1);
				}
			}
		}
		occ[x] -> push_back(other->pre[x]);
		ins(other->pre[x], 1);
		for(auto q: queries[x]) {
			ans[q.id] = (qry(q.l, q.r) > 0);
		}
		if(!keep) {
			for(int j: *occ[x]) {
				ins(j, -1);
			}
		}
	}
};

vector<int> check_validity(int N, vector<int> X, vector<int> Y,
                                vector<int> S, vector<int> E,
                                vector<int> L, vector<int> R) {
	swap(S, E); // == swap(human, wolf)
	int Q = S.size();
	ans.resize(Q);

	vector<int> order;
	for(int i=0; i<N; ++i) order.push_back(i);

	Form human(N), wolf(N);
	wolf.other = &human;

	human.build_graph(X, Y, order);
	reverse(order.begin(), order.end());
	wolf.build_graph(X, Y, order);
	
	human.dfs(N-1, N-1);
	wolf.dfs(0, 0);
	
	for(int i=0; i<Q; ++i) {
		S[i] = human.lowest_ancestor(S[i], R[i]);
		E[i] = wolf.lowest_ancestor(E[i], L[i]);
		if(S[i] > R[i] || E[i] < L[i]) continue;
		wolf.queries[E[i]].push_back({human.pre[S[i]], human.range[S[i]], i});
	}
	wolf.solve(0, 0, 0);

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 8 ms 1772 KB Output is correct
11 Correct 8 ms 1772 KB Output is correct
12 Correct 8 ms 1772 KB Output is correct
13 Correct 8 ms 2028 KB Output is correct
14 Correct 10 ms 2156 KB Output is correct
15 Correct 9 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1559 ms 92736 KB Output is correct
2 Correct 1271 ms 94700 KB Output is correct
3 Correct 1258 ms 91640 KB Output is correct
4 Correct 1323 ms 91640 KB Output is correct
5 Correct 1356 ms 92152 KB Output is correct
6 Correct 1484 ms 92784 KB Output is correct
7 Correct 1414 ms 92664 KB Output is correct
8 Correct 1000 ms 94712 KB Output is correct
9 Correct 636 ms 91756 KB Output is correct
10 Correct 613 ms 91384 KB Output is correct
11 Correct 685 ms 91640 KB Output is correct
12 Correct 694 ms 91756 KB Output is correct
13 Correct 1329 ms 107720 KB Output is correct
14 Correct 1297 ms 107896 KB Output is correct
15 Correct 1312 ms 107768 KB Output is correct
16 Correct 1284 ms 107984 KB Output is correct
17 Correct 1424 ms 92920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 8 ms 1772 KB Output is correct
11 Correct 8 ms 1772 KB Output is correct
12 Correct 8 ms 1772 KB Output is correct
13 Correct 8 ms 2028 KB Output is correct
14 Correct 10 ms 2156 KB Output is correct
15 Correct 9 ms 1900 KB Output is correct
16 Correct 1559 ms 92736 KB Output is correct
17 Correct 1271 ms 94700 KB Output is correct
18 Correct 1258 ms 91640 KB Output is correct
19 Correct 1323 ms 91640 KB Output is correct
20 Correct 1356 ms 92152 KB Output is correct
21 Correct 1484 ms 92784 KB Output is correct
22 Correct 1414 ms 92664 KB Output is correct
23 Correct 1000 ms 94712 KB Output is correct
24 Correct 636 ms 91756 KB Output is correct
25 Correct 613 ms 91384 KB Output is correct
26 Correct 685 ms 91640 KB Output is correct
27 Correct 694 ms 91756 KB Output is correct
28 Correct 1329 ms 107720 KB Output is correct
29 Correct 1297 ms 107896 KB Output is correct
30 Correct 1312 ms 107768 KB Output is correct
31 Correct 1284 ms 107984 KB Output is correct
32 Correct 1424 ms 92920 KB Output is correct
33 Correct 1516 ms 92212 KB Output is correct
34 Correct 395 ms 30552 KB Output is correct
35 Correct 1514 ms 105508 KB Output is correct
36 Correct 1486 ms 99664 KB Output is correct
37 Correct 1503 ms 103800 KB Output is correct
38 Correct 1492 ms 101112 KB Output is correct
39 Correct 1491 ms 117240 KB Output is correct
40 Correct 1139 ms 111480 KB Output is correct
41 Correct 810 ms 101880 KB Output is correct
42 Correct 663 ms 99832 KB Output is correct
43 Correct 1079 ms 113140 KB Output is correct
44 Correct 1058 ms 103160 KB Output is correct
45 Correct 809 ms 116172 KB Output is correct
46 Correct 826 ms 116856 KB Output is correct
47 Correct 1307 ms 116236 KB Output is correct
48 Correct 1330 ms 116216 KB Output is correct
49 Correct 1291 ms 116344 KB Output is correct
50 Correct 1296 ms 116216 KB Output is correct
51 Correct 953 ms 111096 KB Output is correct
52 Correct 958 ms 111096 KB Output is correct