답안 #159333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
159333 2019-10-22T11:13:35 Z iefnah06 늑대인간 (IOI18_werewolf) C++11
100 / 100
1322 ms 155152 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 200010;
int N, M, Q;

vector<int> adj[MAXN];
int par[MAXN];
const int MAXL = 20;
int anc[2][MAXL][MAXN];
vector<int> tr[MAXN];

void reset() {
	fill_n(par, N, -1);
	for (int i = 0; i < N; i++) tr[i].clear();
}
int getpar(int a) {
	return par[a] == -1 ? a : par[a] = getpar(par[a]);
}

int st[MAXN], ed[MAXN];
int ind;

void dfs(int cur = 0) {
	st[cur] = ind++;
	for (int nxt : tr[cur]) {
		dfs(nxt);
	}
	ed[cur] = ind;
}

const int MAXQ = 200010;
vector<int> queries[MAXN];
int ql[MAXQ], qr[MAXQ];
vector<int> ans;

struct seg {
	seg* ch[2] = {nullptr, nullptr};
};

void update(int p, seg* &n, int x = 0, int y = N) {
	if (!n) {
		n = new seg();
	}
	if (y - x > 1) {
		int z = (x + y) / 2;
		if (p < z) {
			update(p, n->ch[0], x, z);
		} else {
			update(p, n->ch[1], z, y);
		}
	}
}

int query(int l, int r, seg* n, int x = 0, int y = N) {
	if (!n) {
		return 0;
	}
	if (l <= x && y <= r) {
		return 1;
	} else {
		int z = (x + y) / 2;
		if (l < z && query(l, r, n->ch[0], x, z)) return 1;
		if (z < r && query(l, r, n->ch[1], z, y)) return 1;
		return 0;
	}
}

seg* merge(seg* a, seg* b) {
	if (!a) return b;
	if (!b) return a;
	a->ch[0] = merge(a->ch[0], b->ch[0]);
	a->ch[1] = merge(a->ch[1], b->ch[1]);
	return a;
}

seg* roots[MAXN];

