#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
const int N = 2e5+5;
int n, m, q;
vector<int> adj[N];
int a[N];
int cnt = 0;
int be[N], en[N], h[N], par[N];
pair<int, int> b[N*2];
pair<int, int> rmq[20][N*2];
struct LCANode {
int all, left, right, lpos, rpos;
int be, en;
};
LCANode it[N*4], res[N*4];
int ans_x, ans_y;
void dfs(int u) {
b[++cnt] = mp(h[u], u);
be[u] = en[u] = cnt;
for (int &v: adj[u]) if (v != par[u]) {
par[v] = u;
h[v] = h[u] + 1;
dfs(v);
b[++cnt] = mp(h[u], u);
en[u] = cnt;
}
}
void build_rmq() {
for (int i = 1; i <= cnt; ++i) {
rmq[0][i] = b[i];
}
for (int j = 1; (1 << j) <= cnt; ++j) {
for (int i = 1; i + (1 << j) <= cnt; ++i) {
rmq[j][i] = min(rmq[j-1][i], rmq[j-1][i + (1 << j-1)]);
}
}
}
int get_lca(int &u, int &v) {
int l = be[u], r = be[v];
if (l > r) swap(l, r);
int k = log2(r-l+1);
auto mi = min(rmq[k][l], rmq[k][r - (1<<k) +1]);
return mi.second;
}
void build(int k, int l, int r) {
if (l == r) {
it[k].all = a[r];
it[k].be = be[a[r]];
it[k].en = en[a[r]];
return;
}
int m = (l + r) >> 1;
build(k*2, l, m);
build(k*2+1, m+1, r);
it[k].all = get_lca(it[k*2].all, it[k*2+1].all);
it[k].be = min(it[k*2].be, it[k*2+1].be);
it[k].en = max(it[k*2].en, it[k*2+1].en);
}
void update(int k, int l, int r, int &i, int &u, int &old_u) {
if (l == r) {
it[k].all = u;
it[k].be = be[u];
it[k].en = en[u];
return;
}
int m = (l + r) >> 1;
if (i <= m) update(k*2, l, m, i, u, old_u);
else update(k*2+1, m+1, r, i, u, old_u);
it[k].all = get_lca(it[k*2].all, it[k*2+1].all);
it[k].be = min(it[k*2].be, it[k*2+1].be);
it[k].en = max(it[k*2].en, it[k*2+1].en);
}
void merge_node(LCANode &node, LCANode &lnode, LCANode &rnode, int u) {
if (lnode.all && rnode.all) {
int lca = get_lca(lnode.all, rnode.all);
node.all = node.left = node.right = lca;
node.lpos = rnode.lpos;
node.rpos = lnode.rpos;
return;
}
node.all = 0;
node.left = lnode.left;
node.right = rnode.right;
node.lpos = lnode.lpos;
node.rpos = rnode.rpos;
if (lnode.all && rnode.left) {
node.left = get_lca(lnode.all, rnode.left);
node.lpos = rnode.lpos;
}
if (rnode.all && lnode.right) {
node.right = get_lca(rnode.all, lnode.right);
node.rpos = lnode.rpos;
}
if (lnode.right && rnode.left) {
int lca = get_lca(lnode.right, rnode.left);
if (lca == u) {
ans_x = lnode.rpos;
ans_y = rnode.lpos;
}
}
}
void get(int k, int l, int r, int &ql, int &qr, int &u) {
if (qr < l || r < ql || it[k].en < be[u] || en[u] < it[k].be) {
// res[k] = LCANode{0, 0, 0, l-1, r+1};
res[k].all = 0;
res[k].left = 0;
res[k].right = 0;
res[k].lpos = l-1;
res[k].rpos = r+1;
return;
}
if (ql <= l && r <= qr && get_lca(it[k].all, u) == u) {
// cout << "a " << l << ' ' << r << ' ' << it[k].all << endl;
int lca = it[k].all;
if (lca == u) {
ans_x = l;
ans_y = r;
}
// res[k] = LCANode{lca, lca, lca, r, l};
res[k].all = res[k].left = res[k].right = lca;
res[k].lpos = r;
res[k].rpos = l;
return;
}
int m = (l + r) >> 1;
get(k*2, l, m, ql, qr, u);
if (ans_x) return;
get(k*2+1, m+1, r, ql, qr, u);
if (ans_x) return;
merge_node(res[k], res[k*2], res[k*2+1], u);
// cout << l << ' ' << r << ' ' << node.all << ' ' << node.left << ' ' << node.right << endl;
// cout << lnode.all << ' ' << lnode.left << ' ' << lnode.right << endl;
// cout << rnode.all << ' ' << rnode.left << ' ' << rnode.right << endl;
}
int main()
{
// freopen("a.inp", "r", stdin);
cin >> n >> m >> q;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= m; ++i) {
cin >> a[i];
}
dfs(1);
build_rmq();
// for (int i = 1; i <= cnt; ++i) cout << b[i].second << ' ';
// cout << endl;
// cout << "lca " << get_lca(4, 5) << endl;
build(1, 1, m);
for (int i = 1; i <= q; ++i) {
int t;
cin >> t;
if (t == 1) {
int pos, u;
cin >> pos >> u;
update(1, 1, m, pos, u, a[pos]);
a[pos] = u;
} else {
int l, r, u;
cin >> l >> r >> u;
ans_x = ans_y = 0;
get(1, 1, m, l, r, u);
if (ans_x) cout << ans_x << ' ' << ans_y << '\n';
else cout << -1 << ' ' << -1 << '\n';
}
}
return 0;
}
Compilation message
treearray.cpp: In function 'void build_rmq()':
treearray.cpp:46:62: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
46 | rmq[j][i] = min(rmq[j-1][i], rmq[j-1][i + (1 << j-1)]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
n=5 |
2 |
Correct |
4 ms |
27996 KB |
n=100 |
3 |
Correct |
4 ms |
27996 KB |
n=100 |
4 |
Correct |
4 ms |
27996 KB |
n=100 |
5 |
Correct |
4 ms |
27996 KB |
n=100 |
6 |
Correct |
4 ms |
28088 KB |
n=100 |
7 |
Correct |
5 ms |
27992 KB |
n=100 |
8 |
Correct |
4 ms |
28084 KB |
n=100 |
9 |
Correct |
4 ms |
27996 KB |
n=100 |
10 |
Correct |
4 ms |
27996 KB |
n=100 |
11 |
Correct |
4 ms |
27996 KB |
n=100 |
12 |
Correct |
5 ms |
27996 KB |
n=100 |
13 |
Correct |
5 ms |
27996 KB |
n=100 |
14 |
Correct |
4 ms |
27996 KB |
n=100 |
15 |
Correct |
4 ms |
27996 KB |
n=100 |
16 |
Correct |
4 ms |
27996 KB |
n=100 |
17 |
Correct |
4 ms |
27996 KB |
n=100 |
18 |
Correct |
5 ms |
27996 KB |
n=100 |
19 |
Correct |
4 ms |
27996 KB |
n=100 |
20 |
Correct |
4 ms |
27996 KB |
n=100 |
21 |
Correct |
4 ms |
27996 KB |
n=100 |
22 |
Correct |
4 ms |
27992 KB |
n=100 |
23 |
Correct |
5 ms |
27992 KB |
n=100 |
24 |
Correct |
4 ms |
27996 KB |
n=100 |
25 |
Correct |
4 ms |
27996 KB |
n=100 |
26 |
Correct |
3 ms |
21852 KB |
n=12 |
27 |
Correct |
4 ms |
27996 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
n=5 |
2 |
Correct |
4 ms |
27996 KB |
n=100 |
3 |
Correct |
4 ms |
27996 KB |
n=100 |
4 |
Correct |
4 ms |
27996 KB |
n=100 |
5 |
Correct |
4 ms |
27996 KB |
n=100 |
6 |
Correct |
4 ms |
28088 KB |
n=100 |
7 |
Correct |
5 ms |
27992 KB |
n=100 |
8 |
Correct |
4 ms |
28084 KB |
n=100 |
9 |
Correct |
4 ms |
27996 KB |
n=100 |
10 |
Correct |
4 ms |
27996 KB |
n=100 |
11 |
Correct |
4 ms |
27996 KB |
n=100 |
12 |
Correct |
5 ms |
27996 KB |
n=100 |
13 |
Correct |
5 ms |
27996 KB |
n=100 |
14 |
Correct |
4 ms |
27996 KB |
n=100 |
15 |
Correct |
4 ms |
27996 KB |
n=100 |
16 |
Correct |
4 ms |
27996 KB |
n=100 |
17 |
Correct |
4 ms |
27996 KB |
n=100 |
18 |
Correct |
5 ms |
27996 KB |
n=100 |
19 |
Correct |
4 ms |
27996 KB |
n=100 |
20 |
Correct |
4 ms |
27996 KB |
n=100 |
21 |
Correct |
4 ms |
27996 KB |
n=100 |
22 |
Correct |
4 ms |
27992 KB |
n=100 |
23 |
Correct |
5 ms |
27992 KB |
n=100 |
24 |
Correct |
4 ms |
27996 KB |
n=100 |
25 |
Correct |
4 ms |
27996 KB |
n=100 |
26 |
Correct |
3 ms |
21852 KB |
n=12 |
27 |
Correct |
4 ms |
27996 KB |
n=100 |
28 |
Correct |
5 ms |
32348 KB |
n=500 |
29 |
Correct |
6 ms |
32344 KB |
n=500 |
30 |
Correct |
6 ms |
32348 KB |
n=500 |
31 |
Correct |
6 ms |
32348 KB |
n=500 |
32 |
Correct |
5 ms |
32088 KB |
n=500 |
33 |
Correct |
7 ms |
32348 KB |
n=500 |
34 |
Correct |
5 ms |
32344 KB |
n=500 |
35 |
Correct |
6 ms |
32348 KB |
n=500 |
36 |
Correct |
11 ms |
32092 KB |
n=500 |
37 |
Correct |
11 ms |
32092 KB |
n=500 |
38 |
Correct |
11 ms |
32092 KB |
n=500 |
39 |
Correct |
8 ms |
32092 KB |
n=500 |
40 |
Correct |
11 ms |
32092 KB |
n=500 |
41 |
Correct |
8 ms |
32092 KB |
n=500 |
42 |
Correct |
7 ms |
32092 KB |
n=500 |
43 |
Correct |
8 ms |
32092 KB |
n=500 |
44 |
Correct |
7 ms |
32092 KB |
n=500 |
45 |
Correct |
5 ms |
32092 KB |
n=500 |
46 |
Correct |
6 ms |
32348 KB |
n=500 |
47 |
Correct |
6 ms |
32348 KB |
n=500 |
48 |
Correct |
5 ms |
32300 KB |
n=500 |
49 |
Correct |
6 ms |
32348 KB |
n=500 |
50 |
Correct |
6 ms |
32348 KB |
n=500 |
51 |
Correct |
8 ms |
32348 KB |
n=500 |
52 |
Correct |
7 ms |
32348 KB |
n=500 |
53 |
Correct |
6 ms |
32348 KB |
n=500 |
54 |
Correct |
7 ms |
32348 KB |
n=500 |
55 |
Correct |
5 ms |
32344 KB |
n=278 |
56 |
Correct |
5 ms |
32348 KB |
n=500 |
57 |
Correct |
5 ms |
32344 KB |
n=500 |
58 |
Correct |
7 ms |
32276 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
n=5 |
2 |
Correct |
4 ms |
27996 KB |
n=100 |
3 |
Correct |
4 ms |
27996 KB |
n=100 |
4 |
Correct |
4 ms |
27996 KB |
n=100 |
5 |
Correct |
4 ms |
27996 KB |
n=100 |
6 |
Correct |
4 ms |
28088 KB |
n=100 |
7 |
Correct |
5 ms |
27992 KB |
n=100 |
8 |
Correct |
4 ms |
28084 KB |
n=100 |
9 |
Correct |
4 ms |
27996 KB |
n=100 |
10 |
Correct |
4 ms |
27996 KB |
n=100 |
11 |
Correct |
4 ms |
27996 KB |
n=100 |
12 |
Correct |
5 ms |
27996 KB |
n=100 |
13 |
Correct |
5 ms |
27996 KB |
n=100 |
14 |
Correct |
4 ms |
27996 KB |
n=100 |
15 |
Correct |
4 ms |
27996 KB |
n=100 |
16 |
Correct |
4 ms |
27996 KB |
n=100 |
17 |
Correct |
4 ms |
27996 KB |
n=100 |
18 |
Correct |
5 ms |
27996 KB |
n=100 |
19 |
Correct |
4 ms |
27996 KB |
n=100 |
20 |
Correct |
4 ms |
27996 KB |
n=100 |
21 |
Correct |
4 ms |
27996 KB |
n=100 |
22 |
Correct |
4 ms |
27992 KB |
n=100 |
23 |
Correct |
5 ms |
27992 KB |
n=100 |
24 |
Correct |
4 ms |
27996 KB |
n=100 |
25 |
Correct |
4 ms |
27996 KB |
n=100 |
26 |
Correct |
3 ms |
21852 KB |
n=12 |
27 |
Correct |
4 ms |
27996 KB |
n=100 |
28 |
Correct |
5 ms |
32348 KB |
n=500 |
29 |
Correct |
6 ms |
32344 KB |
n=500 |
30 |
Correct |
6 ms |
32348 KB |
n=500 |
31 |
Correct |
6 ms |
32348 KB |
n=500 |
32 |
Correct |
5 ms |
32088 KB |
n=500 |
33 |
Correct |
7 ms |
32348 KB |
n=500 |
34 |
Correct |
5 ms |
32344 KB |
n=500 |
35 |
Correct |
6 ms |
32348 KB |
n=500 |
36 |
Correct |
11 ms |
32092 KB |
n=500 |
37 |
Correct |
11 ms |
32092 KB |
n=500 |
38 |
Correct |
11 ms |
32092 KB |
n=500 |
39 |
Correct |
8 ms |
32092 KB |
n=500 |
40 |
Correct |
11 ms |
32092 KB |
n=500 |
41 |
Correct |
8 ms |
32092 KB |
n=500 |
42 |
Correct |
7 ms |
32092 KB |
n=500 |
43 |
Correct |
8 ms |
32092 KB |
n=500 |
44 |
Correct |
7 ms |
32092 KB |
n=500 |
45 |
Correct |
5 ms |
32092 KB |
n=500 |
46 |
Correct |
6 ms |
32348 KB |
n=500 |
47 |
Correct |
6 ms |
32348 KB |
n=500 |
48 |
Correct |
5 ms |
32300 KB |
n=500 |
49 |
Correct |
6 ms |
32348 KB |
n=500 |
50 |
Correct |
6 ms |
32348 KB |
n=500 |
51 |
Correct |
8 ms |
32348 KB |
n=500 |
52 |
Correct |
7 ms |
32348 KB |
n=500 |
53 |
Correct |
6 ms |
32348 KB |
n=500 |
54 |
Correct |
7 ms |
32348 KB |
n=500 |
55 |
Correct |
5 ms |
32344 KB |
n=278 |
56 |
Correct |
5 ms |
32348 KB |
n=500 |
57 |
Correct |
5 ms |
32344 KB |
n=500 |
58 |
Correct |
7 ms |
32276 KB |
n=500 |
59 |
Correct |
9 ms |
36444 KB |
n=2000 |
60 |
Correct |
23 ms |
38744 KB |
n=2000 |
61 |
Correct |
24 ms |
38744 KB |
n=2000 |
62 |
Correct |
17 ms |
38744 KB |
n=2000 |
63 |
Correct |
9 ms |
36444 KB |
n=2000 |
64 |
Correct |
20 ms |
38748 KB |
n=2000 |
65 |
Correct |
8 ms |
36440 KB |
n=2000 |
66 |
Correct |
24 ms |
38748 KB |
n=2000 |
67 |
Correct |
10 ms |
38492 KB |
n=2000 |
68 |
Correct |
21 ms |
38748 KB |
n=2000 |
69 |
Correct |
90 ms |
36536 KB |
n=2000 |
70 |
Correct |
89 ms |
36548 KB |
n=2000 |
71 |
Correct |
95 ms |
36688 KB |
n=2000 |
72 |
Correct |
50 ms |
36440 KB |
n=2000 |
73 |
Correct |
52 ms |
36444 KB |
n=2000 |
74 |
Correct |
8 ms |
36440 KB |
n=1844 |
75 |
Correct |
50 ms |
36444 KB |
n=2000 |
76 |
Correct |
47 ms |
36440 KB |
n=2000 |
77 |
Correct |
45 ms |
36444 KB |
n=2000 |
78 |
Correct |
49 ms |
36532 KB |
n=2000 |
79 |
Correct |
8 ms |
36444 KB |
n=2000 |
80 |
Correct |
22 ms |
38832 KB |
n=2000 |
81 |
Correct |
19 ms |
38744 KB |
n=2000 |
82 |
Correct |
9 ms |
38492 KB |
n=2000 |
83 |
Correct |
24 ms |
38808 KB |
n=2000 |
84 |
Correct |
19 ms |
38492 KB |
n=2000 |
85 |
Correct |
28 ms |
38748 KB |
n=2000 |
86 |
Correct |
29 ms |
38748 KB |
n=2000 |
87 |
Correct |
20 ms |
38492 KB |
n=2000 |
88 |
Correct |
10 ms |
36444 KB |
n=2000 |
89 |
Correct |
10 ms |
36664 KB |
n=2000 |
90 |
Correct |
10 ms |
36444 KB |
n=2000 |
91 |
Correct |
47 ms |
36444 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
n=5 |
2 |
Correct |
4 ms |
27996 KB |
n=100 |
3 |
Correct |
4 ms |
27996 KB |
n=100 |
4 |
Correct |
4 ms |
27996 KB |
n=100 |
5 |
Correct |
4 ms |
27996 KB |
n=100 |
6 |
Correct |
4 ms |
28088 KB |
n=100 |
7 |
Correct |
5 ms |
27992 KB |
n=100 |
8 |
Correct |
4 ms |
28084 KB |
n=100 |
9 |
Correct |
4 ms |
27996 KB |
n=100 |
10 |
Correct |
4 ms |
27996 KB |
n=100 |
11 |
Correct |
4 ms |
27996 KB |
n=100 |
12 |
Correct |
5 ms |
27996 KB |
n=100 |
13 |
Correct |
5 ms |
27996 KB |
n=100 |
14 |
Correct |
4 ms |
27996 KB |
n=100 |
15 |
Correct |
4 ms |
27996 KB |
n=100 |
16 |
Correct |
4 ms |
27996 KB |
n=100 |
17 |
Correct |
4 ms |
27996 KB |
n=100 |
18 |
Correct |
5 ms |
27996 KB |
n=100 |
19 |
Correct |
4 ms |
27996 KB |
n=100 |
20 |
Correct |
4 ms |
27996 KB |
n=100 |
21 |
Correct |
4 ms |
27996 KB |
n=100 |
22 |
Correct |
4 ms |
27992 KB |
n=100 |
23 |
Correct |
5 ms |
27992 KB |
n=100 |
24 |
Correct |
4 ms |
27996 KB |
n=100 |
25 |
Correct |
4 ms |
27996 KB |
n=100 |
26 |
Correct |
3 ms |
21852 KB |
n=12 |
27 |
Correct |
4 ms |
27996 KB |
n=100 |
28 |
Correct |
5 ms |
32348 KB |
n=500 |
29 |
Correct |
6 ms |
32344 KB |
n=500 |
30 |
Correct |
6 ms |
32348 KB |
n=500 |
31 |
Correct |
6 ms |
32348 KB |
n=500 |
32 |
Correct |
5 ms |
32088 KB |
n=500 |
33 |
Correct |
7 ms |
32348 KB |
n=500 |
34 |
Correct |
5 ms |
32344 KB |
n=500 |
35 |
Correct |
6 ms |
32348 KB |
n=500 |
36 |
Correct |
11 ms |
32092 KB |
n=500 |
37 |
Correct |
11 ms |
32092 KB |
n=500 |
38 |
Correct |
11 ms |
32092 KB |
n=500 |
39 |
Correct |
8 ms |
32092 KB |
n=500 |
40 |
Correct |
11 ms |
32092 KB |
n=500 |
41 |
Correct |
8 ms |
32092 KB |
n=500 |
42 |
Correct |
7 ms |
32092 KB |
n=500 |
43 |
Correct |
8 ms |
32092 KB |
n=500 |
44 |
Correct |
7 ms |
32092 KB |
n=500 |
45 |
Correct |
5 ms |
32092 KB |
n=500 |
46 |
Correct |
6 ms |
32348 KB |
n=500 |
47 |
Correct |
6 ms |
32348 KB |
n=500 |
48 |
Correct |
5 ms |
32300 KB |
n=500 |
49 |
Correct |
6 ms |
32348 KB |
n=500 |
50 |
Correct |
6 ms |
32348 KB |
n=500 |
51 |
Correct |
8 ms |
32348 KB |
n=500 |
52 |
Correct |
7 ms |
32348 KB |
n=500 |
53 |
Correct |
6 ms |
32348 KB |
n=500 |
54 |
Correct |
7 ms |
32348 KB |
n=500 |
55 |
Correct |
5 ms |
32344 KB |
n=278 |
56 |
Correct |
5 ms |
32348 KB |
n=500 |
57 |
Correct |
5 ms |
32344 KB |
n=500 |
58 |
Correct |
7 ms |
32276 KB |
n=500 |
59 |
Correct |
9 ms |
36444 KB |
n=2000 |
60 |
Correct |
23 ms |
38744 KB |
n=2000 |
61 |
Correct |
24 ms |
38744 KB |
n=2000 |
62 |
Correct |
17 ms |
38744 KB |
n=2000 |
63 |
Correct |
9 ms |
36444 KB |
n=2000 |
64 |
Correct |
20 ms |
38748 KB |
n=2000 |
65 |
Correct |
8 ms |
36440 KB |
n=2000 |
66 |
Correct |
24 ms |
38748 KB |
n=2000 |
67 |
Correct |
10 ms |
38492 KB |
n=2000 |
68 |
Correct |
21 ms |
38748 KB |
n=2000 |
69 |
Correct |
90 ms |
36536 KB |
n=2000 |
70 |
Correct |
89 ms |
36548 KB |
n=2000 |
71 |
Correct |
95 ms |
36688 KB |
n=2000 |
72 |
Correct |
50 ms |
36440 KB |
n=2000 |
73 |
Correct |
52 ms |
36444 KB |
n=2000 |
74 |
Correct |
8 ms |
36440 KB |
n=1844 |
75 |
Correct |
50 ms |
36444 KB |
n=2000 |
76 |
Correct |
47 ms |
36440 KB |
n=2000 |
77 |
Correct |
45 ms |
36444 KB |
n=2000 |
78 |
Correct |
49 ms |
36532 KB |
n=2000 |
79 |
Correct |
8 ms |
36444 KB |
n=2000 |
80 |
Correct |
22 ms |
38832 KB |
n=2000 |
81 |
Correct |
19 ms |
38744 KB |
n=2000 |
82 |
Correct |
9 ms |
38492 KB |
n=2000 |
83 |
Correct |
24 ms |
38808 KB |
n=2000 |
84 |
Correct |
19 ms |
38492 KB |
n=2000 |
85 |
Correct |
28 ms |
38748 KB |
n=2000 |
86 |
Correct |
29 ms |
38748 KB |
n=2000 |
87 |
Correct |
20 ms |
38492 KB |
n=2000 |
88 |
Correct |
10 ms |
36444 KB |
n=2000 |
89 |
Correct |
10 ms |
36664 KB |
n=2000 |
90 |
Correct |
10 ms |
36444 KB |
n=2000 |
91 |
Correct |
47 ms |
36444 KB |
n=2000 |
92 |
Correct |
624 ms |
112432 KB |
n=200000 |
93 |
Runtime error |
506 ms |
251868 KB |
Execution killed with signal 11 |
94 |
Halted |
0 ms |
0 KB |
- |