Submission #880909

# Submission time Handle Problem Language Result Execution time Memory
880909 2023-11-30T08:33:17 Z tigran Birthday gift (IZhO18_treearray) C++17
100 / 100
1076 ms 76628 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() {
	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 5 ms 21084 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21084 KB n=100
6 Correct 4 ms 21084 KB n=100
7 Correct 4 ms 21080 KB n=100
8 Correct 4 ms 21080 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 5 ms 21084 KB n=100
12 Correct 5 ms 21084 KB n=100
13 Correct 4 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21120 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21112 KB n=100
23 Correct 5 ms 21080 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21084 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 5 ms 21084 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21084 KB n=100
6 Correct 4 ms 21084 KB n=100
7 Correct 4 ms 21080 KB n=100
8 Correct 4 ms 21080 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 5 ms 21084 KB n=100
12 Correct 5 ms 21084 KB n=100
13 Correct 4 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21120 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21112 KB n=100
23 Correct 5 ms 21080 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21080 KB n=500
29 Correct 5 ms 21080 KB n=500
30 Correct 5 ms 21080 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21080 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 5 ms 21084 KB n=500
36 Correct 5 ms 21080 KB n=500
37 Correct 5 ms 21084 KB n=500
38 Correct 6 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21184 KB n=500
41 Correct 6 ms 21084 KB n=500
42 Correct 5 ms 21080 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21084 KB n=500
45 Correct 5 ms 21080 KB n=500
46 Correct 5 ms 21084 KB n=500
47 Correct 6 ms 21084 KB n=500
48 Correct 5 ms 21080 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21080 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 6 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 5 ms 21084 KB n=500
55 Correct 5 ms 21080 KB n=278
56 Correct 6 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 6 ms 21084 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 5 ms 21084 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21084 KB n=100
6 Correct 4 ms 21084 KB n=100
7 Correct 4 ms 21080 KB n=100
8 Correct 4 ms 21080 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 5 ms 21084 KB n=100
12 Correct 5 ms 21084 KB n=100
13 Correct 4 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21120 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21112 KB n=100
23 Correct 5 ms 21080 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21080 KB n=500
29 Correct 5 ms 21080 KB n=500
30 Correct 5 ms 21080 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21080 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 5 ms 21084 KB n=500
36 Correct 5 ms 21080 KB n=500
37 Correct 5 ms 21084 KB n=500
38 Correct 6 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21184 KB n=500
41 Correct 6 ms 21084 KB n=500
42 Correct 5 ms 21080 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21084 KB n=500
45 Correct 5 ms 21080 KB n=500
46 Correct 5 ms 21084 KB n=500
47 Correct 6 ms 21084 KB n=500
48 Correct 5 ms 21080 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21080 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 6 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 5 ms 21084 KB n=500
55 Correct 5 ms 21080 KB n=278
56 Correct 6 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 6 ms 21084 KB n=500
59 Correct 9 ms 21340 KB n=2000
60 Correct 9 ms 21340 KB n=2000
61 Correct 9 ms 21340 KB n=2000
62 Correct 10 ms 21336 KB n=2000
63 Correct 9 ms 21340 KB n=2000
64 Correct 9 ms 21340 KB n=2000
65 Correct 9 ms 21380 KB n=2000
66 Correct 9 ms 21340 KB n=2000
67 Correct 10 ms 21340 KB n=2000
68 Correct 9 ms 21340 KB n=2000
69 Correct 11 ms 21340 KB n=2000
70 Correct 10 ms 21336 KB n=2000
71 Correct 10 ms 21340 KB n=2000
72 Correct 10 ms 21340 KB n=2000
73 Correct 10 ms 21340 KB n=2000
74 Correct 8 ms 21340 KB n=1844
75 Correct 10 ms 21340 KB n=2000
76 Correct 9 ms 21592 KB n=2000
77 Correct 10 ms 21336 KB n=2000
78 Correct 10 ms 21340 KB n=2000
79 Correct 10 ms 21340 KB n=2000
80 Correct 9 ms 21340 KB n=2000
81 Correct 10 ms 21340 KB n=2000
82 Correct 9 ms 21340 KB n=2000
83 Correct 9 ms 21336 KB n=2000
84 Correct 9 ms 21340 KB n=2000
85 Correct 9 ms 21340 KB n=2000
86 Correct 9 ms 21340 KB n=2000
87 Correct 10 ms 21340 KB n=2000
88 Correct 10 ms 21340 KB n=2000
89 Correct 9 ms 21340 KB n=2000
90 Correct 9 ms 21336 KB n=2000
91 Correct 10 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 5 ms 21084 KB n=100
4 Correct 4 ms 21084 KB n=100
5 Correct 4 ms 21084 KB n=100
6 Correct 4 ms 21084 KB n=100
7 Correct 4 ms 21080 KB n=100
8 Correct 4 ms 21080 KB n=100
9 Correct 4 ms 21084 KB n=100
10 Correct 4 ms 21084 KB n=100
11 Correct 5 ms 21084 KB n=100
12 Correct 5 ms 21084 KB n=100
13 Correct 4 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21084 KB n=100
16 Correct 4 ms 21120 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 4 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 4 ms 21112 KB n=100
23 Correct 5 ms 21080 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 4 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21084 KB n=100
28 Correct 6 ms 21080 KB n=500
29 Correct 5 ms 21080 KB n=500
30 Correct 5 ms 21080 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 5 ms 21084 KB n=500
33 Correct 5 ms 21080 KB n=500
34 Correct 5 ms 21084 KB n=500
35 Correct 5 ms 21084 KB n=500
36 Correct 5 ms 21080 KB n=500
37 Correct 5 ms 21084 KB n=500
38 Correct 6 ms 21084 KB n=500
39 Correct 5 ms 21084 KB n=500
40 Correct 5 ms 21184 KB n=500
41 Correct 6 ms 21084 KB n=500
42 Correct 5 ms 21080 KB n=500
43 Correct 5 ms 21084 KB n=500
44 Correct 5 ms 21084 KB n=500
45 Correct 5 ms 21080 KB n=500
46 Correct 5 ms 21084 KB n=500
47 Correct 6 ms 21084 KB n=500
48 Correct 5 ms 21080 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21080 KB n=500
51 Correct 5 ms 21084 KB n=500
52 Correct 6 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 5 ms 21084 KB n=500
55 Correct 5 ms 21080 KB n=278
56 Correct 6 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 6 ms 21084 KB n=500
59 Correct 9 ms 21340 KB n=2000
60 Correct 9 ms 21340 KB n=2000
61 Correct 9 ms 21340 KB n=2000
62 Correct 10 ms 21336 KB n=2000
63 Correct 9 ms 21340 KB n=2000
64 Correct 9 ms 21340 KB n=2000
65 Correct 9 ms 21380 KB n=2000
66 Correct 9 ms 21340 KB n=2000
67 Correct 10 ms 21340 KB n=2000
68 Correct 9 ms 21340 KB n=2000
69 Correct 11 ms 21340 KB n=2000
70 Correct 10 ms 21336 KB n=2000
71 Correct 10 ms 21340 KB n=2000
72 Correct 10 ms 21340 KB n=2000
73 Correct 10 ms 21340 KB n=2000
74 Correct 8 ms 21340 KB n=1844
75 Correct 10 ms 21340 KB n=2000
76 Correct 9 ms 21592 KB n=2000
77 Correct 10 ms 21336 KB n=2000
78 Correct 10 ms 21340 KB n=2000
79 Correct 10 ms 21340 KB n=2000
80 Correct 9 ms 21340 KB n=2000
81 Correct 10 ms 21340 KB n=2000
82 Correct 9 ms 21340 KB n=2000
83 Correct 9 ms 21336 KB n=2000
84 Correct 9 ms 21340 KB n=2000
85 Correct 9 ms 21340 KB n=2000
86 Correct 9 ms 21340 KB n=2000
87 Correct 10 ms 21340 KB n=2000
88 Correct 10 ms 21340 KB n=2000
89 Correct 9 ms 21340 KB n=2000
90 Correct 9 ms 21336 KB n=2000
91 Correct 10 ms 21340 KB n=2000
92 Correct 782 ms 68108 KB n=200000
93 Correct 1014 ms 71852 KB n=200000
94 Correct 996 ms 75112 KB n=200000
95 Correct 746 ms 67848 KB n=200000
96 Correct 751 ms 68296 KB n=200000
97 Correct 988 ms 71192 KB n=200000
98 Correct 764 ms 68168 KB n=200000
99 Correct 910 ms 67888 KB n=200000
100 Correct 766 ms 67952 KB n=200000
101 Correct 1002 ms 76400 KB n=200000
102 Correct 683 ms 68944 KB n=200000
103 Correct 688 ms 69004 KB n=200000
104 Correct 680 ms 68860 KB n=200000
105 Correct 748 ms 68684 KB n=200000
106 Correct 700 ms 68688 KB n=200000
107 Correct 710 ms 68776 KB n=200000
108 Correct 813 ms 67636 KB n=200000
109 Correct 829 ms 67988 KB n=200000
110 Correct 865 ms 67504 KB n=200000
111 Correct 746 ms 67872 KB n=200000
112 Correct 1006 ms 75576 KB n=200000
113 Correct 1008 ms 70808 KB n=200000
114 Correct 764 ms 67784 KB n=200000
115 Correct 1076 ms 68852 KB n=200000
116 Correct 763 ms 67660 KB n=200000
117 Correct 976 ms 75976 KB n=200000
118 Correct 1025 ms 69716 KB n=200000
119 Correct 805 ms 67716 KB n=200000
120 Correct 960 ms 76252 KB n=200000
121 Correct 933 ms 76116 KB n=200000
122 Correct 931 ms 76628 KB n=200000
123 Correct 736 ms 68244 KB n=200000
124 Correct 220 ms 34760 KB n=25264