답안 #381672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381672 2021-03-25T14:56:36 Z LucaDantas 늑대인간 (IOI18_werewolf) C++17
100 / 100
1289 ms 241116 KB
#include "werewolf.h"
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;

using pii = pair<int,int>;

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ff first
#define ss second

constexpr int maxn = 2e6+10, logn = 22;

int n;

struct DSU
{
	int pai[maxn];
	DSU() { for(int i = 0; i < maxn; i++) pai[i] = i; }
	void init() { for(int i = 0; i < maxn; i++) pai[i] = i; }
	int find(int x) { return pai[x]==x?x:pai[x]=find(pai[x]); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if(a == b) return 0;
		pai[b] = a;
		return 1;
	}
} dsu;

struct KRT
{
	vector<int> g[maxn]; // graph pointing down
	int tab[maxn][logn], val[maxn], head = 0, t = 1;

	int in[maxn], out[maxn];

	DSU aux;

	void addEdge(int from, int to, int weight) {
		head = from;
		val[from] = weight;

		to = aux.find(to); // gets the head of the component
		aux.join(from, to); // make him my son

		tab[to][0] = from;
		g[from].pb(to);
	}

	void dfs(int u) {
		if(u <= n) return (void)(in[u] = out[u] = t++);

		in[u] = t;
		for(int v : g[u])
			dfs(v);
		out[u] = t-1;
	}

	void build() {
		dfs(head);

		for(int l = 1; l < logn; l++)
			for(int i = 1; i <= head; i++)
				tab[i][l] = tab[tab[i][l-1]][l-1];
	}

	int get(int u, int v, int k) {
		auto comp = [&](int a, int b) { return k?a>=b:a<=b; };
		for(int l = logn-1; l >= 0; l--) {
			if(tab[u][l] && comp(val[tab[u][l]], v))
				u = tab[u][l];
		}
		return u;
	}
} krt[2];

struct Event
{
	int t, x, y1, y2, id;
	bool operator<(Event e) {
		if(x == e.x) return t < e.t;
		return x < e.x;
	}
} events[maxn];

struct BIT
{
	int bit[maxn];
	void upd(int x, int v) {
		for(; x < maxn; x += x&-x)
			bit[x] += v;
	}
	int query(int x) {
		int ans = 0;
		for(; x > 0; x -= x&-x)
			ans += bit[x];
		return ans;
	}
	int get(int l, int r) { return query(r) - query(l-1); }
} bit;

