답안 #388405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
388405 2021-04-11T11:24:36 Z morato 늑대인간 (IOI18_werewolf) C++17
100 / 100
1507 ms 224408 KB
#include <bits/stdc++.h>
#include "werewolf.h"

using namespace std;

const int MAXL = 20;
const int INF = 1e9;
const int MAXN = 4e5 + 5;

class FenwickTree
{
	public:

		FenwickTree() {
			memset(bit, 0, sizeof bit);
		}

		void update(int x, int val) {
			for (; x < MAXN; x += x & -x)
				bit[x] += val;
		}

		int query(int x) {
			int ans = 0;
			for (; x > 0; x -= x & -x) 
				ans += bit[x];
			return ans;
		}

	private:

		int bit[MAXN];

} bit;

class KRT
{
	public:

		KRT(int _n = 0): n(_n) {
			curTimer = 0;
			for (int i = 1; i <= n; i++) {
				p[i] = i;
			}
		}

		void build(int _n) {
			n = _n;
			for (int i = 1; i <= n; i++) {
				p[i] = i;
			}
		}

		int getTin(int v) { return tin[v]; }
		int getTout(int v) { return tout[v]; }
		int getRoot(int v) { return p[v] == v ? v : p[v] = getRoot(p[v]); }

		void addEdge(int v, int u, int w, int flag) {
			v = getRoot(v);
			u = getRoot(u);
			if (u == v) return;
			++n;
			p[n] = p[u] = p[v] = n;
			adj[n].push_back(v);
			adj[n].push_back(u);
			anc[v][0] = anc[u][0] = {n, w};
            anc[n][0] = make_pair(n, (flag ? 0 : INF));
		}

		void dfs(int cur) {
			tin[cur] = ++curTimer;
			for (int viz : adj[cur]) {
				dfs(viz);
			}
			tout[cur] = curTimer;
		}

		int get(int a, int b, int flag) {
			return flag ? max(a, b) : min(a, b);
		}

		void buildKRT(int flag) {
			for (int i = n; i >= 1; i--) {
				for (int j = 1; j < MAXL; j++) {
					auto mid = anc[i][j - 1];
					auto target = anc[mid.first][j - 1];
					anc[i][j] = {target.first, get(mid.second, target.second, flag)};
				}
			}
			dfs(n);
		}

		bool compare(int limit, int value, int flag) {
			return flag ? limit >= value : limit <= value;
		}

		int goUp(int v, int limit, int flag) {
			for (int j = MAXL - 1; j >= 0; j--) {
				if (compare(limit, anc[v][j].second, flag)) {
					v = anc[v][j].first;
				}
			}
			return v;
		}

	private:

		vector<int> adj[MAXN];

		int n;
		int curLen;
		int curTimer;

		int p[MAXN];
		int tin[MAXN];
		int tout[MAXN];

		pair<int, int> anc[MAXN][MAXL];

} krtMin, krtMax;

struct Event
{
	int tipo, idx;
	int x, y;

	Event() {}

	Event(int _t, int _i, int _x, int _y): tipo(_t), idx(_i), x(_x), y(_y) {}

