Submission #636076

# Submission time Handle Problem Language Result Execution time Memory
636076 2022-08-27T23:23:43 Z pedroslrey Birthday gift (IZhO18_treearray) C++17
100 / 100
925 ms 80564 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 = pos[v].lower_bound(l);

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

			auto blca = pos_lca[v].lower_bound(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 15 ms 23732 KB n=5
2 Correct 13 ms 23804 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23728 KB n=100
6 Correct 13 ms 23800 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 14 ms 23732 KB n=100
10 Correct 13 ms 23776 KB n=100
11 Correct 12 ms 23744 KB n=100
12 Correct 13 ms 23844 KB n=100
13 Correct 13 ms 23764 KB n=100
14 Correct 13 ms 23788 KB n=100
15 Correct 14 ms 23844 KB n=100
16 Correct 14 ms 23768 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 15 ms 23760 KB n=100
20 Correct 13 ms 23732 KB n=100
21 Correct 15 ms 23844 KB n=100
22 Correct 12 ms 23780 KB n=100
23 Correct 13 ms 23720 KB n=100
24 Correct 13 ms 23784 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 13 ms 23848 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23732 KB n=5
2 Correct 13 ms 23804 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23728 KB n=100
6 Correct 13 ms 23800 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 14 ms 23732 KB n=100
10 Correct 13 ms 23776 KB n=100
11 Correct 12 ms 23744 KB n=100
12 Correct 13 ms 23844 KB n=100
13 Correct 13 ms 23764 KB n=100
14 Correct 13 ms 23788 KB n=100
15 Correct 14 ms 23844 KB n=100
16 Correct 14 ms 23768 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 15 ms 23760 KB n=100
20 Correct 13 ms 23732 KB n=100
21 Correct 15 ms 23844 KB n=100
22 Correct 12 ms 23780 KB n=100
23 Correct 13 ms 23720 KB n=100
24 Correct 13 ms 23784 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 13 ms 23848 KB n=100
28 Correct 13 ms 23892 KB n=500
29 Correct 13 ms 23892 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 14 ms 23892 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 13 ms 23936 KB n=500
38 Correct 13 ms 23856 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23876 KB n=500
41 Correct 14 ms 23936 KB n=500
42 Correct 13 ms 23900 KB n=500
43 Correct 16 ms 23824 KB n=500
44 Correct 14 ms 23928 KB n=500
45 Correct 17 ms 23856 KB n=500
46 Correct 14 ms 23920 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 13 ms 23924 KB n=500
49 Correct 12 ms 23932 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23936 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 14 ms 23896 KB n=500
55 Correct 14 ms 23796 KB n=278
56 Correct 15 ms 23944 KB n=500
57 Correct 13 ms 23828 KB n=500
58 Correct 15 ms 23932 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23732 KB n=5
2 Correct 13 ms 23804 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23728 KB n=100
6 Correct 13 ms 23800 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 14 ms 23732 KB n=100
10 Correct 13 ms 23776 KB n=100
11 Correct 12 ms 23744 KB n=100
12 Correct 13 ms 23844 KB n=100
13 Correct 13 ms 23764 KB n=100
14 Correct 13 ms 23788 KB n=100
15 Correct 14 ms 23844 KB n=100
16 Correct 14 ms 23768 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 15 ms 23760 KB n=100
20 Correct 13 ms 23732 KB n=100
21 Correct 15 ms 23844 KB n=100
22 Correct 12 ms 23780 KB n=100
23 Correct 13 ms 23720 KB n=100
24 Correct 13 ms 23784 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 13 ms 23848 KB n=100
28 Correct 13 ms 23892 KB n=500
29 Correct 13 ms 23892 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 14 ms 23892 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 13 ms 23936 KB n=500
38 Correct 13 ms 23856 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23876 KB n=500
41 Correct 14 ms 23936 KB n=500
42 Correct 13 ms 23900 KB n=500
43 Correct 16 ms 23824 KB n=500
44 Correct 14 ms 23928 KB n=500
45 Correct 17 ms 23856 KB n=500
46 Correct 14 ms 23920 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 13 ms 23924 KB n=500
49 Correct 12 ms 23932 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23936 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 14 ms 23896 KB n=500
55 Correct 14 ms 23796 KB n=278
56 Correct 15 ms 23944 KB n=500
57 Correct 13 ms 23828 KB n=500
58 Correct 15 ms 23932 KB n=500
59 Correct 16 ms 24232 KB n=2000
60 Correct 16 ms 24308 KB n=2000
61 Correct 16 ms 24276 KB n=2000
62 Correct 17 ms 24148 KB n=2000
63 Correct 16 ms 24156 KB n=2000
64 Correct 16 ms 24148 KB n=2000
65 Correct 16 ms 24176 KB n=2000
66 Correct 15 ms 24276 KB n=2000
67 Correct 16 ms 24148 KB n=2000
68 Correct 15 ms 24276 KB n=2000
69 Correct 15 ms 24232 KB n=2000
70 Correct 14 ms 24148 KB n=2000
71 Correct 19 ms 24256 KB n=2000
72 Correct 17 ms 24148 KB n=2000
73 Correct 13 ms 24148 KB n=2000
74 Correct 15 ms 24148 KB n=1844
75 Correct 14 ms 24148 KB n=2000
76 Correct 16 ms 24188 KB n=2000
77 Correct 15 ms 24148 KB n=2000
78 Correct 15 ms 24204 KB n=2000
79 Correct 15 ms 24148 KB n=2000
80 Correct 15 ms 24276 KB n=2000
81 Correct 15 ms 24236 KB n=2000
82 Correct 16 ms 24148 KB n=2000
83 Correct 15 ms 24244 KB n=2000
84 Correct 15 ms 24148 KB n=2000
85 Correct 16 ms 24260 KB n=2000
86 Correct 16 ms 24148 KB n=2000
87 Correct 16 ms 24224 KB n=2000
88 Correct 15 ms 24292 KB n=2000
89 Correct 15 ms 24216 KB n=2000
90 Correct 15 ms 24248 KB n=2000
91 Correct 15 ms 24148 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23732 KB n=5
2 Correct 13 ms 23804 KB n=100
3 Correct 13 ms 23764 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 13 ms 23728 KB n=100
6 Correct 13 ms 23800 KB n=100
7 Correct 13 ms 23764 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 14 ms 23732 KB n=100
10 Correct 13 ms 23776 KB n=100
11 Correct 12 ms 23744 KB n=100
12 Correct 13 ms 23844 KB n=100
13 Correct 13 ms 23764 KB n=100
14 Correct 13 ms 23788 KB n=100
15 Correct 14 ms 23844 KB n=100
16 Correct 14 ms 23768 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 13 ms 23764 KB n=100
19 Correct 15 ms 23760 KB n=100
20 Correct 13 ms 23732 KB n=100
21 Correct 15 ms 23844 KB n=100
22 Correct 12 ms 23780 KB n=100
23 Correct 13 ms 23720 KB n=100
24 Correct 13 ms 23784 KB n=100
25 Correct 13 ms 23764 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 13 ms 23848 KB n=100
28 Correct 13 ms 23892 KB n=500
29 Correct 13 ms 23892 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 14 ms 23892 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 13 ms 23936 KB n=500
38 Correct 13 ms 23856 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23876 KB n=500
41 Correct 14 ms 23936 KB n=500
42 Correct 13 ms 23900 KB n=500
43 Correct 16 ms 23824 KB n=500
44 Correct 14 ms 23928 KB n=500
45 Correct 17 ms 23856 KB n=500
46 Correct 14 ms 23920 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 13 ms 23924 KB n=500
49 Correct 12 ms 23932 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23936 KB n=500
52 Correct 14 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 14 ms 23896 KB n=500
55 Correct 14 ms 23796 KB n=278
56 Correct 15 ms 23944 KB n=500
57 Correct 13 ms 23828 KB n=500
58 Correct 15 ms 23932 KB n=500
59 Correct 16 ms 24232 KB n=2000
60 Correct 16 ms 24308 KB n=2000
61 Correct 16 ms 24276 KB n=2000
62 Correct 17 ms 24148 KB n=2000
63 Correct 16 ms 24156 KB n=2000
64 Correct 16 ms 24148 KB n=2000
65 Correct 16 ms 24176 KB n=2000
66 Correct 15 ms 24276 KB n=2000
67 Correct 16 ms 24148 KB n=2000
68 Correct 15 ms 24276 KB n=2000
69 Correct 15 ms 24232 KB n=2000
70 Correct 14 ms 24148 KB n=2000
71 Correct 19 ms 24256 KB n=2000
72 Correct 17 ms 24148 KB n=2000
73 Correct 13 ms 24148 KB n=2000
74 Correct 15 ms 24148 KB n=1844
75 Correct 14 ms 24148 KB n=2000
76 Correct 16 ms 24188 KB n=2000
77 Correct 15 ms 24148 KB n=2000
78 Correct 15 ms 24204 KB n=2000
79 Correct 15 ms 24148 KB n=2000
80 Correct 15 ms 24276 KB n=2000
81 Correct 15 ms 24236 KB n=2000
82 Correct 16 ms 24148 KB n=2000
83 Correct 15 ms 24244 KB n=2000
84 Correct 15 ms 24148 KB n=2000
85 Correct 16 ms 24260 KB n=2000
86 Correct 16 ms 24148 KB n=2000
87 Correct 16 ms 24224 KB n=2000
88 Correct 15 ms 24292 KB n=2000
89 Correct 15 ms 24216 KB n=2000
90 Correct 15 ms 24248 KB n=2000
91 Correct 15 ms 24148 KB n=2000
92 Correct 668 ms 70244 KB n=200000
93 Correct 863 ms 77272 KB n=200000
94 Correct 826 ms 79660 KB n=200000
95 Correct 639 ms 73592 KB n=200000
96 Correct 645 ms 73604 KB n=200000
97 Correct 847 ms 76768 KB n=200000
98 Correct 700 ms 73584 KB n=200000
99 Correct 801 ms 73880 KB n=200000
100 Correct 649 ms 73688 KB n=200000
101 Correct 856 ms 80564 KB n=200000
102 Correct 402 ms 74800 KB n=200000
103 Correct 377 ms 74728 KB n=200000
104 Correct 372 ms 74896 KB n=200000
105 Correct 400 ms 75148 KB n=200000
106 Correct 422 ms 75100 KB n=200000
107 Correct 390 ms 75116 KB n=200000
108 Correct 710 ms 73676 KB n=200000
109 Correct 739 ms 73736 KB n=200000
110 Correct 750 ms 73604 KB n=200000
111 Correct 656 ms 72996 KB n=200000
112 Correct 782 ms 79704 KB n=200000
113 Correct 924 ms 76644 KB n=200000
114 Correct 669 ms 73096 KB n=200000
115 Correct 925 ms 75260 KB n=200000
116 Correct 682 ms 73744 KB n=200000
117 Correct 848 ms 80016 KB n=200000
118 Correct 886 ms 75748 KB n=200000
119 Correct 605 ms 73664 KB n=200000
120 Correct 757 ms 79372 KB n=200000
121 Correct 757 ms 79408 KB n=200000
122 Correct 797 ms 79976 KB n=200000
123 Correct 425 ms 74980 KB n=200000
124 Correct 172 ms 38308 KB n=25264