void go(int cur = N - 1) {
	for (int nxt : tr[cur]) {
		go(nxt);
		roots[cur] = merge(roots[cur], roots[nxt]);
	}
	update(st[cur], roots[cur]);
	for (int q : queries[cur]) {
		ans[q] = query(ql[q], qr[q], roots[cur]); 
	}
}

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 = int(X.size());
	Q = int(S.size());

	for (int i = 0; i < M; i++) {
		adj[X[i]].push_back(Y[i]);
		adj[Y[i]].push_back(X[i]);
	}

	reset();
	for (int a = N - 1; a >= 0; a--) {
		for (int b : adj[a]) {
			if (b < a) continue;
			int t = getpar(b);
			if (a != t) {
				par[t] = a;
				anc[0][0][t] = a;
				tr[a].push_back(t);
			}
		}
	}
	anc[0][0][0] = -1;

	dfs();

	reset();
	for (int a = 0; a < N; a++) {
		for (int b : adj[a]) {
			if (b > a) continue;
			int t = getpar(b);
			if (a != t) {
				par[t] = a;
				anc[1][0][t] = a;
				tr[a].push_back(t);
			}
		}
	}
	anc[1][0][N - 1] = -1;

	for (int t = 0; t < 2; t++) {
		for (int l = 1; l < MAXL; l++) {
			for (int i = 0; i < N; i++) {
				if (anc[t][l - 1][i] == -1) {
					anc[t][l][i] = -1;
				} else {
					anc[t][l][i] = anc[t][l - 1][anc[t][l - 1][i]];
				}
			}
		}
	}
	
	for (int q = 0; q < Q; q++) {
		int a = S[q], b = E[q];
		for (int l = MAXL - 1; l >= 0; l--) {
			if (anc[0][l][a] != -1 && anc[0][l][a] >= L[q]) {
				a = anc[0][l][a];
			}
		}
		for (int l = MAXL - 1; l >= 0; l--) {
			if (anc[1][l][b] != -1 && anc[1][l][b] <= R[q]) {
				b = anc[1][l][b];
			}
		}

		ql[q] = st[a];
		qr[q] = ed[a];
		queries[b].push_back(q);
	}

	ans = vector<int>(Q);
	go();

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14712 KB Output is correct
2 Correct 14 ms 14712 KB Output is correct
3 Correct 15 ms 14712 KB Output is correct
4 Correct 15 ms 14712 KB Output is correct
5 Correct 15 ms 14712 KB Output is correct
6 Correct 15 ms 14712 KB Output is correct
7 Correct 14 ms 14712 KB Output is correct
8 Correct 14 ms 14712 KB Output is correct
9 Correct 14 ms 14712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14712 KB Output is correct
2 Correct 14 ms 14712 KB Output is correct
3 Correct 15 ms 14712 KB Output is correct
4 Correct 15 ms 14712 KB Output is correct
5 Correct 15 ms 14712 KB Output is correct
6 Correct 15 ms 14712 KB Output is correct
7 Correct 14 ms 14712 KB Output is correct
8 Correct 14 ms 14712 KB Output is correct
9 Correct 14 ms 14712 KB Output is correct
10 Correct 23 ms 16376 KB Output is correct
11 Correct 23 ms 16376 KB Output is correct
12 Correct 21 ms 16376 KB Output is correct
13 Correct 21 ms 16120 KB Output is correct
14 Correct 22 ms 15992 KB Output is correct
15 Correct 25 ms 16376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 815 ms 136572 KB Output is correct
2 Correct 958 ms 140024 KB Output is correct
3 Correct 894 ms 136260 KB Output is correct
4 Correct 877 ms 135376 KB Output is correct
5 Correct 835 ms 135224 KB Output is correct
6 Correct 844 ms 135848 KB Output is correct
7 Correct 767 ms 135480 KB Output is correct
8 Correct 941 ms 139956 KB Output is correct
9 Correct 698 ms 136284 KB Output is correct
10 Correct 603 ms 134672 KB Output is correct
11 Correct 613 ms 134888 KB Output is correct
12 Correct 732 ms 134708 KB Output is correct
13 Correct 871 ms 103432 KB Output is correct
14 Correct 856 ms 103240 KB Output is correct
15 Correct 901 ms 103372 KB Output is correct
16 Correct 953 ms 103340 KB Output is correct
17 Correct 796 ms 135332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14712 KB Output is correct
2 Correct 14 ms 14712 KB Output is correct
3 Correct 15 ms 14712 KB Output is correct
4 Correct 15 ms 14712 KB Output is correct
5 Correct 15 ms 14712 KB Output is correct
6 Correct 15 ms 14712 KB Output is correct
7 Correct 14 ms 14712 KB Output is correct
8 Correct 14 ms 14712 KB Output is correct
9 Correct 14 ms 14712 KB Output is correct
10 Correct 23 ms 16376 KB Output is correct
11 Correct 23 ms 16376 KB Output is correct
12 Correct 21 ms 16376 KB Output is correct
13 Correct 21 ms 16120 KB Output is correct
14 Correct 22 ms 15992 KB Output is correct
15 Correct 25 ms 16376 KB Output is correct
16 Correct 815 ms 136572 KB Output is correct
17 Correct 958 ms 140024 KB Output is correct
18 Correct 894 ms 136260 KB Output is correct
19 Correct 877 ms 135376 KB Output is correct
20 Correct 835 ms 135224 KB Output is correct
21 Correct 844 ms 135848 KB Output is correct
22 Correct 767 ms 135480 KB Output is correct
23 Correct 941 ms 139956 KB Output is correct
24 Correct 698 ms 136284 KB Output is correct
25 Correct 603 ms 134672 KB Output is correct
26 Correct 613 ms 134888 KB Output is correct
27 Correct 732 ms 134708 KB Output is correct
28 Correct 871 ms 103432 KB Output is correct
29 Correct 856 ms 103240 KB Output is correct
30 Correct 901 ms 103372 KB Output is correct
31 Correct 953 ms 103340 KB Output is correct
32 Correct 796 ms 135332 KB Output is correct
33 Correct 985 ms 147392 KB Output is correct
34 Correct 399 ms 49144 KB Output is correct
35 Correct 1052 ms 155152 KB Output is correct
36 Correct 915 ms 140596 KB Output is correct
37 Correct 1047 ms 153616 KB Output is correct
38 Correct 986 ms 143384 KB Output is correct
39 Correct 955 ms 108588 KB Output is correct
40 Correct 1067 ms 143196 KB Output is correct
41 Correct 834 ms 150264 KB Output is correct
42 Correct 809 ms 138536 KB Output is correct
43 Correct 1322 ms 154296 KB Output is correct
44 Correct 1012 ms 152164 KB Output is correct
45 Correct 803 ms 109796 KB Output is correct
46 Correct 793 ms 109420 KB Output is correct
47 Correct 912 ms 103480 KB Output is correct
48 Correct 929 ms 103248 KB Output is correct
49 Correct 967 ms 103568 KB Output is correct
50 Correct 897 ms 103264 KB Output is correct
51 Correct 935 ms 148600 KB Output is correct
52 Correct 949 ms 148936 KB Output is correct