	bool operator <(const Event &e) const {
		if (x == e.x) return tipo < e.tipo;
		return x < e.x;
	}
};

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 n = N;
	krtMin.build(n);
	krtMax.build(n);
	vector<pair<int, pair<int, int>>> edges[2];
	int m = (int) X.size();
	for (int i = 0; i < m; i++) {
		X[i]++, Y[i]++;
		edges[0].push_back(make_pair(min(X[i], Y[i]), make_pair(X[i], Y[i])));
		edges[1].push_back(make_pair(max(X[i], Y[i]), make_pair(X[i], Y[i])));
	}
	for (int i : {0, 1}) {
		sort(edges[i].begin(), edges[i].end());
	}
    reverse(edges[0].begin(), edges[0].end());
	for (int i = 0; i < m; i++) {
		int w = edges[0][i].first;
		int v = edges[0][i].second.first;
		int u = edges[0][i].second.second;
		krtMin.addEdge(v, u, w, 0);
	}
	for (int i = 0; i < m; i++) {
		int w = edges[1][i].first;
		int v = edges[1][i].second.first;
		int u = edges[1][i].second.second;
		krtMax.addEdge(v, u, w, 1);
	}
	krtMin.buildKRT(0);
	krtMax.buildKRT(1);
	int q = (int) S.size();
	vector<Event> sweep;
	for (int i = 1; i <= n; i++) {
		int x = krtMax.getTin(i);
		int y = krtMin.getTin(i);
		sweep.emplace_back(0, -1, x, y);
	}
	vector<int> ans(q);
	for (int i = 0; i < q; i++) {
		S[i]++, E[i]++, L[i]++, R[i]++;
		// S[i] -> E[i]
		// human     avoids -> 1, 2, ..., L
		// werewolf  avoids -> R, R-1, ..., N
		int maxHuman = krtMin.goUp(S[i], L[i], 0);
		int maxWerewolf = krtMax.goUp(E[i], R[i], 1);
		int y1 = krtMin.getTin(maxHuman);
		int y2 = krtMin.getTout(maxHuman);
		int x1 = krtMax.getTin(maxWerewolf);
		int x2 = krtMax.getTout(maxWerewolf);
		sweep.emplace_back(1, i, x1 - 1, y2);
		sweep.emplace_back(1, i, x2, y1 - 1);
		sweep.emplace_back(2, i, x1 - 1, y1 - 1);
		sweep.emplace_back(2, i, x2, y2);
	}
	sort(sweep.begin(), sweep.end());
	for (auto event : sweep) {
		if (event.tipo == 0) {
			bit.update(event.y, 1);
		} else if (event.tipo == 1) {
			ans[event.idx] -= bit.query(event.y);
		} else {
			ans[event.idx] += bit.query(event.y);
		}
	}
	for (int& x : ans) {
		x = !!x;
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 145852 KB Output is correct
2 Correct 64 ms 145952 KB Output is correct
3 Correct 63 ms 145884 KB Output is correct
4 Correct 63 ms 145856 KB Output is correct
5 Correct 63 ms 145860 KB Output is correct
6 Correct 64 ms 145924 KB Output is correct
7 Correct 65 ms 145904 KB Output is correct
8 Correct 66 ms 145912 KB Output is correct
9 Correct 63 ms 145860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 145852 KB Output is correct
2 Correct 64 ms 145952 KB Output is correct
3 Correct 63 ms 145884 KB Output is correct
4 Correct 63 ms 145856 KB Output is correct
5 Correct 63 ms 145860 KB Output is correct
6 Correct 64 ms 145924 KB Output is correct
7 Correct 65 ms 145904 KB Output is correct
8 Correct 66 ms 145912 KB Output is correct
9 Correct 63 ms 145860 KB Output is correct
10 Correct 71 ms 147024 KB Output is correct
11 Correct 72 ms 146988 KB Output is correct
12 Correct 72 ms 146876 KB Output is correct
13 Correct 74 ms 147056 KB Output is correct
14 Correct 72 ms 147012 KB Output is correct
15 Correct 73 ms 147148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1070 ms 208780 KB Output is correct
2 Correct 1000 ms 213500 KB Output is correct
3 Correct 922 ms 210464 KB Output is correct
4 Correct 883 ms 209056 KB Output is correct
5 Correct 874 ms 209056 KB Output is correct
6 Correct 935 ms 208844 KB Output is correct
7 Correct 1000 ms 208728 KB Output is correct
8 Correct 946 ms 213552 KB Output is correct
9 Correct 771 ms 210500 KB Output is correct
10 Correct 715 ms 209168 KB Output is correct
11 Correct 761 ms 209052 KB Output is correct
12 Correct 864 ms 208920 KB Output is correct
13 Correct 1182 ms 213528 KB Output is correct
14 Correct 1189 ms 213492 KB Output is correct
15 Correct 1172 ms 213464 KB Output is correct
16 Correct 1198 ms 213448 KB Output is correct
17 Correct 998 ms 208800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 145852 KB Output is correct
2 Correct 64 ms 145952 KB Output is correct
3 Correct 63 ms 145884 KB Output is correct
4 Correct 63 ms 145856 KB Output is correct
5 Correct 63 ms 145860 KB Output is correct
6 Correct 64 ms 145924 KB Output is correct
7 Correct 65 ms 145904 KB Output is correct
8 Correct 66 ms 145912 KB Output is correct
9 Correct 63 ms 145860 KB Output is correct
10 Correct 71 ms 147024 KB Output is correct
11 Correct 72 ms 146988 KB Output is correct
12 Correct 72 ms 146876 KB Output is correct
13 Correct 74 ms 147056 KB Output is correct
14 Correct 72 ms 147012 KB Output is correct
15 Correct 73 ms 147148 KB Output is correct
16 Correct 1070 ms 208780 KB Output is correct
17 Correct 1000 ms 213500 KB Output is correct
18 Correct 922 ms 210464 KB Output is correct
19 Correct 883 ms 209056 KB Output is correct
20 Correct 874 ms 209056 KB Output is correct
21 Correct 935 ms 208844 KB Output is correct
22 Correct 1000 ms 208728 KB Output is correct
23 Correct 946 ms 213552 KB Output is correct
24 Correct 771 ms 210500 KB Output is correct
25 Correct 715 ms 209168 KB Output is correct
26 Correct 761 ms 209052 KB Output is correct
27 Correct 864 ms 208920 KB Output is correct
28 Correct 1182 ms 213528 KB Output is correct
29 Correct 1189 ms 213492 KB Output is correct
30 Correct 1172 ms 213464 KB Output is correct
31 Correct 1198 ms 213448 KB Output is correct
32 Correct 998 ms 208800 KB Output is correct
33 Correct 1409 ms 209992 KB Output is correct
34 Correct 580 ms 200940 KB Output is correct
35 Correct 1507 ms 213732 KB Output is correct
36 Correct 1265 ms 209644 KB Output is correct
37 Correct 1454 ms 212560 KB Output is correct
38 Correct 1394 ms 210516 KB Output is correct
39 Correct 1067 ms 218272 KB Output is correct
40 Correct 1386 ms 224128 KB Output is correct
41 Correct 1329 ms 211856 KB Output is correct
42 Correct 1156 ms 209744 KB Output is correct
43 Correct 1380 ms 221468 KB Output is correct
44 Correct 1416 ms 212564 KB Output is correct
45 Correct 1101 ms 218804 KB Output is correct
46 Correct 1008 ms 218264 KB Output is correct
47 Correct 1237 ms 213820 KB Output is correct
48 Correct 1199 ms 213680 KB Output is correct
49 Correct 1191 ms 213896 KB Output is correct
50 Correct 1166 ms 213464 KB Output is correct
51 Correct 1342 ms 224408 KB Output is correct
52 Correct 1406 ms 224376 KB Output is correct