Submission #881120

# Submission time Handle Problem Language Result Execution time Memory
881120 2023-11-30T15:55:15 Z Hovhannes1234 Birthday gift (IZhO18_treearray) C++17
100 / 100
728 ms 84228 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <unordered_set>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <fstream>
#include <random>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
vector <vector<int>> G;
const int N = 2e5 + 10;
int a[N];
int dp[N][20];
int depth[N];
set <int> f1[N];
set <int> f2[N];
void dfs(int v, int p) {
	depth[v] = depth[p] + 1;
	dp[v][0] = p;
	for (int j = 1; j < 20; j++) {
		dp[v][j] = dp[dp[v][j - 1]][j - 1];
	}
	for (auto it : G[v]) {
		if (it == p) continue;
		dfs(it, v);
	}
}
int find_lca(int a, int b) {
	if (depth[a] < depth[b]) swap(a, b);
	int k = depth[a] - depth[b];
	for (int i = 0; i < 20; i++) {
		if (k & (1 << i)) {
			a = dp[a][i];
		}
	}
	if (a == b) return a;
	for (int i = 19; i >= 0; i--) {
		if (dp[a][i] != dp[b][i]) {
			a = dp[a][i];
			b = dp[b][i];
		}
	}
	return dp[a][0];
}
int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	G.resize(n + 1);
	for (int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(1, 1);
	for (int i = 1; i <= m; i++) {
		cin >> a[i];
		f1[a[i]].insert(i);
	}
	for (int i = 1; i <= m - 1; i++) {
		f2[find_lca(a[i], a[i + 1])].insert(i);
	}
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int pos, val;
			cin >> pos >> val;
			f1[a[pos]].erase(pos);
			f1[val].insert(pos);
			if (pos != 1) {
				f2[find_lca(a[pos - 1], a[pos])].erase(pos - 1);
				f2[find_lca(a[pos - 1], val)].insert(pos - 1);
			}
			if (pos != n) {
				f2[find_lca(a[pos], a[pos + 1])].erase(pos);
				f2[find_lca(val, a[pos + 1])].insert(pos);
			}
			a[pos] = val;
		}
		else {
			int l, r, v;
			cin >> l >> r >> v;
			auto it = f1[v].lower_bound(l);
			if (it != f1[v].end() && *it <= r) {
				cout << *it << " " << *it << "\n";
				continue;
			}
			auto w = f2[v].lower_bound(l);
			if (w != f2[v].end() && *w < r) {
				cout << *w << " " << *w + 1 << "\n";
				continue;
			}
			cout << -1 << " " << -1 << "\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21172 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21160 KB n=100
6 Correct 4 ms 21336 KB n=100
7 Correct 4 ms 21084 KB n=100
8 Correct 4 ms 21084 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 4 ms 20928 KB n=100
14 Correct 4 ms 21080 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21156 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 5 ms 21340 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21084 KB n=100
23 Correct 4 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21080 KB n=12
27 Correct 4 ms 21084 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21172 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21160 KB n=100
6 Correct 4 ms 21336 KB n=100
7 Correct 4 ms 21084 KB n=100
8 Correct 4 ms 21084 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 4 ms 20928 KB n=100
14 Correct 4 ms 21080 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21156 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 5 ms 21340 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21084 KB n=100
23 Correct 4 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21080 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21084 KB n=500
29 Correct 5 ms 21120 KB n=500
30 Correct 6 ms 21596 KB n=500
31 Correct 5 ms 21084 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21084 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 4 ms 21336 KB n=500
36 Correct 4 ms 21084 KB n=500
37 Correct 4 ms 21084 KB n=500
38 Correct 4 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21084 KB n=500
41 Correct 5 ms 21084 KB n=500
42 Correct 5 ms 21084 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21080 KB n=500
45 Correct 5 ms 21084 KB n=500
46 Correct 5 ms 21164 KB n=500
47 Correct 4 ms 21084 KB n=500
48 Correct 4 ms 21084 KB n=500
49 Correct 4 ms 21080 KB n=500
50 Correct 4 ms 21084 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21080 KB n=500
54 Correct 4 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21080 KB n=500
57 Correct 4 ms 21080 KB n=500
58 Correct 5 ms 21124 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21172 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21160 KB n=100
6 Correct 4 ms 21336 KB n=100
7 Correct 4 ms 21084 KB n=100
8 Correct 4 ms 21084 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 4 ms 20928 KB n=100
14 Correct 4 ms 21080 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21156 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 5 ms 21340 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21084 KB n=100
23 Correct 4 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21080 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21084 KB n=500
29 Correct 5 ms 21120 KB n=500
30 Correct 6 ms 21596 KB n=500
31 Correct 5 ms 21084 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21084 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 4 ms 21336 KB n=500
36 Correct 4 ms 21084 KB n=500
37 Correct 4 ms 21084 KB n=500
38 Correct 4 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21084 KB n=500
41 Correct 5 ms 21084 KB n=500
42 Correct 5 ms 21084 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21080 KB n=500
45 Correct 5 ms 21084 KB n=500
46 Correct 5 ms 21164 KB n=500
47 Correct 4 ms 21084 KB n=500
48 Correct 4 ms 21084 KB n=500
49 Correct 4 ms 21080 KB n=500
50 Correct 4 ms 21084 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21080 KB n=500
54 Correct 4 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21080 KB n=500
57 Correct 4 ms 21080 KB n=500
58 Correct 5 ms 21124 KB n=500
59 Correct 6 ms 21340 KB n=2000
60 Correct 7 ms 21592 KB n=2000
61 Correct 8 ms 21584 KB n=2000
62 Correct 7 ms 21340 KB n=2000
63 Correct 7 ms 21340 KB n=2000
64 Correct 7 ms 21424 KB n=2000
65 Correct 6 ms 21340 KB n=2000
66 Correct 7 ms 21424 KB n=2000
67 Correct 8 ms 21340 KB n=2000
68 Correct 7 ms 21340 KB n=2000
69 Correct 6 ms 21340 KB n=2000
70 Correct 6 ms 21420 KB n=2000
71 Correct 6 ms 21340 KB n=2000
72 Correct 6 ms 21380 KB n=2000
73 Correct 6 ms 21340 KB n=2000
74 Correct 6 ms 21336 KB n=1844
75 Correct 6 ms 21336 KB n=2000
76 Correct 6 ms 21340 KB n=2000
77 Correct 6 ms 21340 KB n=2000
78 Correct 7 ms 21336 KB n=2000
79 Correct 6 ms 21340 KB n=2000
80 Correct 6 ms 21592 KB n=2000
81 Correct 6 ms 21336 KB n=2000
82 Correct 6 ms 21340 KB n=2000
83 Correct 7 ms 21336 KB n=2000
84 Correct 7 ms 21340 KB n=2000
85 Correct 6 ms 21340 KB n=2000
86 Correct 6 ms 21548 KB n=2000
87 Correct 6 ms 21340 KB n=2000
88 Correct 6 ms 21596 KB n=2000
89 Correct 8 ms 21676 KB n=2000
90 Correct 6 ms 21576 KB n=2000
91 Correct 6 ms 21340 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21172 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21160 KB n=100
6 Correct 4 ms 21336 KB n=100
7 Correct 4 ms 21084 KB n=100
8 Correct 4 ms 21084 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 4 ms 20928 KB n=100
14 Correct 4 ms 21080 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21156 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 5 ms 21340 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21084 KB n=100
23 Correct 4 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21080 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21084 KB n=500
29 Correct 5 ms 21120 KB n=500
30 Correct 6 ms 21596 KB n=500
31 Correct 5 ms 21084 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21084 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 4 ms 21336 KB n=500
36 Correct 4 ms 21084 KB n=500
37 Correct 4 ms 21084 KB n=500
38 Correct 4 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21084 KB n=500
41 Correct 5 ms 21084 KB n=500
42 Correct 5 ms 21084 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21080 KB n=500
45 Correct 5 ms 21084 KB n=500
46 Correct 5 ms 21164 KB n=500
47 Correct 4 ms 21084 KB n=500
48 Correct 4 ms 21084 KB n=500
49 Correct 4 ms 21080 KB n=500
50 Correct 4 ms 21084 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21080 KB n=500
54 Correct 4 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21080 KB n=500
57 Correct 4 ms 21080 KB n=500
58 Correct 5 ms 21124 KB n=500
59 Correct 6 ms 21340 KB n=2000
60 Correct 7 ms 21592 KB n=2000
61 Correct 8 ms 21584 KB n=2000
62 Correct 7 ms 21340 KB n=2000
63 Correct 7 ms 21340 KB n=2000
64 Correct 7 ms 21424 KB n=2000
65 Correct 6 ms 21340 KB n=2000
66 Correct 7 ms 21424 KB n=2000
67 Correct 8 ms 21340 KB n=2000
68 Correct 7 ms 21340 KB n=2000
69 Correct 6 ms 21340 KB n=2000
70 Correct 6 ms 21420 KB n=2000
71 Correct 6 ms 21340 KB n=2000
72 Correct 6 ms 21380 KB n=2000
73 Correct 6 ms 21340 KB n=2000
74 Correct 6 ms 21336 KB n=1844
75 Correct 6 ms 21336 KB n=2000
76 Correct 6 ms 21340 KB n=2000
77 Correct 6 ms 21340 KB n=2000
78 Correct 7 ms 21336 KB n=2000
79 Correct 6 ms 21340 KB n=2000
80 Correct 6 ms 21592 KB n=2000
81 Correct 6 ms 21336 KB n=2000
82 Correct 6 ms 21340 KB n=2000
83 Correct 7 ms 21336 KB n=2000
84 Correct 7 ms 21340 KB n=2000
85 Correct 6 ms 21340 KB n=2000
86 Correct 6 ms 21548 KB n=2000
87 Correct 6 ms 21340 KB n=2000
88 Correct 6 ms 21596 KB n=2000
89 Correct 8 ms 21676 KB n=2000
90 Correct 6 ms 21576 KB n=2000
91 Correct 6 ms 21340 KB n=2000
92 Correct 461 ms 74672 KB n=200000
93 Correct 667 ms 79444 KB n=200000
94 Correct 692 ms 83096 KB n=200000
95 Correct 466 ms 74436 KB n=200000
96 Correct 450 ms 74436 KB n=200000
97 Correct 670 ms 78684 KB n=200000
98 Correct 451 ms 74604 KB n=200000
99 Correct 566 ms 74908 KB n=200000
100 Correct 455 ms 74724 KB n=200000
101 Correct 662 ms 84228 KB n=200000
102 Correct 293 ms 75628 KB n=200000
103 Correct 281 ms 75760 KB n=200000
104 Correct 279 ms 75856 KB n=200000
105 Correct 274 ms 76024 KB n=200000
106 Correct 276 ms 76112 KB n=200000
107 Correct 296 ms 76060 KB n=200000
108 Correct 521 ms 74548 KB n=200000
109 Correct 528 ms 74576 KB n=200000
110 Correct 578 ms 74580 KB n=200000
111 Correct 525 ms 73792 KB n=200000
112 Correct 653 ms 83280 KB n=200000
113 Correct 695 ms 78820 KB n=200000
114 Correct 479 ms 74180 KB n=200000
115 Correct 728 ms 76548 KB n=200000
116 Correct 483 ms 74468 KB n=200000
117 Correct 616 ms 83296 KB n=200000
118 Correct 648 ms 77404 KB n=200000
119 Correct 447 ms 74552 KB n=200000
120 Correct 564 ms 82980 KB n=200000
121 Correct 552 ms 83044 KB n=200000
122 Correct 583 ms 83280 KB n=200000
123 Correct 283 ms 75860 KB n=200000
124 Correct 113 ms 36432 KB n=25264