Submission #880899

# Submission time Handle Problem Language Result Execution time Memory
880899 2023-11-30T08:16:12 Z alex_2008 Birthday gift (IZhO18_treearray) C++14
100 / 100
1068 ms 84020 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 21080 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21080 KB n=100
4 Correct 5 ms 21084 KB n=100
5 Correct 5 ms 21080 KB n=100
6 Correct 5 ms 21084 KB n=100
7 Correct 6 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 21336 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 5 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21336 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 5 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 5 ms 21112 KB n=100
23 Correct 5 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 5 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21336 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21080 KB n=100
4 Correct 5 ms 21084 KB n=100
5 Correct 5 ms 21080 KB n=100
6 Correct 5 ms 21084 KB n=100
7 Correct 6 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 21336 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 5 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21336 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 5 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 5 ms 21112 KB n=100
23 Correct 5 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 5 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21336 KB n=100
28 Correct 5 ms 21084 KB n=500
29 Correct 5 ms 21084 KB n=500
30 Correct 5 ms 21084 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 6 ms 21188 KB n=500
33 Correct 5 ms 21084 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 7 ms 21080 KB n=500
39 Correct 6 ms 21084 KB n=500
40 Correct 5 ms 21080 KB n=500
41 Correct 5 ms 21180 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 6 ms 21080 KB n=500
46 Correct 5 ms 21080 KB n=500
47 Correct 5 ms 21084 KB n=500
48 Correct 5 ms 21084 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21084 KB n=500
51 Correct 6 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 6 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 5 ms 21084 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21080 KB n=100
4 Correct 5 ms 21084 KB n=100
5 Correct 5 ms 21080 KB n=100
6 Correct 5 ms 21084 KB n=100
7 Correct 6 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 21336 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 5 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21336 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 5 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 5 ms 21112 KB n=100
23 Correct 5 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 5 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21336 KB n=100
28 Correct 5 ms 21084 KB n=500
29 Correct 5 ms 21084 KB n=500
30 Correct 5 ms 21084 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 6 ms 21188 KB n=500
33 Correct 5 ms 21084 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 7 ms 21080 KB n=500
39 Correct 6 ms 21084 KB n=500
40 Correct 5 ms 21080 KB n=500
41 Correct 5 ms 21180 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 6 ms 21080 KB n=500
46 Correct 5 ms 21080 KB n=500
47 Correct 5 ms 21084 KB n=500
48 Correct 5 ms 21084 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21084 KB n=500
51 Correct 6 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 6 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 5 ms 21084 KB n=500
59 Correct 9 ms 21340 KB n=2000
60 Correct 10 ms 21340 KB n=2000
61 Correct 10 ms 21336 KB n=2000
62 Correct 9 ms 21336 KB n=2000
63 Correct 9 ms 21340 KB n=2000
64 Correct 12 ms 21596 KB n=2000
65 Correct 9 ms 21336 KB n=2000
66 Correct 9 ms 21336 KB n=2000
67 Correct 9 ms 21340 KB n=2000
68 Correct 9 ms 21340 KB n=2000
69 Correct 10 ms 21340 KB n=2000
70 Correct 10 ms 21340 KB n=2000
71 Correct 9 ms 21340 KB n=2000
72 Correct 10 ms 21336 KB n=2000
73 Correct 10 ms 21340 KB n=2000
74 Correct 8 ms 21592 KB n=1844
75 Correct 10 ms 21336 KB n=2000
76 Correct 9 ms 21340 KB n=2000
77 Correct 9 ms 21340 KB n=2000
78 Correct 9 ms 21340 KB n=2000
79 Correct 9 ms 21340 KB n=2000
80 Correct 9 ms 21336 KB n=2000
81 Correct 10 ms 21340 KB n=2000
82 Correct 9 ms 21220 KB n=2000
83 Correct 9 ms 21340 KB n=2000
84 Correct 9 ms 21340 KB n=2000
85 Correct 9 ms 21340 KB n=2000
86 Correct 9 ms 21592 KB n=2000
87 Correct 9 ms 21340 KB n=2000
88 Correct 9 ms 21340 KB n=2000
89 Correct 9 ms 21340 KB n=2000
90 Correct 9 ms 21340 KB n=2000
91 Correct 10 ms 21340 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB n=5
2 Correct 4 ms 21084 KB n=100
3 Correct 4 ms 21080 KB n=100
4 Correct 5 ms 21084 KB n=100
5 Correct 5 ms 21080 KB n=100
6 Correct 5 ms 21084 KB n=100
7 Correct 6 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 21336 KB n=100
11 Correct 4 ms 21084 KB n=100
12 Correct 4 ms 21084 KB n=100
13 Correct 5 ms 21084 KB n=100
14 Correct 4 ms 21084 KB n=100
15 Correct 4 ms 21336 KB n=100
16 Correct 4 ms 21084 KB n=100
17 Correct 4 ms 21084 KB n=100
18 Correct 4 ms 21084 KB n=100
19 Correct 5 ms 21084 KB n=100
20 Correct 4 ms 21084 KB n=100
21 Correct 4 ms 21084 KB n=100
22 Correct 5 ms 21112 KB n=100
23 Correct 5 ms 21084 KB n=100
24 Correct 4 ms 21084 KB n=100
25 Correct 5 ms 21084 KB n=100
26 Correct 4 ms 21084 KB n=12
27 Correct 4 ms 21336 KB n=100
28 Correct 5 ms 21084 KB n=500
29 Correct 5 ms 21084 KB n=500
30 Correct 5 ms 21084 KB n=500
31 Correct 5 ms 21080 KB n=500
32 Correct 6 ms 21188 KB n=500
33 Correct 5 ms 21084 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 7 ms 21080 KB n=500
39 Correct 6 ms 21084 KB n=500
40 Correct 5 ms 21080 KB n=500
41 Correct 5 ms 21180 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 6 ms 21080 KB n=500
46 Correct 5 ms 21080 KB n=500
47 Correct 5 ms 21084 KB n=500
48 Correct 5 ms 21084 KB n=500
49 Correct 5 ms 21084 KB n=500
50 Correct 5 ms 21084 KB n=500
51 Correct 6 ms 21084 KB n=500
52 Correct 5 ms 21084 KB n=500
53 Correct 5 ms 21084 KB n=500
54 Correct 6 ms 21084 KB n=500
55 Correct 4 ms 21084 KB n=278
56 Correct 5 ms 21084 KB n=500
57 Correct 5 ms 21084 KB n=500
58 Correct 5 ms 21084 KB n=500
59 Correct 9 ms 21340 KB n=2000
60 Correct 10 ms 21340 KB n=2000
61 Correct 10 ms 21336 KB n=2000
62 Correct 9 ms 21336 KB n=2000
63 Correct 9 ms 21340 KB n=2000
64 Correct 12 ms 21596 KB n=2000
65 Correct 9 ms 21336 KB n=2000
66 Correct 9 ms 21336 KB n=2000
67 Correct 9 ms 21340 KB n=2000
68 Correct 9 ms 21340 KB n=2000
69 Correct 10 ms 21340 KB n=2000
70 Correct 10 ms 21340 KB n=2000
71 Correct 9 ms 21340 KB n=2000
72 Correct 10 ms 21336 KB n=2000
73 Correct 10 ms 21340 KB n=2000
74 Correct 8 ms 21592 KB n=1844
75 Correct 10 ms 21336 KB n=2000
76 Correct 9 ms 21340 KB n=2000
77 Correct 9 ms 21340 KB n=2000
78 Correct 9 ms 21340 KB n=2000
79 Correct 9 ms 21340 KB n=2000
80 Correct 9 ms 21336 KB n=2000
81 Correct 10 ms 21340 KB n=2000
82 Correct 9 ms 21220 KB n=2000
83 Correct 9 ms 21340 KB n=2000
84 Correct 9 ms 21340 KB n=2000
85 Correct 9 ms 21340 KB n=2000
86 Correct 9 ms 21592 KB n=2000
87 Correct 9 ms 21340 KB n=2000
88 Correct 9 ms 21340 KB n=2000
89 Correct 9 ms 21340 KB n=2000
90 Correct 9 ms 21340 KB n=2000
91 Correct 10 ms 21340 KB n=2000
92 Correct 771 ms 72540 KB n=200000
93 Correct 939 ms 79480 KB n=200000
94 Correct 972 ms 83088 KB n=200000
95 Correct 763 ms 74312 KB n=200000
96 Correct 745 ms 74436 KB n=200000
97 Correct 979 ms 78756 KB n=200000
98 Correct 747 ms 74528 KB n=200000
99 Correct 930 ms 74624 KB n=200000
100 Correct 789 ms 74460 KB n=200000
101 Correct 966 ms 84020 KB n=200000
102 Correct 672 ms 75700 KB n=200000
103 Correct 734 ms 75476 KB n=200000
104 Correct 702 ms 75712 KB n=200000
105 Correct 680 ms 76116 KB n=200000
106 Correct 711 ms 76008 KB n=200000
107 Correct 690 ms 75936 KB n=200000
108 Correct 875 ms 74368 KB n=200000
109 Correct 828 ms 74572 KB n=200000
110 Correct 833 ms 74344 KB n=200000
111 Correct 773 ms 74028 KB n=200000
112 Correct 1006 ms 83204 KB n=200000
113 Correct 1026 ms 78876 KB n=200000
114 Correct 753 ms 73912 KB n=200000
115 Correct 1068 ms 76496 KB n=200000
116 Correct 827 ms 74580 KB n=200000
117 Correct 1004 ms 83520 KB n=200000
118 Correct 1064 ms 77504 KB n=200000
119 Correct 803 ms 74504 KB n=200000
120 Correct 970 ms 83036 KB n=200000
121 Correct 942 ms 83076 KB n=200000
122 Correct 955 ms 83292 KB n=200000
123 Correct 739 ms 76116 KB n=200000
124 Correct 226 ms 36600 KB n=25264