#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5+2, LOG = 20;
vector<int> g[mxN];
multiset<int> st[4*mxN];
int up[LOG][mxN], dep[mxN], _m;
void build(const vector<int> &v, int i = 1, int l2 = 0, int r2 = _m - 1) {
if (l2 == r2) {
st[i].insert(v[l2]);
return;
}
int m2 = (l2 + r2)/2;
build(v, 2*i, l2, m2);
build(v, 2*i+1, m2+1, r2);
for (auto u : st[2*i]) {
st[i].insert(u);
}
for (auto u : st[2*i+1]) {
st[i].insert(u);
}
}
void update(int l, int x, int y, int i = 1, int l2 = 0, int r2 = _m - 1) {
st[i].erase(st[i].find(y));
st[i].insert(x);
if (l2 == r2) return;
int m2 = (l2 + r2)/2;
if (l <= m2) {
update(l, x, y, 2*i, l2, m2);
}
else {
update(l, x, y, 2*i+1, m2+1, r2);
}
}
int query(int l, int r, int u, int i = 1, int l2 = 0, int r2 = _m - 1) {
if (l <= l2 && r2 <= r) {
if (st[i].find(u) != st[i].end()) {
while (l2 != r2) {
int m2 = (l2 + r2)/2;
if (st[2*i].find(u) != st[2*i].end()) {
r2 = m2;
i = 2*i;
}
else {
l2 = m2+1;
i = 2*i+1;
}
}
return l2;
}
return -1;
}
int m2 = (l2 + r2)/2;
if (l <= m2) {
int x = query(l, r, u, 2*i, l2, m2);
if (x != -1) return x;
}
if (m2 + 1 <= r) {
int x = query(l, r, u, 2*i+1, m2+1, r2);
if (x != -1) return x;
}
return -1;
}
void dfs(int s, int e) {
for (auto u : g[s]) {
if (u == e) continue;
up[0][u] = s;
dep[u] = dep[s] + 1;
dfs(u, s);
}
}
int jump(int s, int k) {
for (int i = 0; i < LOG; ++i) {
if ((1 << i) & k) {
s = up[i][s];
}
}
return s;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
u = jump(u, dep[u] - dep[v]);
if (u == v) return u;
for (int i = LOG-1; i >= 0; --i) {
if (up[i][u] != up[i][v]) {
u = up[i][u], v = up[i][v];
}
}
return up[0][u];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q; _m = m-1;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v), g[v].push_back(u);
}
dfs(0, -1);
for (int i = 1; i < LOG; ++i) {
for (int j = 0; j < n; ++j) {
up[i][j] = up[i-1][up[i-1][j]];
}
}
int a[m];
multiset<int> ms[n];
vector<int> val(m);
for (int i = 0; i < n; ++i) ms[i].insert(1e9);
for (int i = 0; i < m; ++i) {
cin >> a[i];
a[i]--;
if (i) {
val[i-1] = lca(a[i-1], a[i]);
}
ms[a[i]].insert(i);
}
build(val);
while (q--) {
int t;
cin >> t;
if (t == 1) {
int l, u;
cin >> l >> u;
l--, u--;
if (l != m-1) {
int x = lca(u, a[l+1]);
update(l, x, val[l]);
val[l] = x;
}
if (l) {
int x = lca(u, a[l-1]);
update(l-1, x, val[l-1]);
val[l-1] = x;
}
ms[a[l]].erase(ms[a[l]].find(l));
ms[u].insert(l);
a[l] = u;
}
if (t == 2) {
int l, r, v;
cin >> l >> r >> v;
l--, r--, v--;
if (l == r) {
if (a[l] == v) {
cout << l+1 << ' ' << l+1 << '\n';
}
else {
cout << -1 << ' ' << -1 << '\n';
}
continue;
}
if (*ms[v].lower_bound(l) <= r) {
cout << *ms[v].lower_bound(l)+1 << ' ' << *ms[v].lower_bound(l)+1 << '\n';
}
else {
int x = query(l, r-1, v);
if (x != -1) {
cout << x+1 << ' ' << x+2 << '\n';
}
else {
cout << -1 << ' ' << -1 << '\n';
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
58972 KB |
n=5 |
2 |
Correct |
11 ms |
58932 KB |
n=100 |
3 |
Correct |
11 ms |
58972 KB |
n=100 |
4 |
Correct |
11 ms |
58972 KB |
n=100 |
5 |
Correct |
11 ms |
58972 KB |
n=100 |
6 |
Correct |
11 ms |
58972 KB |
n=100 |
7 |
Correct |
11 ms |
58808 KB |
n=100 |
8 |
Correct |
12 ms |
58972 KB |
n=100 |
9 |
Correct |
10 ms |
58972 KB |
n=100 |
10 |
Correct |
10 ms |
58972 KB |
n=100 |
11 |
Correct |
11 ms |
58972 KB |
n=100 |
12 |
Correct |
11 ms |
58968 KB |
n=100 |
13 |
Correct |
11 ms |
58972 KB |
n=100 |
14 |
Correct |
11 ms |
58972 KB |
n=100 |
15 |
Correct |
13 ms |
58972 KB |
n=100 |
16 |
Correct |
12 ms |
59224 KB |
n=100 |
17 |
Correct |
11 ms |
58968 KB |
n=100 |
18 |
Correct |
11 ms |
58972 KB |
n=100 |
19 |
Correct |
12 ms |
58968 KB |
n=100 |
20 |
Correct |
11 ms |
58972 KB |
n=100 |
21 |
Correct |
11 ms |
58972 KB |
n=100 |
22 |
Correct |
12 ms |
58968 KB |
n=100 |
23 |
Correct |
11 ms |
58980 KB |
n=100 |
24 |
Correct |
12 ms |
59052 KB |
n=100 |
25 |
Correct |
11 ms |
58972 KB |
n=100 |
26 |
Correct |
11 ms |
59172 KB |
n=12 |
27 |
Correct |
11 ms |
58972 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
58972 KB |
n=5 |
2 |
Correct |
11 ms |
58932 KB |
n=100 |
3 |
Correct |
11 ms |
58972 KB |
n=100 |
4 |
Correct |
11 ms |
58972 KB |
n=100 |
5 |
Correct |
11 ms |
58972 KB |
n=100 |
6 |
Correct |
11 ms |
58972 KB |
n=100 |
7 |
Correct |
11 ms |
58808 KB |
n=100 |
8 |
Correct |
12 ms |
58972 KB |
n=100 |
9 |
Correct |
10 ms |
58972 KB |
n=100 |
10 |
Correct |
10 ms |
58972 KB |
n=100 |
11 |
Correct |
11 ms |
58972 KB |
n=100 |
12 |
Correct |
11 ms |
58968 KB |
n=100 |
13 |
Correct |
11 ms |
58972 KB |
n=100 |
14 |
Correct |
11 ms |
58972 KB |
n=100 |
15 |
Correct |
13 ms |
58972 KB |
n=100 |
16 |
Correct |
12 ms |
59224 KB |
n=100 |
17 |
Correct |
11 ms |
58968 KB |
n=100 |
18 |
Correct |
11 ms |
58972 KB |
n=100 |
19 |
Correct |
12 ms |
58968 KB |
n=100 |
20 |
Correct |
11 ms |
58972 KB |
n=100 |
21 |
Correct |
11 ms |
58972 KB |
n=100 |
22 |
Correct |
12 ms |
58968 KB |
n=100 |
23 |
Correct |
11 ms |
58980 KB |
n=100 |
24 |
Correct |
12 ms |
59052 KB |
n=100 |
25 |
Correct |
11 ms |
58972 KB |
n=100 |
26 |
Correct |
11 ms |
59172 KB |
n=12 |
27 |
Correct |
11 ms |
58972 KB |
n=100 |
28 |
Correct |
11 ms |
59228 KB |
n=500 |
29 |
Correct |
13 ms |
59224 KB |
n=500 |
30 |
Correct |
12 ms |
59228 KB |
n=500 |
31 |
Correct |
12 ms |
59224 KB |
n=500 |
32 |
Correct |
12 ms |
59228 KB |
n=500 |
33 |
Correct |
13 ms |
59224 KB |
n=500 |
34 |
Correct |
12 ms |
59228 KB |
n=500 |
35 |
Correct |
12 ms |
59224 KB |
n=500 |
36 |
Correct |
11 ms |
59184 KB |
n=500 |
37 |
Correct |
11 ms |
59228 KB |
n=500 |
38 |
Correct |
11 ms |
59224 KB |
n=500 |
39 |
Correct |
12 ms |
59224 KB |
n=500 |
40 |
Correct |
12 ms |
59344 KB |
n=500 |
41 |
Correct |
11 ms |
59224 KB |
n=500 |
42 |
Correct |
13 ms |
59272 KB |
n=500 |
43 |
Correct |
12 ms |
59228 KB |
n=500 |
44 |
Correct |
12 ms |
59228 KB |
n=500 |
45 |
Correct |
13 ms |
59228 KB |
n=500 |
46 |
Correct |
12 ms |
59228 KB |
n=500 |
47 |
Correct |
12 ms |
59224 KB |
n=500 |
48 |
Correct |
12 ms |
59276 KB |
n=500 |
49 |
Correct |
13 ms |
59236 KB |
n=500 |
50 |
Correct |
13 ms |
59224 KB |
n=500 |
51 |
Correct |
12 ms |
59240 KB |
n=500 |
52 |
Correct |
12 ms |
59228 KB |
n=500 |
53 |
Correct |
12 ms |
59228 KB |
n=500 |
54 |
Correct |
12 ms |
59228 KB |
n=500 |
55 |
Correct |
12 ms |
59280 KB |
n=278 |
56 |
Correct |
13 ms |
59228 KB |
n=500 |
57 |
Correct |
13 ms |
59224 KB |
n=500 |
58 |
Correct |
11 ms |
59228 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
58972 KB |
n=5 |
2 |
Correct |
11 ms |
58932 KB |
n=100 |
3 |
Correct |
11 ms |
58972 KB |
n=100 |
4 |
Correct |
11 ms |
58972 KB |
n=100 |
5 |
Correct |
11 ms |
58972 KB |
n=100 |
6 |
Correct |
11 ms |
58972 KB |
n=100 |
7 |
Correct |
11 ms |
58808 KB |
n=100 |
8 |
Correct |
12 ms |
58972 KB |
n=100 |
9 |
Correct |
10 ms |
58972 KB |
n=100 |
10 |
Correct |
10 ms |
58972 KB |
n=100 |
11 |
Correct |
11 ms |
58972 KB |
n=100 |
12 |
Correct |
11 ms |
58968 KB |
n=100 |
13 |
Correct |
11 ms |
58972 KB |
n=100 |
14 |
Correct |
11 ms |
58972 KB |
n=100 |
15 |
Correct |
13 ms |
58972 KB |
n=100 |
16 |
Correct |
12 ms |
59224 KB |
n=100 |
17 |
Correct |
11 ms |
58968 KB |
n=100 |
18 |
Correct |
11 ms |
58972 KB |
n=100 |
19 |
Correct |
12 ms |
58968 KB |
n=100 |
20 |
Correct |
11 ms |
58972 KB |
n=100 |
21 |
Correct |
11 ms |
58972 KB |
n=100 |
22 |
Correct |
12 ms |
58968 KB |
n=100 |
23 |
Correct |
11 ms |
58980 KB |
n=100 |
24 |
Correct |
12 ms |
59052 KB |
n=100 |
25 |
Correct |
11 ms |
58972 KB |
n=100 |
26 |
Correct |
11 ms |
59172 KB |
n=12 |
27 |
Correct |
11 ms |
58972 KB |
n=100 |
28 |
Correct |
11 ms |
59228 KB |
n=500 |
29 |
Correct |
13 ms |
59224 KB |
n=500 |
30 |
Correct |
12 ms |
59228 KB |
n=500 |
31 |
Correct |
12 ms |
59224 KB |
n=500 |
32 |
Correct |
12 ms |
59228 KB |
n=500 |
33 |
Correct |
13 ms |
59224 KB |
n=500 |
34 |
Correct |
12 ms |
59228 KB |
n=500 |
35 |
Correct |
12 ms |
59224 KB |
n=500 |
36 |
Correct |
11 ms |
59184 KB |
n=500 |
37 |
Correct |
11 ms |
59228 KB |
n=500 |
38 |
Correct |
11 ms |
59224 KB |
n=500 |
39 |
Correct |
12 ms |
59224 KB |
n=500 |
40 |
Correct |
12 ms |
59344 KB |
n=500 |
41 |
Correct |
11 ms |
59224 KB |
n=500 |
42 |
Correct |
13 ms |
59272 KB |
n=500 |
43 |
Correct |
12 ms |
59228 KB |
n=500 |
44 |
Correct |
12 ms |
59228 KB |
n=500 |
45 |
Correct |
13 ms |
59228 KB |
n=500 |
46 |
Correct |
12 ms |
59228 KB |
n=500 |
47 |
Correct |
12 ms |
59224 KB |
n=500 |
48 |
Correct |
12 ms |
59276 KB |
n=500 |
49 |
Correct |
13 ms |
59236 KB |
n=500 |
50 |
Correct |
13 ms |
59224 KB |
n=500 |
51 |
Correct |
12 ms |
59240 KB |
n=500 |
52 |
Correct |
12 ms |
59228 KB |
n=500 |
53 |
Correct |
12 ms |
59228 KB |
n=500 |
54 |
Correct |
12 ms |
59228 KB |
n=500 |
55 |
Correct |
12 ms |
59280 KB |
n=278 |
56 |
Correct |
13 ms |
59228 KB |
n=500 |
57 |
Correct |
13 ms |
59224 KB |
n=500 |
58 |
Correct |
11 ms |
59228 KB |
n=500 |
59 |
Correct |
17 ms |
60508 KB |
n=2000 |
60 |
Correct |
19 ms |
60508 KB |
n=2000 |
61 |
Correct |
19 ms |
60508 KB |
n=2000 |
62 |
Correct |
18 ms |
60504 KB |
n=2000 |
63 |
Correct |
17 ms |
60508 KB |
n=2000 |
64 |
Correct |
18 ms |
60508 KB |
n=2000 |
65 |
Correct |
20 ms |
60764 KB |
n=2000 |
66 |
Correct |
18 ms |
60508 KB |
n=2000 |
67 |
Correct |
19 ms |
60544 KB |
n=2000 |
68 |
Correct |
22 ms |
60524 KB |
n=2000 |
69 |
Correct |
15 ms |
60416 KB |
n=2000 |
70 |
Correct |
14 ms |
60504 KB |
n=2000 |
71 |
Correct |
15 ms |
60508 KB |
n=2000 |
72 |
Correct |
15 ms |
60504 KB |
n=2000 |
73 |
Correct |
15 ms |
60508 KB |
n=2000 |
74 |
Correct |
16 ms |
60352 KB |
n=1844 |
75 |
Correct |
15 ms |
60508 KB |
n=2000 |
76 |
Correct |
18 ms |
60508 KB |
n=2000 |
77 |
Correct |
20 ms |
60504 KB |
n=2000 |
78 |
Correct |
18 ms |
60504 KB |
n=2000 |
79 |
Correct |
18 ms |
60508 KB |
n=2000 |
80 |
Correct |
18 ms |
60508 KB |
n=2000 |
81 |
Correct |
19 ms |
60508 KB |
n=2000 |
82 |
Correct |
17 ms |
60508 KB |
n=2000 |
83 |
Correct |
20 ms |
60508 KB |
n=2000 |
84 |
Correct |
21 ms |
60508 KB |
n=2000 |
85 |
Correct |
20 ms |
60764 KB |
n=2000 |
86 |
Correct |
19 ms |
60508 KB |
n=2000 |
87 |
Correct |
21 ms |
60508 KB |
n=2000 |
88 |
Correct |
20 ms |
60508 KB |
n=2000 |
89 |
Correct |
19 ms |
60508 KB |
n=2000 |
90 |
Correct |
18 ms |
60508 KB |
n=2000 |
91 |
Correct |
15 ms |
60508 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
58972 KB |
n=5 |
2 |
Correct |
11 ms |
58932 KB |
n=100 |
3 |
Correct |
11 ms |
58972 KB |
n=100 |
4 |
Correct |
11 ms |
58972 KB |
n=100 |
5 |
Correct |
11 ms |
58972 KB |
n=100 |
6 |
Correct |
11 ms |
58972 KB |
n=100 |
7 |
Correct |
11 ms |
58808 KB |
n=100 |
8 |
Correct |
12 ms |
58972 KB |
n=100 |
9 |
Correct |
10 ms |
58972 KB |
n=100 |
10 |
Correct |
10 ms |
58972 KB |
n=100 |
11 |
Correct |
11 ms |
58972 KB |
n=100 |
12 |
Correct |
11 ms |
58968 KB |
n=100 |
13 |
Correct |
11 ms |
58972 KB |
n=100 |
14 |
Correct |
11 ms |
58972 KB |
n=100 |
15 |
Correct |
13 ms |
58972 KB |
n=100 |
16 |
Correct |
12 ms |
59224 KB |
n=100 |
17 |
Correct |
11 ms |
58968 KB |
n=100 |
18 |
Correct |
11 ms |
58972 KB |
n=100 |
19 |
Correct |
12 ms |
58968 KB |
n=100 |
20 |
Correct |
11 ms |
58972 KB |
n=100 |
21 |
Correct |
11 ms |
58972 KB |
n=100 |
22 |
Correct |
12 ms |
58968 KB |
n=100 |
23 |
Correct |
11 ms |
58980 KB |
n=100 |
24 |
Correct |
12 ms |
59052 KB |
n=100 |
25 |
Correct |
11 ms |
58972 KB |
n=100 |
26 |
Correct |
11 ms |
59172 KB |
n=12 |
27 |
Correct |
11 ms |
58972 KB |
n=100 |
28 |
Correct |
11 ms |
59228 KB |
n=500 |
29 |
Correct |
13 ms |
59224 KB |
n=500 |
30 |
Correct |
12 ms |
59228 KB |
n=500 |
31 |
Correct |
12 ms |
59224 KB |
n=500 |
32 |
Correct |
12 ms |
59228 KB |
n=500 |
33 |
Correct |
13 ms |
59224 KB |
n=500 |
34 |
Correct |
12 ms |
59228 KB |
n=500 |
35 |
Correct |
12 ms |
59224 KB |
n=500 |
36 |
Correct |
11 ms |
59184 KB |
n=500 |
37 |
Correct |
11 ms |
59228 KB |
n=500 |
38 |
Correct |
11 ms |
59224 KB |
n=500 |
39 |
Correct |
12 ms |
59224 KB |
n=500 |
40 |
Correct |
12 ms |
59344 KB |
n=500 |
41 |
Correct |
11 ms |
59224 KB |
n=500 |
42 |
Correct |
13 ms |
59272 KB |
n=500 |
43 |
Correct |
12 ms |
59228 KB |
n=500 |
44 |
Correct |
12 ms |
59228 KB |
n=500 |
45 |
Correct |
13 ms |
59228 KB |
n=500 |
46 |
Correct |
12 ms |
59228 KB |
n=500 |
47 |
Correct |
12 ms |
59224 KB |
n=500 |
48 |
Correct |
12 ms |
59276 KB |
n=500 |
49 |
Correct |
13 ms |
59236 KB |
n=500 |
50 |
Correct |
13 ms |
59224 KB |
n=500 |
51 |
Correct |
12 ms |
59240 KB |
n=500 |
52 |
Correct |
12 ms |
59228 KB |
n=500 |
53 |
Correct |
12 ms |
59228 KB |
n=500 |
54 |
Correct |
12 ms |
59228 KB |
n=500 |
55 |
Correct |
12 ms |
59280 KB |
n=278 |
56 |
Correct |
13 ms |
59228 KB |
n=500 |
57 |
Correct |
13 ms |
59224 KB |
n=500 |
58 |
Correct |
11 ms |
59228 KB |
n=500 |
59 |
Correct |
17 ms |
60508 KB |
n=2000 |
60 |
Correct |
19 ms |
60508 KB |
n=2000 |
61 |
Correct |
19 ms |
60508 KB |
n=2000 |
62 |
Correct |
18 ms |
60504 KB |
n=2000 |
63 |
Correct |
17 ms |
60508 KB |
n=2000 |
64 |
Correct |
18 ms |
60508 KB |
n=2000 |
65 |
Correct |
20 ms |
60764 KB |
n=2000 |
66 |
Correct |
18 ms |
60508 KB |
n=2000 |
67 |
Correct |
19 ms |
60544 KB |
n=2000 |
68 |
Correct |
22 ms |
60524 KB |
n=2000 |
69 |
Correct |
15 ms |
60416 KB |
n=2000 |
70 |
Correct |
14 ms |
60504 KB |
n=2000 |
71 |
Correct |
15 ms |
60508 KB |
n=2000 |
72 |
Correct |
15 ms |
60504 KB |
n=2000 |
73 |
Correct |
15 ms |
60508 KB |
n=2000 |
74 |
Correct |
16 ms |
60352 KB |
n=1844 |
75 |
Correct |
15 ms |
60508 KB |
n=2000 |
76 |
Correct |
18 ms |
60508 KB |
n=2000 |
77 |
Correct |
20 ms |
60504 KB |
n=2000 |
78 |
Correct |
18 ms |
60504 KB |
n=2000 |
79 |
Correct |
18 ms |
60508 KB |
n=2000 |
80 |
Correct |
18 ms |
60508 KB |
n=2000 |
81 |
Correct |
19 ms |
60508 KB |
n=2000 |
82 |
Correct |
17 ms |
60508 KB |
n=2000 |
83 |
Correct |
20 ms |
60508 KB |
n=2000 |
84 |
Correct |
21 ms |
60508 KB |
n=2000 |
85 |
Correct |
20 ms |
60764 KB |
n=2000 |
86 |
Correct |
19 ms |
60508 KB |
n=2000 |
87 |
Correct |
21 ms |
60508 KB |
n=2000 |
88 |
Correct |
20 ms |
60508 KB |
n=2000 |
89 |
Correct |
19 ms |
60508 KB |
n=2000 |
90 |
Correct |
18 ms |
60508 KB |
n=2000 |
91 |
Correct |
15 ms |
60508 KB |
n=2000 |
92 |
Runtime error |
466 ms |
262144 KB |
Execution killed with signal 9 |
93 |
Halted |
0 ms |
0 KB |
- |