#include <bits/stdc++.h>
using namespace std;
const int LOG = 18;
struct lowest_common_ancestor{
vector<int> d;
vector<vector<int>> pp;
lowest_common_ancestor(vector<int> &p, vector<int> &d): d(d){
int N = p.size();
pp = vector<vector<int>>(LOG, vector<int>(N, -1));
pp[0] = p;
for (int i = 0; i < LOG - 1; i++){
for (int j = 0; j < N; j++){
if (pp[i][j] != -1){
pp[i + 1][j] = pp[i][pp[i][j]];
}
}
}
}
int lca(int u, int v){
if (d[u] > d[v]){
swap(u, v);
}
for (int i = 0; i < LOG; i++){
if (((d[v] - d[u]) >> i & 1) == 1){
v = pp[i][v];
}
}
if (u == v){
return u;
} else {
for (int i = LOG - 1; i >= 0; i--){
if (pp[i][u] != pp[i][v]){
u = pp[i][u];
v = pp[i][v];
}
}
return pp[0][u];
}
}
};
int main(){
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> E(n);
for (int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--;
v--;
E[u].push_back(v);
E[v].push_back(u);
}
vector<int> a(m);
for (int i = 0; i < m; i++){
cin >> a[i];
a[i]--;
}
vector<int> p(n, -1);
vector<int> d(n, 0);
queue<int> Q;
Q.push(0);
while (!Q.empty()){
int v = Q.front();
Q.pop();
for (int w : E[v]){
if (w != p[v]){
p[w] = v;
d[w] = d[v] + 1;
Q.push(w);
}
}
}
lowest_common_ancestor T(p, d);
vector<set<int>> st1(n);
for (int i = 0; i < m; i++){
st1[a[i]].insert(i);
}
for (int i = 0; i < n; i++){
st1[i].insert(m);
}
vector<set<int>> st2(n);
for (int i = 0; i < m - 1; i++){
st2[T.lca(a[i], a[i + 1])].insert(i);
}
for (int i = 0; i < n; i++){
st2[i].insert(m - 1);
}
for (int i = 0; i < q; i++){
int t;
cin >> t;
if (t == 1){
int pos, v;
cin >> pos >> v;
pos--;
v--;
st1[a[pos]].erase(pos);
if (pos > 0){
st2[T.lca(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos < m - 1){
st2[T.lca(a[pos], a[pos + 1])].erase(pos);
}
a[pos] = v;
st1[a[pos]].insert(pos);
if (pos > 0){
st2[T.lca(a[pos - 1], a[pos])].insert(pos - 1);
}
if (pos < m - 1){
st2[T.lca(a[pos], a[pos + 1])].insert(pos);
}
}
if (t == 2){
int l, r, v;
cin >> l >> r >> v;
l--;
v--;
auto itr1 = st1[v].lower_bound(l);
if (*itr1 < r){
cout << *itr1 + 1 << ' ' << *itr1 + 1 << endl;
} else {
auto itr2 = st2[v].lower_bound(l);
if (*itr2 < r - 1){
cout << *itr2 + 1 << ' ' << *itr2 + 2 << endl;
} else {
cout << -1 << ' ' << -1 << endl;
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n=5 |
2 |
Correct |
1 ms |
304 KB |
n=100 |
3 |
Correct |
1 ms |
440 KB |
n=100 |
4 |
Correct |
1 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
304 KB |
n=100 |
6 |
Correct |
1 ms |
300 KB |
n=100 |
7 |
Correct |
1 ms |
300 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
1 ms |
340 KB |
n=100 |
11 |
Correct |
1 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
340 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
340 KB |
n=100 |
17 |
Correct |
1 ms |
296 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
296 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
340 KB |
n=100 |
26 |
Correct |
1 ms |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n=5 |
2 |
Correct |
1 ms |
304 KB |
n=100 |
3 |
Correct |
1 ms |
440 KB |
n=100 |
4 |
Correct |
1 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
304 KB |
n=100 |
6 |
Correct |
1 ms |
300 KB |
n=100 |
7 |
Correct |
1 ms |
300 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
1 ms |
340 KB |
n=100 |
11 |
Correct |
1 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
340 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
340 KB |
n=100 |
17 |
Correct |
1 ms |
296 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
296 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
340 KB |
n=100 |
26 |
Correct |
1 ms |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
2 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
2 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
2 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
2 ms |
440 KB |
n=500 |
35 |
Correct |
2 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
2 ms |
468 KB |
n=500 |
40 |
Correct |
2 ms |
468 KB |
n=500 |
41 |
Correct |
2 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
436 KB |
n=500 |
44 |
Correct |
2 ms |
436 KB |
n=500 |
45 |
Correct |
2 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
3 ms |
440 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 ms |
468 KB |
n=500 |
50 |
Correct |
2 ms |
444 KB |
n=500 |
51 |
Correct |
2 ms |
440 KB |
n=500 |
52 |
Correct |
2 ms |
440 KB |
n=500 |
53 |
Correct |
2 ms |
468 KB |
n=500 |
54 |
Correct |
2 ms |
468 KB |
n=500 |
55 |
Correct |
1 ms |
340 KB |
n=278 |
56 |
Correct |
2 ms |
468 KB |
n=500 |
57 |
Correct |
2 ms |
468 KB |
n=500 |
58 |
Correct |
2 ms |
468 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n=5 |
2 |
Correct |
1 ms |
304 KB |
n=100 |
3 |
Correct |
1 ms |
440 KB |
n=100 |
4 |
Correct |
1 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
304 KB |
n=100 |
6 |
Correct |
1 ms |
300 KB |
n=100 |
7 |
Correct |
1 ms |
300 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
1 ms |
340 KB |
n=100 |
11 |
Correct |
1 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
340 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
340 KB |
n=100 |
17 |
Correct |
1 ms |
296 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
296 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
340 KB |
n=100 |
26 |
Correct |
1 ms |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
2 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
2 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
2 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
2 ms |
440 KB |
n=500 |
35 |
Correct |
2 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
2 ms |
468 KB |
n=500 |
40 |
Correct |
2 ms |
468 KB |
n=500 |
41 |
Correct |
2 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
436 KB |
n=500 |
44 |
Correct |
2 ms |
436 KB |
n=500 |
45 |
Correct |
2 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
3 ms |
440 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 ms |
468 KB |
n=500 |
50 |
Correct |
2 ms |
444 KB |
n=500 |
51 |
Correct |
2 ms |
440 KB |
n=500 |
52 |
Correct |
2 ms |
440 KB |
n=500 |
53 |
Correct |
2 ms |
468 KB |
n=500 |
54 |
Correct |
2 ms |
468 KB |
n=500 |
55 |
Correct |
1 ms |
340 KB |
n=278 |
56 |
Correct |
2 ms |
468 KB |
n=500 |
57 |
Correct |
2 ms |
468 KB |
n=500 |
58 |
Correct |
2 ms |
468 KB |
n=500 |
59 |
Correct |
7 ms |
1108 KB |
n=2000 |
60 |
Correct |
8 ms |
1108 KB |
n=2000 |
61 |
Correct |
7 ms |
1108 KB |
n=2000 |
62 |
Correct |
7 ms |
1212 KB |
n=2000 |
63 |
Correct |
7 ms |
1108 KB |
n=2000 |
64 |
Correct |
8 ms |
1204 KB |
n=2000 |
65 |
Correct |
7 ms |
1092 KB |
n=2000 |
66 |
Correct |
7 ms |
1108 KB |
n=2000 |
67 |
Correct |
7 ms |
1108 KB |
n=2000 |
68 |
Correct |
7 ms |
1080 KB |
n=2000 |
69 |
Correct |
7 ms |
1204 KB |
n=2000 |
70 |
Correct |
7 ms |
1108 KB |
n=2000 |
71 |
Correct |
7 ms |
1208 KB |
n=2000 |
72 |
Correct |
8 ms |
1108 KB |
n=2000 |
73 |
Correct |
7 ms |
1108 KB |
n=2000 |
74 |
Correct |
6 ms |
1108 KB |
n=1844 |
75 |
Correct |
7 ms |
1108 KB |
n=2000 |
76 |
Correct |
8 ms |
1108 KB |
n=2000 |
77 |
Correct |
7 ms |
1208 KB |
n=2000 |
78 |
Correct |
7 ms |
1108 KB |
n=2000 |
79 |
Correct |
7 ms |
1092 KB |
n=2000 |
80 |
Correct |
7 ms |
1108 KB |
n=2000 |
81 |
Correct |
8 ms |
1208 KB |
n=2000 |
82 |
Correct |
7 ms |
1108 KB |
n=2000 |
83 |
Correct |
7 ms |
1108 KB |
n=2000 |
84 |
Correct |
7 ms |
1108 KB |
n=2000 |
85 |
Correct |
7 ms |
1108 KB |
n=2000 |
86 |
Correct |
7 ms |
1108 KB |
n=2000 |
87 |
Correct |
7 ms |
1108 KB |
n=2000 |
88 |
Correct |
7 ms |
1076 KB |
n=2000 |
89 |
Correct |
7 ms |
1076 KB |
n=2000 |
90 |
Correct |
7 ms |
1088 KB |
n=2000 |
91 |
Correct |
9 ms |
1108 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n=5 |
2 |
Correct |
1 ms |
304 KB |
n=100 |
3 |
Correct |
1 ms |
440 KB |
n=100 |
4 |
Correct |
1 ms |
340 KB |
n=100 |
5 |
Correct |
1 ms |
304 KB |
n=100 |
6 |
Correct |
1 ms |
300 KB |
n=100 |
7 |
Correct |
1 ms |
300 KB |
n=100 |
8 |
Correct |
1 ms |
340 KB |
n=100 |
9 |
Correct |
1 ms |
340 KB |
n=100 |
10 |
Correct |
1 ms |
340 KB |
n=100 |
11 |
Correct |
1 ms |
340 KB |
n=100 |
12 |
Correct |
1 ms |
340 KB |
n=100 |
13 |
Correct |
1 ms |
340 KB |
n=100 |
14 |
Correct |
1 ms |
340 KB |
n=100 |
15 |
Correct |
1 ms |
340 KB |
n=100 |
16 |
Correct |
1 ms |
340 KB |
n=100 |
17 |
Correct |
1 ms |
296 KB |
n=100 |
18 |
Correct |
1 ms |
340 KB |
n=100 |
19 |
Correct |
1 ms |
340 KB |
n=100 |
20 |
Correct |
1 ms |
340 KB |
n=100 |
21 |
Correct |
1 ms |
340 KB |
n=100 |
22 |
Correct |
1 ms |
296 KB |
n=100 |
23 |
Correct |
1 ms |
340 KB |
n=100 |
24 |
Correct |
1 ms |
340 KB |
n=100 |
25 |
Correct |
1 ms |
340 KB |
n=100 |
26 |
Correct |
1 ms |
212 KB |
n=12 |
27 |
Correct |
1 ms |
340 KB |
n=100 |
28 |
Correct |
2 ms |
468 KB |
n=500 |
29 |
Correct |
2 ms |
468 KB |
n=500 |
30 |
Correct |
2 ms |
468 KB |
n=500 |
31 |
Correct |
2 ms |
468 KB |
n=500 |
32 |
Correct |
2 ms |
468 KB |
n=500 |
33 |
Correct |
2 ms |
468 KB |
n=500 |
34 |
Correct |
2 ms |
440 KB |
n=500 |
35 |
Correct |
2 ms |
468 KB |
n=500 |
36 |
Correct |
2 ms |
468 KB |
n=500 |
37 |
Correct |
2 ms |
468 KB |
n=500 |
38 |
Correct |
2 ms |
468 KB |
n=500 |
39 |
Correct |
2 ms |
468 KB |
n=500 |
40 |
Correct |
2 ms |
468 KB |
n=500 |
41 |
Correct |
2 ms |
468 KB |
n=500 |
42 |
Correct |
2 ms |
468 KB |
n=500 |
43 |
Correct |
2 ms |
436 KB |
n=500 |
44 |
Correct |
2 ms |
436 KB |
n=500 |
45 |
Correct |
2 ms |
468 KB |
n=500 |
46 |
Correct |
2 ms |
468 KB |
n=500 |
47 |
Correct |
3 ms |
440 KB |
n=500 |
48 |
Correct |
2 ms |
468 KB |
n=500 |
49 |
Correct |
2 ms |
468 KB |
n=500 |
50 |
Correct |
2 ms |
444 KB |
n=500 |
51 |
Correct |
2 ms |
440 KB |
n=500 |
52 |
Correct |
2 ms |
440 KB |
n=500 |
53 |
Correct |
2 ms |
468 KB |
n=500 |
54 |
Correct |
2 ms |
468 KB |
n=500 |
55 |
Correct |
1 ms |
340 KB |
n=278 |
56 |
Correct |
2 ms |
468 KB |
n=500 |
57 |
Correct |
2 ms |
468 KB |
n=500 |
58 |
Correct |
2 ms |
468 KB |
n=500 |
59 |
Correct |
7 ms |
1108 KB |
n=2000 |
60 |
Correct |
8 ms |
1108 KB |
n=2000 |
61 |
Correct |
7 ms |
1108 KB |
n=2000 |
62 |
Correct |
7 ms |
1212 KB |
n=2000 |
63 |
Correct |
7 ms |
1108 KB |
n=2000 |
64 |
Correct |
8 ms |
1204 KB |
n=2000 |
65 |
Correct |
7 ms |
1092 KB |
n=2000 |
66 |
Correct |
7 ms |
1108 KB |
n=2000 |
67 |
Correct |
7 ms |
1108 KB |
n=2000 |
68 |
Correct |
7 ms |
1080 KB |
n=2000 |
69 |
Correct |
7 ms |
1204 KB |
n=2000 |
70 |
Correct |
7 ms |
1108 KB |
n=2000 |
71 |
Correct |
7 ms |
1208 KB |
n=2000 |
72 |
Correct |
8 ms |
1108 KB |
n=2000 |
73 |
Correct |
7 ms |
1108 KB |
n=2000 |
74 |
Correct |
6 ms |
1108 KB |
n=1844 |
75 |
Correct |
7 ms |
1108 KB |
n=2000 |
76 |
Correct |
8 ms |
1108 KB |
n=2000 |
77 |
Correct |
7 ms |
1208 KB |
n=2000 |
78 |
Correct |
7 ms |
1108 KB |
n=2000 |
79 |
Correct |
7 ms |
1092 KB |
n=2000 |
80 |
Correct |
7 ms |
1108 KB |
n=2000 |
81 |
Correct |
8 ms |
1208 KB |
n=2000 |
82 |
Correct |
7 ms |
1108 KB |
n=2000 |
83 |
Correct |
7 ms |
1108 KB |
n=2000 |
84 |
Correct |
7 ms |
1108 KB |
n=2000 |
85 |
Correct |
7 ms |
1108 KB |
n=2000 |
86 |
Correct |
7 ms |
1108 KB |
n=2000 |
87 |
Correct |
7 ms |
1108 KB |
n=2000 |
88 |
Correct |
7 ms |
1076 KB |
n=2000 |
89 |
Correct |
7 ms |
1076 KB |
n=2000 |
90 |
Correct |
7 ms |
1088 KB |
n=2000 |
91 |
Correct |
9 ms |
1108 KB |
n=2000 |
92 |
Correct |
1146 ms |
93364 KB |
n=200000 |
93 |
Correct |
1505 ms |
93580 KB |
n=200000 |
94 |
Correct |
1509 ms |
93544 KB |
n=200000 |
95 |
Correct |
1181 ms |
93128 KB |
n=200000 |
96 |
Correct |
1135 ms |
93316 KB |
n=200000 |
97 |
Correct |
1632 ms |
93568 KB |
n=200000 |
98 |
Correct |
1241 ms |
93120 KB |
n=200000 |
99 |
Correct |
1314 ms |
93428 KB |
n=200000 |
100 |
Correct |
1137 ms |
93236 KB |
n=200000 |
101 |
Correct |
1554 ms |
93632 KB |
n=200000 |
102 |
Correct |
912 ms |
94340 KB |
n=200000 |
103 |
Correct |
918 ms |
94404 KB |
n=200000 |
104 |
Correct |
902 ms |
94396 KB |
n=200000 |
105 |
Correct |
943 ms |
94768 KB |
n=200000 |
106 |
Correct |
949 ms |
94768 KB |
n=200000 |
107 |
Correct |
912 ms |
94788 KB |
n=200000 |
108 |
Correct |
1329 ms |
93372 KB |
n=200000 |
109 |
Correct |
1345 ms |
93360 KB |
n=200000 |
110 |
Correct |
1331 ms |
93216 KB |
n=200000 |
111 |
Correct |
1100 ms |
92876 KB |
n=200000 |
112 |
Correct |
1379 ms |
93500 KB |
n=200000 |
113 |
Correct |
1483 ms |
93592 KB |
n=200000 |
114 |
Correct |
1126 ms |
92780 KB |
n=200000 |
115 |
Correct |
1721 ms |
93836 KB |
n=200000 |
116 |
Correct |
1164 ms |
93380 KB |
n=200000 |
117 |
Correct |
1402 ms |
93400 KB |
n=200000 |
118 |
Correct |
1635 ms |
93540 KB |
n=200000 |
119 |
Correct |
1163 ms |
93368 KB |
n=200000 |
120 |
Correct |
1421 ms |
92480 KB |
n=200000 |
121 |
Correct |
1361 ms |
92448 KB |
n=200000 |
122 |
Correct |
1483 ms |
92896 KB |
n=200000 |
123 |
Correct |
1004 ms |
94700 KB |
n=200000 |
124 |
Correct |
333 ms |
19528 KB |
n=25264 |