Submission #636075

# Submission time Handle Problem Language Result Execution time Memory
636075 2022-08-27T23:20:06 Z pedroslrey Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 66252 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5 + 10;
const int MAXK = 19;

vector<int> graph[MAXN];
int dp[MAXN][MAXK];
int as[MAXN], prof[MAXN];
set<int> pos[MAXN], pos_lca[MAXN];

void dfs(int u, int p) {
	for (int v: graph[u]) {
		if (v == p) continue;

		prof[v] = prof[u] + 1;
		dp[v][0] = u;
		for (int k = 1; k < MAXK; ++k)
			dp[v][k] = dp[dp[v][k-1]][k-1];

		dfs(v, u);
	}
}

int lca(int a, int b) {
	if (prof[a] > prof[b]) swap(a, b);

	int d = prof[b] - prof[a];
	for (int k = 0; k < MAXK; ++k)
		if (d & (1 << k)) b = dp[b][k];

	if (a == b) return a;

	for (int k = MAXK - 1; k >= 0; --k)
		if (dp[a][k] != dp[b][k]) {
			a = dp[a][k];
			b = dp[b][k];
		}

	return dp[a][0];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, m, q;
	cin >> n >> m >> q;

	for (int i = 0; i < n-1; ++i) {
		int u, v;
		cin >> u >> v;

		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	dfs(1, 1);

	for (int i = 1; i <= m; ++i)
		cin >> as[i];

	for (int i = 1; i <= m; ++i) {
		pos[as[i]].insert(i);
		if (i < m) 
			pos_lca[lca(as[i], as[i+1])].insert(i);
	}

	for (int qq = 0; qq < q; ++qq) {
		int t;
		cin >> t;

		if (t == 1) {
			int i, v;
			cin >> i >> v;

			pos[as[i]].erase(i);
			if (i > 1) pos_lca[lca(as[i-1], as[i])].erase(i-1);
			if (i < m) pos_lca[lca(as[i], as[i+1])].erase(i);

			as[i] = v;

			pos[as[i]].insert(i);
			if (i > 1) pos_lca[lca(as[i-1], as[i])].insert(i-1);
			if (i < m) pos_lca[lca(as[i], as[i+1])].insert(i);
		}
		else {
			int l, r, v;
			cin >> l >> r >> v;
			auto b = lower_bound(pos[v].begin(), pos[v].end(), l);

			if (b != pos[v].end() && *b <= r) {
				cout << *b << " " << *b << "\n";
				continue;
			}

			auto blca = lower_bound(pos_lca[v].begin(), pos_lca[v].end(), l);

			if (blca != pos_lca[v].end() && *blca + 1 <= r) {
				cout << *blca << " " << *blca + 1 << "\n";
				continue;
			}
			cout << "-1 -1\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 15 ms 23844 KB n=100
3 Correct 13 ms 23724 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23764 KB n=100
6 Correct 15 ms 23764 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 13 ms 23844 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23752 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 13 ms 23764 KB n=100
13 Correct 13 ms 23780 KB n=100
14 Correct 12 ms 23764 KB n=100
15 Correct 12 ms 23764 KB n=100
16 Correct 12 ms 23836 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 14 ms 23892 KB n=100
19 Correct 13 ms 23764 KB n=100
20 Correct 12 ms 23804 KB n=100
21 Correct 14 ms 23824 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23844 KB n=100
24 Correct 14 ms 23792 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 12 ms 23828 KB n=12
27 Correct 14 ms 23788 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 15 ms 23844 KB n=100
3 Correct 13 ms 23724 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23764 KB n=100
6 Correct 15 ms 23764 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 13 ms 23844 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23752 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 13 ms 23764 KB n=100
13 Correct 13 ms 23780 KB n=100
14 Correct 12 ms 23764 KB n=100
15 Correct 12 ms 23764 KB n=100
16 Correct 12 ms 23836 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 14 ms 23892 KB n=100
19 Correct 13 ms 23764 KB n=100
20 Correct 12 ms 23804 KB n=100
21 Correct 14 ms 23824 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23844 KB n=100
24 Correct 14 ms 23792 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 12 ms 23828 KB n=12
27 Correct 14 ms 23788 KB n=100
28 Correct 15 ms 23848 KB n=500
29 Correct 13 ms 24020 KB n=500
30 Correct 13 ms 23888 KB n=500
31 Correct 13 ms 23932 KB n=500
32 Correct 15 ms 23928 KB n=500
33 Correct 13 ms 23928 KB n=500
34 Correct 16 ms 23840 KB n=500
35 Correct 16 ms 23872 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23864 KB n=500
38 Correct 13 ms 23836 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 14 ms 24020 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 16 ms 23928 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 23916 KB n=500
45 Correct 13 ms 23940 KB n=500
46 Correct 13 ms 23840 KB n=500
47 Correct 17 ms 23892 KB n=500
48 Correct 14 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23892 KB n=500
51 Correct 13 ms 23880 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 14 ms 23892 KB n=500
54 Correct 16 ms 23892 KB n=500
55 Correct 13 ms 23832 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 13 ms 23940 KB n=500
58 Correct 13 ms 23928 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 15 ms 23844 KB n=100
3 Correct 13 ms 23724 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23764 KB n=100
6 Correct 15 ms 23764 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 13 ms 23844 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23752 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 13 ms 23764 KB n=100
13 Correct 13 ms 23780 KB n=100
14 Correct 12 ms 23764 KB n=100
15 Correct 12 ms 23764 KB n=100
16 Correct 12 ms 23836 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 14 ms 23892 KB n=100
19 Correct 13 ms 23764 KB n=100
20 Correct 12 ms 23804 KB n=100
21 Correct 14 ms 23824 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23844 KB n=100
24 Correct 14 ms 23792 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 12 ms 23828 KB n=12
27 Correct 14 ms 23788 KB n=100
28 Correct 15 ms 23848 KB n=500
29 Correct 13 ms 24020 KB n=500
30 Correct 13 ms 23888 KB n=500
31 Correct 13 ms 23932 KB n=500
32 Correct 15 ms 23928 KB n=500
33 Correct 13 ms 23928 KB n=500
34 Correct 16 ms 23840 KB n=500
35 Correct 16 ms 23872 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23864 KB n=500
38 Correct 13 ms 23836 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 14 ms 24020 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 16 ms 23928 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 23916 KB n=500
45 Correct 13 ms 23940 KB n=500
46 Correct 13 ms 23840 KB n=500
47 Correct 17 ms 23892 KB n=500
48 Correct 14 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23892 KB n=500
51 Correct 13 ms 23880 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 14 ms 23892 KB n=500
54 Correct 16 ms 23892 KB n=500
55 Correct 13 ms 23832 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 13 ms 23940 KB n=500
58 Correct 13 ms 23928 KB n=500
59 Correct 32 ms 24276 KB n=2000
60 Correct 15 ms 24264 KB n=2000
61 Correct 16 ms 24224 KB n=2000
62 Correct 16 ms 24160 KB n=2000
63 Correct 44 ms 24148 KB n=2000
64 Correct 16 ms 24276 KB n=2000
65 Correct 39 ms 24248 KB n=2000
66 Correct 16 ms 24292 KB n=2000
67 Correct 26 ms 24148 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 15 ms 24148 KB n=2000
70 Correct 14 ms 24260 KB n=2000
71 Correct 15 ms 24244 KB n=2000
72 Correct 14 ms 24220 KB n=2000
73 Correct 15 ms 24148 KB n=2000
74 Correct 33 ms 24228 KB n=1844
75 Correct 15 ms 24148 KB n=2000
76 Correct 16 ms 24148 KB n=2000
77 Correct 16 ms 24252 KB n=2000
78 Correct 16 ms 24172 KB n=2000
79 Correct 25 ms 24256 KB n=2000
80 Correct 15 ms 24276 KB n=2000
81 Correct 16 ms 24160 KB n=2000
82 Correct 26 ms 24196 KB n=2000
83 Correct 15 ms 24292 KB n=2000
84 Correct 17 ms 24164 KB n=2000
85 Correct 15 ms 24276 KB n=2000
86 Correct 15 ms 24256 KB n=2000
87 Correct 18 ms 24204 KB n=2000
88 Correct 15 ms 24276 KB n=2000
89 Correct 15 ms 24276 KB n=2000
90 Correct 15 ms 24308 KB n=2000
91 Correct 15 ms 24148 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 15 ms 23844 KB n=100
3 Correct 13 ms 23724 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23764 KB n=100
6 Correct 15 ms 23764 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 13 ms 23844 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23752 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 13 ms 23764 KB n=100
13 Correct 13 ms 23780 KB n=100
14 Correct 12 ms 23764 KB n=100
15 Correct 12 ms 23764 KB n=100
16 Correct 12 ms 23836 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 14 ms 23892 KB n=100
19 Correct 13 ms 23764 KB n=100
20 Correct 12 ms 23804 KB n=100
21 Correct 14 ms 23824 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 13 ms 23844 KB n=100
24 Correct 14 ms 23792 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 12 ms 23828 KB n=12
27 Correct 14 ms 23788 KB n=100
28 Correct 15 ms 23848 KB n=500
29 Correct 13 ms 24020 KB n=500
30 Correct 13 ms 23888 KB n=500
31 Correct 13 ms 23932 KB n=500
32 Correct 15 ms 23928 KB n=500
33 Correct 13 ms 23928 KB n=500
34 Correct 16 ms 23840 KB n=500
35 Correct 16 ms 23872 KB n=500
36 Correct 13 ms 23892 KB n=500
37 Correct 13 ms 23864 KB n=500
38 Correct 13 ms 23836 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 14 ms 24020 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 16 ms 23928 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 14 ms 23916 KB n=500
45 Correct 13 ms 23940 KB n=500
46 Correct 13 ms 23840 KB n=500
47 Correct 17 ms 23892 KB n=500
48 Correct 14 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23892 KB n=500
51 Correct 13 ms 23880 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 14 ms 23892 KB n=500
54 Correct 16 ms 23892 KB n=500
55 Correct 13 ms 23832 KB n=278
56 Correct 13 ms 23892 KB n=500
57 Correct 13 ms 23940 KB n=500
58 Correct 13 ms 23928 KB n=500
59 Correct 32 ms 24276 KB n=2000
60 Correct 15 ms 24264 KB n=2000
61 Correct 16 ms 24224 KB n=2000
62 Correct 16 ms 24160 KB n=2000
63 Correct 44 ms 24148 KB n=2000
64 Correct 16 ms 24276 KB n=2000
65 Correct 39 ms 24248 KB n=2000
66 Correct 16 ms 24292 KB n=2000
67 Correct 26 ms 24148 KB n=2000
68 Correct 15 ms 24268 KB n=2000
69 Correct 15 ms 24148 KB n=2000
70 Correct 14 ms 24260 KB n=2000
71 Correct 15 ms 24244 KB n=2000
72 Correct 14 ms 24220 KB n=2000
73 Correct 15 ms 24148 KB n=2000
74 Correct 33 ms 24228 KB n=1844
75 Correct 15 ms 24148 KB n=2000
76 Correct 16 ms 24148 KB n=2000
77 Correct 16 ms 24252 KB n=2000
78 Correct 16 ms 24172 KB n=2000
79 Correct 25 ms 24256 KB n=2000
80 Correct 15 ms 24276 KB n=2000
81 Correct 16 ms 24160 KB n=2000
82 Correct 26 ms 24196 KB n=2000
83 Correct 15 ms 24292 KB n=2000
84 Correct 17 ms 24164 KB n=2000
85 Correct 15 ms 24276 KB n=2000
86 Correct 15 ms 24256 KB n=2000
87 Correct 18 ms 24204 KB n=2000
88 Correct 15 ms 24276 KB n=2000
89 Correct 15 ms 24276 KB n=2000
90 Correct 15 ms 24308 KB n=2000
91 Correct 15 ms 24148 KB n=2000
92 Execution timed out 4077 ms 66252 KB Time limit exceeded
93 Halted 0 ms 0 KB -