Submission #467611

# Submission time Handle Problem Language Result Execution time Memory
467611 2021-08-23T19:54:15 Z rainboy Keys (IOI21_keys) C++17
100 / 100
1330 ms 215220 KB
#include <vector>
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef vector<int> vi;

const int N = 300000, M = 300000, L = 19, N_ = 1 + (N + M * 2) * (L + 1), INF = 0x3f3f3f3f;	/* L = ceil(log2(N)) */

int ii[M], jj[M], nxt[M * 2];
int ll[N_], rr[N_], head[N_], tail[N_], _; char available[N_], has[N_];
int n;

void pul_leaf(int t) {
	has[t] = available[t] && head[t] != -1;
}

void pul(int t) {
	has[t] = has[ll[t]] || has[rr[t]];
}

void push(int t, int h) {
	if (head[t] == -1)
		head[t] = tail[t] = h;
	else
		nxt[tail[t]] = h, tail[t] = h;
}

int pop(int t) {
	int h = head[t];

	if ((head[t] = nxt[h]) == -1)
		tail[t] = -1;
	return h >> 1;
}

int update(int t, int l, int r, int c, int h) {
	if (t == 0)
		t = ++_, head[t] = tail[t] = -1;
	if (r - l == 1) {
		if (h != -1)
			push(t, h);
		else
			available[t] = 1;
		pul_leaf(t);
	} else {
		int m = (l + r) / 2;

		if (c < m)
			ll[t] = update(ll[t], l, m, c, h);
		else
			rr[t] = update(rr[t], m, r, c, h);
		pul(t);
	}
	return t;
}

int merge(int t1, int t2, int l, int r) {
	int m;

	if (t1 == 0)
		return t2;
	if (t2 == 0)
		return t1;
	if (r - l == 1) {
		available[t1] |= available[t2];
		if (head[t1] == -1)
			head[t1] = head[t2], tail[t1] = tail[t2];
		else if (head[t2] != -1)
			nxt[tail[t1]] = head[t2], tail[t1] = tail[t2];
		pul_leaf(t1);
	} else {
		m = (l + r) / 2;
		ll[t1] = merge(ll[t1], ll[t2], l, m), rr[t1] = merge(rr[t1], rr[t2], m, r);
		pul(t1);
	}
	return t1;
}

int find_(int i);

int query(int t, int l, int r, int i) {
	if (!has[t])
		return -1;
	if (r - l == 1) {
		while (head[t] != -1) {
			int h = pop(t), j = i ^ find_(ii[h]) ^ find_(jj[h]);

			if (j != i) {
				pul_leaf(t);
				return j;
			}
		}
		pul_leaf(t);
		return -1;
	} else {
		int m = (l + r) / 2, j;

		if ((j = query(ll[t], l, m, i)) != -1) {
			pul(t);
			return j;
		}
		if ((j = query(rr[t], m, r, i)) != -1) {
			pul(t);
			return j;
		}
		pul(t);
		return -1;
	}
}

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

int join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return 0;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
	return 1;
}

int ds_[N], tt[N];

int find_(int i) {
	return ds_[i] < 0 ? i : (ds_[i] = find_(ds_[i]));
}

void join_(int i, int j) {
	int tmp;

	i = find_(i);
	j = find_(j);
	if (i == j)
		return;
	if (ds_[i] > ds_[j])
		tmp = i, i = j, j = tmp;
	ds_[i] += ds_[j], ds_[j] = i, tt[i] = merge(tt[i], tt[j], 0, n);
}

int pp[N], qu[N], cnt;

void get(int i) {
	int j = query(tt[i], 0, n, i);

	if (j != -1) {
		pp[i] = j;
		if (!join(i, j))
			qu[cnt++] = i;
	} else
		pp[i] = -1;
}

vi find_reachable(vi aa, vi ii_, vi jj_, vi cc) {
	int m = ii_.size(), h, i, j, mn;
	vi ans(n = aa.size());

	for (h = 0; h < m; h++)
		ii[h] = ii_[h], jj[h] = jj_[h];
	for (i = 0; i < n; i++)
		tt[i] = update(tt[i], 0, n, aa[i], -1);
	for (h = 0; h < m * 2; h++)
		nxt[h] = -1;
	for (h = 0; h < m; h++) {
		i = ii[h], j = jj[h];
		tt[i] = update(tt[i], 0, n, cc[h], h << 1 | 0), tt[j] = update(tt[j], 0, n, cc[h], h << 1 | 1);
	}
	memset(ds, -1, n * sizeof *ds), memset(ds_, -1, n * sizeof *ds_);
	for (i = 0; i < n; i++)
		get(i);
	while (cnt--) {
		i = qu[cnt];
		for (j = find_(pp[i]); j != find_(i); j = find_(pp[j]))
			join_(i, j);
		get(find_(i));
	}
	mn = INF;
	for (i = 0; i < n; i++)
		if (ds_[i] < 0 && pp[i] == -1)
			mn = min(mn, -ds_[i]);
	for (i = 0; i < n; i++)
		ans[i] = pp[find_(i)] == -1 && -ds_[find_(i)] == mn;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 3 ms 1356 KB Output is correct
28 Correct 3 ms 1356 KB Output is correct
29 Correct 3 ms 1356 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 3 ms 1100 KB Output is correct
37 Correct 4 ms 1100 KB Output is correct
38 Correct 3 ms 1228 KB Output is correct
39 Correct 4 ms 1228 KB Output is correct
40 Correct 2 ms 716 KB Output is correct
41 Correct 2 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 728 ms 70612 KB Output is correct
11 Correct 748 ms 124448 KB Output is correct
12 Correct 125 ms 24700 KB Output is correct
13 Correct 799 ms 132520 KB Output is correct
14 Correct 471 ms 123716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 3 ms 1356 KB Output is correct
28 Correct 3 ms 1356 KB Output is correct
29 Correct 3 ms 1356 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 3 ms 1100 KB Output is correct
37 Correct 4 ms 1100 KB Output is correct
38 Correct 3 ms 1228 KB Output is correct
39 Correct 4 ms 1228 KB Output is correct
40 Correct 2 ms 716 KB Output is correct
41 Correct 2 ms 844 KB Output is correct
42 Correct 728 ms 70612 KB Output is correct
43 Correct 748 ms 124448 KB Output is correct
44 Correct 125 ms 24700 KB Output is correct
45 Correct 799 ms 132520 KB Output is correct
46 Correct 471 ms 123716 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 0 ms 332 KB Output is correct
50 Correct 454 ms 103032 KB Output is correct
51 Correct 493 ms 128600 KB Output is correct
52 Correct 510 ms 166724 KB Output is correct
53 Correct 518 ms 166724 KB Output is correct
54 Correct 500 ms 166724 KB Output is correct
55 Correct 867 ms 176636 KB Output is correct
56 Correct 678 ms 215220 KB Output is correct
57 Correct 511 ms 214972 KB Output is correct
58 Correct 760 ms 139752 KB Output is correct
59 Correct 1330 ms 201392 KB Output is correct
60 Correct 1217 ms 179908 KB Output is correct
61 Correct 1239 ms 180292 KB Output is correct
62 Correct 1287 ms 168160 KB Output is correct
63 Correct 428 ms 94532 KB Output is correct
64 Correct 7 ms 1868 KB Output is correct
65 Correct 7 ms 1968 KB Output is correct
66 Correct 1271 ms 168148 KB Output is correct
67 Correct 43 ms 11336 KB Output is correct
68 Correct 73 ms 19276 KB Output is correct
69 Correct 1319 ms 201288 KB Output is correct
70 Correct 151 ms 40004 KB Output is correct
71 Correct 489 ms 127840 KB Output is correct
72 Correct 1302 ms 201312 KB Output is correct
73 Correct 1293 ms 168072 KB Output is correct