#include <bits/stdc++.h>
#define y1 theboatman
#define make_struct(args...) {args}
using namespace std;
struct Tree {
multiset <int> s;
void modify(int pos, int val) {
if (val == 1) {
s.insert(pos);
}
else {
s.erase(s.find(pos));
}
}
int get(int l, int r) {
auto it = s.lower_bound(l);
if (it == s.end()) {
return int(1e9);
}
else {
return *it;
}
}
};
struct Lca {
int n, bit, timer;
vector <int> tin, tout;
vector <vector <int> > g, up;
void dfs(int v, int from) {
tin[v] = timer++;
up[v][0] = from;
for(int i = 1; i < bit; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(auto to : g[v]) {
if (to != from) {
dfs(to, v);
}
}
tout[v] = timer++;
}
void build(vector <vector <int> > &temp_graph) {
g = temp_graph;
n = g.size();
bit = 0;
while((1 << bit) <= n) {
bit++;
}
bit--;
tin.resize(n);
tout.resize(n);
up.resize(n, vector <int> (bit, 1));
timer = 0;
dfs(1, 1);
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int get(int a, int b) {
if (upper(a, b)) {
return a;
}
if (upper(b, a)) {
return b;
}
for(int i = bit - 1; i > -1; i--) {
if (!upper(up[a][i], b)) {
a = up[a][i];
}
}
return up[a][0];
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector <vector <int> > g(n + 1);
for(int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
vector <int> a(m + 1);
for(int i = 1; i <= m; i++) {
cin >> a[i];
}
Lca lca;
lca.build(g);
vector <Tree> tree(n + 1);
for(int i = 2; i <= m; i++) {
int c = lca.get(a[i], a[i - 1]);
tree[c].modify(i - 1, 1);
}
for(int i = 1; i <= m; i++) {
tree[a[i]].modify(i, 1);
}
for(int it = 0; it < q; it++) {
int type;
cin >> type;
if (type == 1) {
int pos, val;
cin >> pos >> val;
for(int i = max(pos, 2); i <= min(n, pos + 1); i++) {
int c = lca.get(a[i], a[i - 1]);
tree[c].modify(i - 1, -1);
}
tree[a[pos]].modify(pos, -1);
a[pos] = val;
tree[a[pos]].modify(pos, 1);
for(int i = max(pos, 2); i <= min(n, pos + 1); i++) {
int c = lca.get(a[i], a[i - 1]);
tree[c].modify(i - 1, 1);
}
}
else {
int l, r, c;
cin >> l >> r >> c;
int pos = tree[c].get(l, r);
if (pos > r || pos + (a[pos] != c) > r) {
cout << "-1 -1\n";
}
else {
cout << pos << " " << pos + (a[pos] != c) << "\n";
}
}
}
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=5 |
2 |
Correct |
2 ms |
376 KB |
n=100 |
3 |
Correct |
3 ms |
380 KB |
n=100 |
4 |
Correct |
3 ms |
376 KB |
n=100 |
5 |
Correct |
2 ms |
376 KB |
n=100 |
6 |
Correct |
2 ms |
376 KB |
n=100 |
7 |
Correct |
1 ms |
356 KB |
n=100 |
8 |
Correct |
2 ms |
376 KB |
n=100 |
9 |
Correct |
2 ms |
376 KB |
n=100 |
10 |
Correct |
2 ms |
376 KB |
n=100 |
11 |
Correct |
2 ms |
376 KB |
n=100 |
12 |
Correct |
2 ms |
376 KB |
n=100 |
13 |
Correct |
2 ms |
376 KB |
n=100 |
14 |
Correct |
2 ms |
376 KB |
n=100 |
15 |
Correct |
2 ms |
376 KB |
n=100 |
16 |
Correct |
2 ms |
376 KB |
n=100 |
17 |
Correct |
3 ms |
376 KB |
n=100 |
18 |
Correct |
2 ms |
248 KB |
n=100 |
19 |
Correct |
2 ms |
380 KB |
n=100 |
20 |
Correct |
2 ms |
376 KB |
n=100 |
21 |
Correct |
2 ms |
376 KB |
n=100 |
22 |
Correct |
2 ms |
376 KB |
n=100 |
23 |
Correct |
2 ms |
376 KB |
n=100 |
24 |
Correct |
2 ms |
376 KB |
n=100 |
25 |
Correct |
2 ms |
376 KB |
n=100 |
26 |
Correct |
8 ms |
376 KB |
n=12 |
27 |
Correct |
2 ms |
376 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=5 |
2 |
Correct |
2 ms |
376 KB |
n=100 |
3 |
Correct |
3 ms |
380 KB |
n=100 |
4 |
Correct |
3 ms |
376 KB |
n=100 |
5 |
Correct |
2 ms |
376 KB |
n=100 |
6 |
Correct |
2 ms |
376 KB |
n=100 |
7 |
Correct |
1 ms |
356 KB |
n=100 |
8 |
Correct |
2 ms |
376 KB |
n=100 |
9 |
Correct |
2 ms |
376 KB |
n=100 |
10 |
Correct |
2 ms |
376 KB |
n=100 |
11 |
Correct |
2 ms |
376 KB |
n=100 |
12 |
Correct |
2 ms |
376 KB |
n=100 |
13 |
Correct |
2 ms |
376 KB |
n=100 |
14 |
Correct |
2 ms |
376 KB |
n=100 |
15 |
Correct |
2 ms |
376 KB |
n=100 |
16 |
Correct |
2 ms |
376 KB |
n=100 |
17 |
Correct |
3 ms |
376 KB |
n=100 |
18 |
Correct |
2 ms |
248 KB |
n=100 |
19 |
Correct |
2 ms |
380 KB |
n=100 |
20 |
Correct |
2 ms |
376 KB |
n=100 |
21 |
Correct |
2 ms |
376 KB |
n=100 |
22 |
Correct |
2 ms |
376 KB |
n=100 |
23 |
Correct |
2 ms |
376 KB |
n=100 |
24 |
Correct |
2 ms |
376 KB |
n=100 |
25 |
Correct |
2 ms |
376 KB |
n=100 |
26 |
Correct |
8 ms |
376 KB |
n=12 |
27 |
Correct |
2 ms |
376 KB |
n=100 |
28 |
Correct |
3 ms |
504 KB |
n=500 |
29 |
Correct |
3 ms |
516 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
3 ms |
504 KB |
n=500 |
32 |
Correct |
3 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
504 KB |
n=500 |
34 |
Correct |
3 ms |
504 KB |
n=500 |
35 |
Correct |
3 ms |
528 KB |
n=500 |
36 |
Correct |
3 ms |
504 KB |
n=500 |
37 |
Correct |
3 ms |
504 KB |
n=500 |
38 |
Correct |
3 ms |
504 KB |
n=500 |
39 |
Correct |
3 ms |
504 KB |
n=500 |
40 |
Correct |
3 ms |
504 KB |
n=500 |
41 |
Correct |
3 ms |
476 KB |
n=500 |
42 |
Correct |
3 ms |
504 KB |
n=500 |
43 |
Correct |
3 ms |
504 KB |
n=500 |
44 |
Correct |
3 ms |
508 KB |
n=500 |
45 |
Correct |
3 ms |
504 KB |
n=500 |
46 |
Correct |
3 ms |
504 KB |
n=500 |
47 |
Correct |
3 ms |
508 KB |
n=500 |
48 |
Correct |
3 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
4 ms |
504 KB |
n=500 |
51 |
Correct |
3 ms |
504 KB |
n=500 |
52 |
Correct |
3 ms |
504 KB |
n=500 |
53 |
Correct |
3 ms |
504 KB |
n=500 |
54 |
Correct |
3 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
376 KB |
n=278 |
56 |
Correct |
3 ms |
632 KB |
n=500 |
57 |
Correct |
3 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
504 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=5 |
2 |
Correct |
2 ms |
376 KB |
n=100 |
3 |
Correct |
3 ms |
380 KB |
n=100 |
4 |
Correct |
3 ms |
376 KB |
n=100 |
5 |
Correct |
2 ms |
376 KB |
n=100 |
6 |
Correct |
2 ms |
376 KB |
n=100 |
7 |
Correct |
1 ms |
356 KB |
n=100 |
8 |
Correct |
2 ms |
376 KB |
n=100 |
9 |
Correct |
2 ms |
376 KB |
n=100 |
10 |
Correct |
2 ms |
376 KB |
n=100 |
11 |
Correct |
2 ms |
376 KB |
n=100 |
12 |
Correct |
2 ms |
376 KB |
n=100 |
13 |
Correct |
2 ms |
376 KB |
n=100 |
14 |
Correct |
2 ms |
376 KB |
n=100 |
15 |
Correct |
2 ms |
376 KB |
n=100 |
16 |
Correct |
2 ms |
376 KB |
n=100 |
17 |
Correct |
3 ms |
376 KB |
n=100 |
18 |
Correct |
2 ms |
248 KB |
n=100 |
19 |
Correct |
2 ms |
380 KB |
n=100 |
20 |
Correct |
2 ms |
376 KB |
n=100 |
21 |
Correct |
2 ms |
376 KB |
n=100 |
22 |
Correct |
2 ms |
376 KB |
n=100 |
23 |
Correct |
2 ms |
376 KB |
n=100 |
24 |
Correct |
2 ms |
376 KB |
n=100 |
25 |
Correct |
2 ms |
376 KB |
n=100 |
26 |
Correct |
8 ms |
376 KB |
n=12 |
27 |
Correct |
2 ms |
376 KB |
n=100 |
28 |
Correct |
3 ms |
504 KB |
n=500 |
29 |
Correct |
3 ms |
516 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
3 ms |
504 KB |
n=500 |
32 |
Correct |
3 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
504 KB |
n=500 |
34 |
Correct |
3 ms |
504 KB |
n=500 |
35 |
Correct |
3 ms |
528 KB |
n=500 |
36 |
Correct |
3 ms |
504 KB |
n=500 |
37 |
Correct |
3 ms |
504 KB |
n=500 |
38 |
Correct |
3 ms |
504 KB |
n=500 |
39 |
Correct |
3 ms |
504 KB |
n=500 |
40 |
Correct |
3 ms |
504 KB |
n=500 |
41 |
Correct |
3 ms |
476 KB |
n=500 |
42 |
Correct |
3 ms |
504 KB |
n=500 |
43 |
Correct |
3 ms |
504 KB |
n=500 |
44 |
Correct |
3 ms |
508 KB |
n=500 |
45 |
Correct |
3 ms |
504 KB |
n=500 |
46 |
Correct |
3 ms |
504 KB |
n=500 |
47 |
Correct |
3 ms |
508 KB |
n=500 |
48 |
Correct |
3 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
4 ms |
504 KB |
n=500 |
51 |
Correct |
3 ms |
504 KB |
n=500 |
52 |
Correct |
3 ms |
504 KB |
n=500 |
53 |
Correct |
3 ms |
504 KB |
n=500 |
54 |
Correct |
3 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
376 KB |
n=278 |
56 |
Correct |
3 ms |
632 KB |
n=500 |
57 |
Correct |
3 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
504 KB |
n=500 |
59 |
Correct |
7 ms |
1016 KB |
n=2000 |
60 |
Correct |
6 ms |
1144 KB |
n=2000 |
61 |
Correct |
6 ms |
1144 KB |
n=2000 |
62 |
Correct |
7 ms |
1016 KB |
n=2000 |
63 |
Correct |
7 ms |
1020 KB |
n=2000 |
64 |
Correct |
8 ms |
1144 KB |
n=2000 |
65 |
Correct |
17 ms |
1144 KB |
n=2000 |
66 |
Correct |
6 ms |
1144 KB |
n=2000 |
67 |
Correct |
7 ms |
1016 KB |
n=2000 |
68 |
Correct |
6 ms |
1144 KB |
n=2000 |
69 |
Correct |
5 ms |
1016 KB |
n=2000 |
70 |
Correct |
9 ms |
1144 KB |
n=2000 |
71 |
Correct |
6 ms |
1144 KB |
n=2000 |
72 |
Correct |
4 ms |
1016 KB |
n=2000 |
73 |
Correct |
0 ms |
1016 KB |
n=2000 |
74 |
Correct |
6 ms |
1016 KB |
n=1844 |
75 |
Correct |
5 ms |
1016 KB |
n=2000 |
76 |
Correct |
6 ms |
1016 KB |
n=2000 |
77 |
Correct |
6 ms |
1016 KB |
n=2000 |
78 |
Correct |
6 ms |
1016 KB |
n=2000 |
79 |
Correct |
6 ms |
1016 KB |
n=2000 |
80 |
Correct |
6 ms |
1144 KB |
n=2000 |
81 |
Correct |
6 ms |
1144 KB |
n=2000 |
82 |
Correct |
6 ms |
1016 KB |
n=2000 |
83 |
Correct |
7 ms |
1144 KB |
n=2000 |
84 |
Correct |
7 ms |
1016 KB |
n=2000 |
85 |
Correct |
7 ms |
1144 KB |
n=2000 |
86 |
Correct |
7 ms |
1144 KB |
n=2000 |
87 |
Correct |
7 ms |
1144 KB |
n=2000 |
88 |
Correct |
6 ms |
1144 KB |
n=2000 |
89 |
Correct |
5 ms |
1144 KB |
n=2000 |
90 |
Correct |
6 ms |
1144 KB |
n=2000 |
91 |
Correct |
6 ms |
1148 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=5 |
2 |
Correct |
2 ms |
376 KB |
n=100 |
3 |
Correct |
3 ms |
380 KB |
n=100 |
4 |
Correct |
3 ms |
376 KB |
n=100 |
5 |
Correct |
2 ms |
376 KB |
n=100 |
6 |
Correct |
2 ms |
376 KB |
n=100 |
7 |
Correct |
1 ms |
356 KB |
n=100 |
8 |
Correct |
2 ms |
376 KB |
n=100 |
9 |
Correct |
2 ms |
376 KB |
n=100 |
10 |
Correct |
2 ms |
376 KB |
n=100 |
11 |
Correct |
2 ms |
376 KB |
n=100 |
12 |
Correct |
2 ms |
376 KB |
n=100 |
13 |
Correct |
2 ms |
376 KB |
n=100 |
14 |
Correct |
2 ms |
376 KB |
n=100 |
15 |
Correct |
2 ms |
376 KB |
n=100 |
16 |
Correct |
2 ms |
376 KB |
n=100 |
17 |
Correct |
3 ms |
376 KB |
n=100 |
18 |
Correct |
2 ms |
248 KB |
n=100 |
19 |
Correct |
2 ms |
380 KB |
n=100 |
20 |
Correct |
2 ms |
376 KB |
n=100 |
21 |
Correct |
2 ms |
376 KB |
n=100 |
22 |
Correct |
2 ms |
376 KB |
n=100 |
23 |
Correct |
2 ms |
376 KB |
n=100 |
24 |
Correct |
2 ms |
376 KB |
n=100 |
25 |
Correct |
2 ms |
376 KB |
n=100 |
26 |
Correct |
8 ms |
376 KB |
n=12 |
27 |
Correct |
2 ms |
376 KB |
n=100 |
28 |
Correct |
3 ms |
504 KB |
n=500 |
29 |
Correct |
3 ms |
516 KB |
n=500 |
30 |
Correct |
5 ms |
504 KB |
n=500 |
31 |
Correct |
3 ms |
504 KB |
n=500 |
32 |
Correct |
3 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
504 KB |
n=500 |
34 |
Correct |
3 ms |
504 KB |
n=500 |
35 |
Correct |
3 ms |
528 KB |
n=500 |
36 |
Correct |
3 ms |
504 KB |
n=500 |
37 |
Correct |
3 ms |
504 KB |
n=500 |
38 |
Correct |
3 ms |
504 KB |
n=500 |
39 |
Correct |
3 ms |
504 KB |
n=500 |
40 |
Correct |
3 ms |
504 KB |
n=500 |
41 |
Correct |
3 ms |
476 KB |
n=500 |
42 |
Correct |
3 ms |
504 KB |
n=500 |
43 |
Correct |
3 ms |
504 KB |
n=500 |
44 |
Correct |
3 ms |
508 KB |
n=500 |
45 |
Correct |
3 ms |
504 KB |
n=500 |
46 |
Correct |
3 ms |
504 KB |
n=500 |
47 |
Correct |
3 ms |
508 KB |
n=500 |
48 |
Correct |
3 ms |
504 KB |
n=500 |
49 |
Correct |
4 ms |
504 KB |
n=500 |
50 |
Correct |
4 ms |
504 KB |
n=500 |
51 |
Correct |
3 ms |
504 KB |
n=500 |
52 |
Correct |
3 ms |
504 KB |
n=500 |
53 |
Correct |
3 ms |
504 KB |
n=500 |
54 |
Correct |
3 ms |
504 KB |
n=500 |
55 |
Correct |
3 ms |
376 KB |
n=278 |
56 |
Correct |
3 ms |
632 KB |
n=500 |
57 |
Correct |
3 ms |
504 KB |
n=500 |
58 |
Correct |
3 ms |
504 KB |
n=500 |
59 |
Correct |
7 ms |
1016 KB |
n=2000 |
60 |
Correct |
6 ms |
1144 KB |
n=2000 |
61 |
Correct |
6 ms |
1144 KB |
n=2000 |
62 |
Correct |
7 ms |
1016 KB |
n=2000 |
63 |
Correct |
7 ms |
1020 KB |
n=2000 |
64 |
Correct |
8 ms |
1144 KB |
n=2000 |
65 |
Correct |
17 ms |
1144 KB |
n=2000 |
66 |
Correct |
6 ms |
1144 KB |
n=2000 |
67 |
Correct |
7 ms |
1016 KB |
n=2000 |
68 |
Correct |
6 ms |
1144 KB |
n=2000 |
69 |
Correct |
5 ms |
1016 KB |
n=2000 |
70 |
Correct |
9 ms |
1144 KB |
n=2000 |
71 |
Correct |
6 ms |
1144 KB |
n=2000 |
72 |
Correct |
4 ms |
1016 KB |
n=2000 |
73 |
Correct |
0 ms |
1016 KB |
n=2000 |
74 |
Correct |
6 ms |
1016 KB |
n=1844 |
75 |
Correct |
5 ms |
1016 KB |
n=2000 |
76 |
Correct |
6 ms |
1016 KB |
n=2000 |
77 |
Correct |
6 ms |
1016 KB |
n=2000 |
78 |
Correct |
6 ms |
1016 KB |
n=2000 |
79 |
Correct |
6 ms |
1016 KB |
n=2000 |
80 |
Correct |
6 ms |
1144 KB |
n=2000 |
81 |
Correct |
6 ms |
1144 KB |
n=2000 |
82 |
Correct |
6 ms |
1016 KB |
n=2000 |
83 |
Correct |
7 ms |
1144 KB |
n=2000 |
84 |
Correct |
7 ms |
1016 KB |
n=2000 |
85 |
Correct |
7 ms |
1144 KB |
n=2000 |
86 |
Correct |
7 ms |
1144 KB |
n=2000 |
87 |
Correct |
7 ms |
1144 KB |
n=2000 |
88 |
Correct |
6 ms |
1144 KB |
n=2000 |
89 |
Correct |
5 ms |
1144 KB |
n=2000 |
90 |
Correct |
6 ms |
1144 KB |
n=2000 |
91 |
Correct |
6 ms |
1148 KB |
n=2000 |
92 |
Correct |
1084 ms |
82476 KB |
n=200000 |
93 |
Correct |
1165 ms |
85784 KB |
n=200000 |
94 |
Correct |
859 ms |
88392 KB |
n=200000 |
95 |
Correct |
964 ms |
82412 KB |
n=200000 |
96 |
Correct |
1122 ms |
82348 KB |
n=200000 |
97 |
Correct |
1438 ms |
85152 KB |
n=200000 |
98 |
Correct |
1098 ms |
82180 KB |
n=200000 |
99 |
Correct |
1231 ms |
81760 KB |
n=200000 |
100 |
Correct |
983 ms |
82204 KB |
n=200000 |
101 |
Incorrect |
751 ms |
89208 KB |
Wrong range. |
102 |
Halted |
0 ms |
0 KB |
- |