vector<int> check_validity(int N, vector<int> X, vector<int> Y,
	vector<int> S, vector<int> E, vector<int> L, vector<int> R) {

	n = N;

	vector<pii> edges;
	int M = (int)X.size();
	for(int i = 0; i < M; i++) {
		if(X[i] > Y[i]) swap(X[i], Y[i]);
		++X[i], ++Y[i];

		edges.pb({X[i], Y[i]});
	}


	{
		sort(all(edges), [](pii a, pii b) {
			return a.first > b.first;
		});

		int cnt = N;
		for(int i = 0; i < M; i++) {
			int a = edges[i].ff, b = edges[i].ss;
			if(dsu.join(a, b)) {
				++cnt;
				krt[0].addEdge(cnt, a, a); // from, to, value of new node
				krt[0].addEdge(cnt, b, a);
			}
		}

		krt[0].build();
	}

	{
		dsu.init();

		// Sort by increasing big value
		sort(all(edges), [](pii a, pii b) {
			return a.ss < b.ss;
		});

		int cnt = N;
		for(int i = 0; i < M; i++) {
			int a = edges[i].ff, b = edges[i].ss;
			if(dsu.join(a, b)) {
				++cnt;
				krt[1].addEdge(cnt, a, b);
				krt[1].addEdge(cnt, b, b);
			}
		}

		krt[1].build();
	}

	int Q = S.size();

	vector<int> ans(Q);

	for(int i = 1; i <= N; i++)
		events[i] = {0, krt[0].in[i], krt[1].in[i], -1, -1};

	for(int i = 0; i < Q; i++) {
		if(S[i] < L[i] || E[i] > R[i]) ans[i] = -2*maxn;

		++S[i], ++E[i], ++L[i], ++R[i];

		int p1 = krt[0].get(S[i], L[i], 1);
		int p2 = krt[1].get(E[i], R[i], 0);

		events[N+2*i+1] = {-1, krt[0].in[p1], krt[1].in[p2], krt[1].out[p2], i},
		events[N+2*i+2] = {1, krt[0].out[p1], krt[1].in[p2], krt[1].out[p2], i};
	}

	sort(events+1, events+N+2*Q+1);

	for(int i = 1; i <= N+2*Q; i++) {
		Event e = events[i];
		if(e.t == 0) bit.upd(e.y1, 1);
		else ans[e.id] += e.t * bit.get(e.y1, e.y2);
	}

	for(int i = 0; i < Q; i++)
		ans[i] = ans[i]>0;
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 117868 KB Output is correct
2 Correct 72 ms 117868 KB Output is correct
3 Correct 74 ms 117868 KB Output is correct
4 Correct 73 ms 117868 KB Output is correct
5 Correct 73 ms 117868 KB Output is correct
6 Correct 75 ms 117888 KB Output is correct
7 Correct 74 ms 117868 KB Output is correct
8 Correct 79 ms 117868 KB Output is correct
9 Correct 72 ms 117868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 117868 KB Output is correct
2 Correct 72 ms 117868 KB Output is correct
3 Correct 74 ms 117868 KB Output is correct
4 Correct 73 ms 117868 KB Output is correct
5 Correct 73 ms 117868 KB Output is correct
6 Correct 75 ms 117888 KB Output is correct
7 Correct 74 ms 117868 KB Output is correct
8 Correct 79 ms 117868 KB Output is correct
9 Correct 72 ms 117868 KB Output is correct
10 Correct 80 ms 119660 KB Output is correct
11 Correct 81 ms 119660 KB Output is correct
12 Correct 79 ms 119532 KB Output is correct
13 Correct 80 ms 119788 KB Output is correct
14 Correct 79 ms 119660 KB Output is correct
15 Correct 82 ms 119660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 985 ms 231440 KB Output is correct
2 Correct 1097 ms 236304 KB Output is correct
3 Correct 986 ms 233232 KB Output is correct
4 Correct 890 ms 231900 KB Output is correct
5 Correct 903 ms 231644 KB Output is correct
6 Correct 967 ms 231612 KB Output is correct
7 Correct 920 ms 231516 KB Output is correct
8 Correct 1058 ms 236252 KB Output is correct
9 Correct 853 ms 233180 KB Output is correct
10 Correct 724 ms 231772 KB Output is correct
11 Correct 738 ms 231772 KB Output is correct
12 Correct 792 ms 231644 KB Output is correct
13 Correct 1179 ms 236040 KB Output is correct
14 Correct 1160 ms 236048 KB Output is correct
15 Correct 1176 ms 236124 KB Output is correct
16 Correct 1179 ms 236100 KB Output is correct
17 Correct 922 ms 231532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 117868 KB Output is correct
2 Correct 72 ms 117868 KB Output is correct
3 Correct 74 ms 117868 KB Output is correct
4 Correct 73 ms 117868 KB Output is correct
5 Correct 73 ms 117868 KB Output is correct
6 Correct 75 ms 117888 KB Output is correct
7 Correct 74 ms 117868 KB Output is correct
8 Correct 79 ms 117868 KB Output is correct
9 Correct 72 ms 117868 KB Output is correct
10 Correct 80 ms 119660 KB Output is correct
11 Correct 81 ms 119660 KB Output is correct
12 Correct 79 ms 119532 KB Output is correct
13 Correct 80 ms 119788 KB Output is correct
14 Correct 79 ms 119660 KB Output is correct
15 Correct 82 ms 119660 KB Output is correct
16 Correct 985 ms 231440 KB Output is correct
17 Correct 1097 ms 236304 KB Output is correct
18 Correct 986 ms 233232 KB Output is correct
19 Correct 890 ms 231900 KB Output is correct
20 Correct 903 ms 231644 KB Output is correct
21 Correct 967 ms 231612 KB Output is correct
22 Correct 920 ms 231516 KB Output is correct
23 Correct 1058 ms 236252 KB Output is correct
24 Correct 853 ms 233180 KB Output is correct
25 Correct 724 ms 231772 KB Output is correct
26 Correct 738 ms 231772 KB Output is correct
27 Correct 792 ms 231644 KB Output is correct
28 Correct 1179 ms 236040 KB Output is correct
29 Correct 1160 ms 236048 KB Output is correct
30 Correct 1176 ms 236124 KB Output is correct
31 Correct 1179 ms 236100 KB Output is correct
32 Correct 922 ms 231532 KB Output is correct
33 Correct 1156 ms 232620 KB Output is correct
34 Correct 486 ms 143060 KB Output is correct
35 Correct 1245 ms 235860 KB Output is correct
36 Correct 1076 ms 232156 KB Output is correct
37 Correct 1216 ms 234972 KB Output is correct
38 Correct 1125 ms 232968 KB Output is correct
39 Correct 1088 ms 240860 KB Output is correct
40 Correct 1201 ms 238676 KB Output is correct
41 Correct 1016 ms 234380 KB Output is correct
42 Correct 839 ms 232284 KB Output is correct
43 Correct 1289 ms 238804 KB Output is correct
44 Correct 1190 ms 234940 KB Output is correct
45 Correct 938 ms 241116 KB Output is correct
46 Correct 965 ms 240860 KB Output is correct
47 Correct 1182 ms 236292 KB Output is correct
48 Correct 1170 ms 236124 KB Output is correct
49 Correct 1206 ms 236252 KB Output is correct
50 Correct 1187 ms 236124 KB Output is correct
51 Correct 1120 ms 238804 KB Output is correct
52 Correct 1141 ms 238804 KB Output is correct