#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int N = 1 << 18;
int n, m, q, a[N];
vector<int> g[N];
set<int> pos[N];
int d[N];
vector<int> anc[N];
void dfs(int u, int p) {
for(int v: g[u]) if(v != p) {
d[v] = d[u] + 1;
anc[v].push_back(u);
for(int j = 0; 1 << j+1 <= d[v]; j++)
anc[v].push_back(anc[anc[v][j]][j]);
dfs(v, u);
}
}
int lca(int u, int v) {
if(d[v] > d[u]) swap(u, v);
if(d[u] > d[v]) {
for(int j = 0; 1 << j <= d[u] - d[v]; j++)
if(d[u] - d[v] >> j & 1) u = anc[u][j];
}
if(u == v) return u;
for(int j = 18; j >= 0; j--)
if(j < anc[u].size() && anc[u][j] != anc[v][j])
u = anc[u][j], v = anc[v][j];
return anc[u][0];
}
unordered_map<int, unordered_set<int> > t[2*N];
void update(int i, int nw, int rm, int idx) {
i += N;
t[i][rm].erase(idx);
t[i][nw].insert(idx);
i >>= 1;
while(i >= 1) {
t[i][rm].erase(idx);
t[i][nw].insert(idx);
i >>= 1;
}
}
int query(int l, int r, int v) {
int res = -1;
for(l += N, r += N + 1; l < r; l >>= 1, r >>= 1) {
if(l & 1) {
if(!t[l][v].empty()) res = *t[l][v].begin();
++l;
}
if(r & 1) {
--r;
if(!t[r][v].empty()) res = *t[r][v].begin();
}
}
return res;
}
int main() {
scanf("%d %d %d", &n, &m, &q);
for(int i = 1, u, v; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for(int i = 1; i <= m; i++) {
scanf("%d", a+i);
pos[a[i]].insert(i);
if(i > 1) t[N+i-1][lca(a[i-1], a[i])].insert(i-1);
}
for(int i = N-1; i >= 1; i--) {
t[i].insert(t[i<<1].begin(), t[i<<1].end());
for(auto elem: t[i<<1|1]) {
for(int v: elem.second)
t[i][elem.first].insert(v);
}
}
for(int z = 0, tp, l, r, v, i; z < q; z++) {
scanf("%d", &tp);
if(tp == 1) {
scanf("%d %d", &i, &v);
if(i < m) update(i, lca(v, a[i+1]), lca(a[i], a[i+1]), i);
if(i > 1) update(i-1, lca(a[i-1], v), lca(a[i-1], a[i]), i-1);
pos[a[i]].erase(i);
a[i] = v;
pos[a[i]].insert(i);
} else {
scanf("%d %d %d", &l, &r, &v);
int res = query(l, r-1, v);
if(res == -1) {
auto it = pos[v].lower_bound(l);
if(it == pos[v].end() || *it > r) printf("-1 -1\n");
else printf("%d %d\n", *it, *it);
} else printf("%d %d\n", res, res+1);
}
}
}
Compilation message
treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:21:24: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
for(int j = 0; 1 << j+1 <= d[v]; j++)
^
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:30:12: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
if(d[u] - d[v] >> j & 1) u = anc[u][j];
^
treearray.cpp:34:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j < anc[u].size() && anc[u][j] != anc[v][j])
^
treearray.cpp: In function 'int main()':
treearray.cpp:66:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &m, &q);
^
treearray.cpp:68:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
^
treearray.cpp:74:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", a+i);
^
treearray.cpp:86:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &tp);
^
treearray.cpp:88:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &i, &v);
^
treearray.cpp:95:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &l, &r, &v);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
53624 KB |
n=5 |
2 |
Correct |
51 ms |
53892 KB |
n=100 |
3 |
Correct |
53 ms |
54128 KB |
n=100 |
4 |
Correct |
51 ms |
54128 KB |
n=100 |
5 |
Correct |
58 ms |
54128 KB |
n=100 |
6 |
Correct |
52 ms |
54128 KB |
n=100 |
7 |
Correct |
51 ms |
54128 KB |
n=100 |
8 |
Correct |
51 ms |
54128 KB |
n=100 |
9 |
Correct |
51 ms |
54128 KB |
n=100 |
10 |
Correct |
52 ms |
54236 KB |
n=100 |
11 |
Correct |
65 ms |
54236 KB |
n=100 |
12 |
Correct |
51 ms |
54236 KB |
n=100 |
13 |
Correct |
54 ms |
54236 KB |
n=100 |
14 |
Correct |
54 ms |
54236 KB |
n=100 |
15 |
Correct |
52 ms |
54236 KB |
n=100 |
16 |
Correct |
51 ms |
54236 KB |
n=100 |
17 |
Correct |
60 ms |
54236 KB |
n=100 |
18 |
Correct |
51 ms |
54236 KB |
n=100 |
19 |
Correct |
55 ms |
54236 KB |
n=100 |
20 |
Correct |
60 ms |
54236 KB |
n=100 |
21 |
Correct |
52 ms |
54236 KB |
n=100 |
22 |
Correct |
53 ms |
54236 KB |
n=100 |
23 |
Correct |
53 ms |
54236 KB |
n=100 |
24 |
Correct |
51 ms |
54236 KB |
n=100 |
25 |
Correct |
56 ms |
54236 KB |
n=100 |
26 |
Correct |
51 ms |
54236 KB |
n=12 |
27 |
Correct |
51 ms |
54236 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
53624 KB |
n=5 |
2 |
Correct |
51 ms |
53892 KB |
n=100 |
3 |
Correct |
53 ms |
54128 KB |
n=100 |
4 |
Correct |
51 ms |
54128 KB |
n=100 |
5 |
Correct |
58 ms |
54128 KB |
n=100 |
6 |
Correct |
52 ms |
54128 KB |
n=100 |
7 |
Correct |
51 ms |
54128 KB |
n=100 |
8 |
Correct |
51 ms |
54128 KB |
n=100 |
9 |
Correct |
51 ms |
54128 KB |
n=100 |
10 |
Correct |
52 ms |
54236 KB |
n=100 |
11 |
Correct |
65 ms |
54236 KB |
n=100 |
12 |
Correct |
51 ms |
54236 KB |
n=100 |
13 |
Correct |
54 ms |
54236 KB |
n=100 |
14 |
Correct |
54 ms |
54236 KB |
n=100 |
15 |
Correct |
52 ms |
54236 KB |
n=100 |
16 |
Correct |
51 ms |
54236 KB |
n=100 |
17 |
Correct |
60 ms |
54236 KB |
n=100 |
18 |
Correct |
51 ms |
54236 KB |
n=100 |
19 |
Correct |
55 ms |
54236 KB |
n=100 |
20 |
Correct |
60 ms |
54236 KB |
n=100 |
21 |
Correct |
52 ms |
54236 KB |
n=100 |
22 |
Correct |
53 ms |
54236 KB |
n=100 |
23 |
Correct |
53 ms |
54236 KB |
n=100 |
24 |
Correct |
51 ms |
54236 KB |
n=100 |
25 |
Correct |
56 ms |
54236 KB |
n=100 |
26 |
Correct |
51 ms |
54236 KB |
n=12 |
27 |
Correct |
51 ms |
54236 KB |
n=100 |
28 |
Correct |
55 ms |
54492 KB |
n=500 |
29 |
Correct |
61 ms |
55516 KB |
n=500 |
30 |
Correct |
58 ms |
55516 KB |
n=500 |
31 |
Correct |
59 ms |
55516 KB |
n=500 |
32 |
Correct |
60 ms |
55516 KB |
n=500 |
33 |
Correct |
57 ms |
55516 KB |
n=500 |
34 |
Correct |
58 ms |
55516 KB |
n=500 |
35 |
Correct |
59 ms |
55536 KB |
n=500 |
36 |
Correct |
52 ms |
55536 KB |
n=500 |
37 |
Correct |
52 ms |
55536 KB |
n=500 |
38 |
Correct |
53 ms |
55536 KB |
n=500 |
39 |
Correct |
52 ms |
55536 KB |
n=500 |
40 |
Correct |
53 ms |
55536 KB |
n=500 |
41 |
Correct |
54 ms |
55536 KB |
n=500 |
42 |
Correct |
55 ms |
55536 KB |
n=500 |
43 |
Correct |
57 ms |
55536 KB |
n=500 |
44 |
Correct |
55 ms |
55536 KB |
n=500 |
45 |
Correct |
55 ms |
55536 KB |
n=500 |
46 |
Correct |
57 ms |
55536 KB |
n=500 |
47 |
Correct |
58 ms |
55536 KB |
n=500 |
48 |
Correct |
55 ms |
55536 KB |
n=500 |
49 |
Correct |
60 ms |
55536 KB |
n=500 |
50 |
Correct |
57 ms |
55536 KB |
n=500 |
51 |
Correct |
59 ms |
55536 KB |
n=500 |
52 |
Correct |
60 ms |
55644 KB |
n=500 |
53 |
Correct |
59 ms |
55644 KB |
n=500 |
54 |
Correct |
59 ms |
55644 KB |
n=500 |
55 |
Correct |
55 ms |
55644 KB |
n=278 |
56 |
Correct |
69 ms |
55644 KB |
n=500 |
57 |
Correct |
61 ms |
55644 KB |
n=500 |
58 |
Correct |
55 ms |
55644 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
53624 KB |
n=5 |
2 |
Correct |
51 ms |
53892 KB |
n=100 |
3 |
Correct |
53 ms |
54128 KB |
n=100 |
4 |
Correct |
51 ms |
54128 KB |
n=100 |
5 |
Correct |
58 ms |
54128 KB |
n=100 |
6 |
Correct |
52 ms |
54128 KB |
n=100 |
7 |
Correct |
51 ms |
54128 KB |
n=100 |
8 |
Correct |
51 ms |
54128 KB |
n=100 |
9 |
Correct |
51 ms |
54128 KB |
n=100 |
10 |
Correct |
52 ms |
54236 KB |
n=100 |
11 |
Correct |
65 ms |
54236 KB |
n=100 |
12 |
Correct |
51 ms |
54236 KB |
n=100 |
13 |
Correct |
54 ms |
54236 KB |
n=100 |
14 |
Correct |
54 ms |
54236 KB |
n=100 |
15 |
Correct |
52 ms |
54236 KB |
n=100 |
16 |
Correct |
51 ms |
54236 KB |
n=100 |
17 |
Correct |
60 ms |
54236 KB |
n=100 |
18 |
Correct |
51 ms |
54236 KB |
n=100 |
19 |
Correct |
55 ms |
54236 KB |
n=100 |
20 |
Correct |
60 ms |
54236 KB |
n=100 |
21 |
Correct |
52 ms |
54236 KB |
n=100 |
22 |
Correct |
53 ms |
54236 KB |
n=100 |
23 |
Correct |
53 ms |
54236 KB |
n=100 |
24 |
Correct |
51 ms |
54236 KB |
n=100 |
25 |
Correct |
56 ms |
54236 KB |
n=100 |
26 |
Correct |
51 ms |
54236 KB |
n=12 |
27 |
Correct |
51 ms |
54236 KB |
n=100 |
28 |
Correct |
55 ms |
54492 KB |
n=500 |
29 |
Correct |
61 ms |
55516 KB |
n=500 |
30 |
Correct |
58 ms |
55516 KB |
n=500 |
31 |
Correct |
59 ms |
55516 KB |
n=500 |
32 |
Correct |
60 ms |
55516 KB |
n=500 |
33 |
Correct |
57 ms |
55516 KB |
n=500 |
34 |
Correct |
58 ms |
55516 KB |
n=500 |
35 |
Correct |
59 ms |
55536 KB |
n=500 |
36 |
Correct |
52 ms |
55536 KB |
n=500 |
37 |
Correct |
52 ms |
55536 KB |
n=500 |
38 |
Correct |
53 ms |
55536 KB |
n=500 |
39 |
Correct |
52 ms |
55536 KB |
n=500 |
40 |
Correct |
53 ms |
55536 KB |
n=500 |
41 |
Correct |
54 ms |
55536 KB |
n=500 |
42 |
Correct |
55 ms |
55536 KB |
n=500 |
43 |
Correct |
57 ms |
55536 KB |
n=500 |
44 |
Correct |
55 ms |
55536 KB |
n=500 |
45 |
Correct |
55 ms |
55536 KB |
n=500 |
46 |
Correct |
57 ms |
55536 KB |
n=500 |
47 |
Correct |
58 ms |
55536 KB |
n=500 |
48 |
Correct |
55 ms |
55536 KB |
n=500 |
49 |
Correct |
60 ms |
55536 KB |
n=500 |
50 |
Correct |
57 ms |
55536 KB |
n=500 |
51 |
Correct |
59 ms |
55536 KB |
n=500 |
52 |
Correct |
60 ms |
55644 KB |
n=500 |
53 |
Correct |
59 ms |
55644 KB |
n=500 |
54 |
Correct |
59 ms |
55644 KB |
n=500 |
55 |
Correct |
55 ms |
55644 KB |
n=278 |
56 |
Correct |
69 ms |
55644 KB |
n=500 |
57 |
Correct |
61 ms |
55644 KB |
n=500 |
58 |
Correct |
55 ms |
55644 KB |
n=500 |
59 |
Correct |
70 ms |
56412 KB |
n=2000 |
60 |
Correct |
97 ms |
60764 KB |
n=2000 |
61 |
Correct |
122 ms |
60764 KB |
n=2000 |
62 |
Correct |
89 ms |
60764 KB |
n=2000 |
63 |
Correct |
72 ms |
60764 KB |
n=2000 |
64 |
Correct |
97 ms |
60764 KB |
n=2000 |
65 |
Correct |
70 ms |
60764 KB |
n=2000 |
66 |
Correct |
106 ms |
60764 KB |
n=2000 |
67 |
Correct |
77 ms |
60764 KB |
n=2000 |
68 |
Correct |
96 ms |
60764 KB |
n=2000 |
69 |
Correct |
63 ms |
60764 KB |
n=2000 |
70 |
Correct |
70 ms |
60764 KB |
n=2000 |
71 |
Correct |
67 ms |
60764 KB |
n=2000 |
72 |
Correct |
64 ms |
60764 KB |
n=2000 |
73 |
Correct |
66 ms |
60764 KB |
n=2000 |
74 |
Correct |
69 ms |
60764 KB |
n=1844 |
75 |
Correct |
64 ms |
60764 KB |
n=2000 |
76 |
Correct |
81 ms |
60764 KB |
n=2000 |
77 |
Correct |
75 ms |
60764 KB |
n=2000 |
78 |
Correct |
76 ms |
60764 KB |
n=2000 |
79 |
Correct |
72 ms |
60764 KB |
n=2000 |
80 |
Correct |
97 ms |
60764 KB |
n=2000 |
81 |
Correct |
89 ms |
60764 KB |
n=2000 |
82 |
Correct |
71 ms |
60764 KB |
n=2000 |
83 |
Correct |
96 ms |
60764 KB |
n=2000 |
84 |
Correct |
76 ms |
60764 KB |
n=2000 |
85 |
Correct |
100 ms |
60764 KB |
n=2000 |
86 |
Correct |
97 ms |
60764 KB |
n=2000 |
87 |
Correct |
78 ms |
60764 KB |
n=2000 |
88 |
Correct |
93 ms |
60764 KB |
n=2000 |
89 |
Correct |
94 ms |
60764 KB |
n=2000 |
90 |
Correct |
96 ms |
60764 KB |
n=2000 |
91 |
Correct |
65 ms |
60764 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
53624 KB |
n=5 |
2 |
Correct |
51 ms |
53892 KB |
n=100 |
3 |
Correct |
53 ms |
54128 KB |
n=100 |
4 |
Correct |
51 ms |
54128 KB |
n=100 |
5 |
Correct |
58 ms |
54128 KB |
n=100 |
6 |
Correct |
52 ms |
54128 KB |
n=100 |
7 |
Correct |
51 ms |
54128 KB |
n=100 |
8 |
Correct |
51 ms |
54128 KB |
n=100 |
9 |
Correct |
51 ms |
54128 KB |
n=100 |
10 |
Correct |
52 ms |
54236 KB |
n=100 |
11 |
Correct |
65 ms |
54236 KB |
n=100 |
12 |
Correct |
51 ms |
54236 KB |
n=100 |
13 |
Correct |
54 ms |
54236 KB |
n=100 |
14 |
Correct |
54 ms |
54236 KB |
n=100 |
15 |
Correct |
52 ms |
54236 KB |
n=100 |
16 |
Correct |
51 ms |
54236 KB |
n=100 |
17 |
Correct |
60 ms |
54236 KB |
n=100 |
18 |
Correct |
51 ms |
54236 KB |
n=100 |
19 |
Correct |
55 ms |
54236 KB |
n=100 |
20 |
Correct |
60 ms |
54236 KB |
n=100 |
21 |
Correct |
52 ms |
54236 KB |
n=100 |
22 |
Correct |
53 ms |
54236 KB |
n=100 |
23 |
Correct |
53 ms |
54236 KB |
n=100 |
24 |
Correct |
51 ms |
54236 KB |
n=100 |
25 |
Correct |
56 ms |
54236 KB |
n=100 |
26 |
Correct |
51 ms |
54236 KB |
n=12 |
27 |
Correct |
51 ms |
54236 KB |
n=100 |
28 |
Correct |
55 ms |
54492 KB |
n=500 |
29 |
Correct |
61 ms |
55516 KB |
n=500 |
30 |
Correct |
58 ms |
55516 KB |
n=500 |
31 |
Correct |
59 ms |
55516 KB |
n=500 |
32 |
Correct |
60 ms |
55516 KB |
n=500 |
33 |
Correct |
57 ms |
55516 KB |
n=500 |
34 |
Correct |
58 ms |
55516 KB |
n=500 |
35 |
Correct |
59 ms |
55536 KB |
n=500 |
36 |
Correct |
52 ms |
55536 KB |
n=500 |
37 |
Correct |
52 ms |
55536 KB |
n=500 |
38 |
Correct |
53 ms |
55536 KB |
n=500 |
39 |
Correct |
52 ms |
55536 KB |
n=500 |
40 |
Correct |
53 ms |
55536 KB |
n=500 |
41 |
Correct |
54 ms |
55536 KB |
n=500 |
42 |
Correct |
55 ms |
55536 KB |
n=500 |
43 |
Correct |
57 ms |
55536 KB |
n=500 |
44 |
Correct |
55 ms |
55536 KB |
n=500 |
45 |
Correct |
55 ms |
55536 KB |
n=500 |
46 |
Correct |
57 ms |
55536 KB |
n=500 |
47 |
Correct |
58 ms |
55536 KB |
n=500 |
48 |
Correct |
55 ms |
55536 KB |
n=500 |
49 |
Correct |
60 ms |
55536 KB |
n=500 |
50 |
Correct |
57 ms |
55536 KB |
n=500 |
51 |
Correct |
59 ms |
55536 KB |
n=500 |
52 |
Correct |
60 ms |
55644 KB |
n=500 |
53 |
Correct |
59 ms |
55644 KB |
n=500 |
54 |
Correct |
59 ms |
55644 KB |
n=500 |
55 |
Correct |
55 ms |
55644 KB |
n=278 |
56 |
Correct |
69 ms |
55644 KB |
n=500 |
57 |
Correct |
61 ms |
55644 KB |
n=500 |
58 |
Correct |
55 ms |
55644 KB |
n=500 |
59 |
Correct |
70 ms |
56412 KB |
n=2000 |
60 |
Correct |
97 ms |
60764 KB |
n=2000 |
61 |
Correct |
122 ms |
60764 KB |
n=2000 |
62 |
Correct |
89 ms |
60764 KB |
n=2000 |
63 |
Correct |
72 ms |
60764 KB |
n=2000 |
64 |
Correct |
97 ms |
60764 KB |
n=2000 |
65 |
Correct |
70 ms |
60764 KB |
n=2000 |
66 |
Correct |
106 ms |
60764 KB |
n=2000 |
67 |
Correct |
77 ms |
60764 KB |
n=2000 |
68 |
Correct |
96 ms |
60764 KB |
n=2000 |
69 |
Correct |
63 ms |
60764 KB |
n=2000 |
70 |
Correct |
70 ms |
60764 KB |
n=2000 |
71 |
Correct |
67 ms |
60764 KB |
n=2000 |
72 |
Correct |
64 ms |
60764 KB |
n=2000 |
73 |
Correct |
66 ms |
60764 KB |
n=2000 |
74 |
Correct |
69 ms |
60764 KB |
n=1844 |
75 |
Correct |
64 ms |
60764 KB |
n=2000 |
76 |
Correct |
81 ms |
60764 KB |
n=2000 |
77 |
Correct |
75 ms |
60764 KB |
n=2000 |
78 |
Correct |
76 ms |
60764 KB |
n=2000 |
79 |
Correct |
72 ms |
60764 KB |
n=2000 |
80 |
Correct |
97 ms |
60764 KB |
n=2000 |
81 |
Correct |
89 ms |
60764 KB |
n=2000 |
82 |
Correct |
71 ms |
60764 KB |
n=2000 |
83 |
Correct |
96 ms |
60764 KB |
n=2000 |
84 |
Correct |
76 ms |
60764 KB |
n=2000 |
85 |
Correct |
100 ms |
60764 KB |
n=2000 |
86 |
Correct |
97 ms |
60764 KB |
n=2000 |
87 |
Correct |
78 ms |
60764 KB |
n=2000 |
88 |
Correct |
93 ms |
60764 KB |
n=2000 |
89 |
Correct |
94 ms |
60764 KB |
n=2000 |
90 |
Correct |
96 ms |
60764 KB |
n=2000 |
91 |
Correct |
65 ms |
60764 KB |
n=2000 |
92 |
Execution timed out |
4075 ms |
262144 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |