#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
const int MAXLOG = 20;
vector<int> g[MAXN];
namespace lca{
int st[MAXN][MAXLOG], d[MAXN];
void get_par(int x, int p){
st[x][0] = p;
if(p != -1) d[x] = d[p] + 1;
for(auto y : g[x]) if(y != p) get_par(y, x);
}
void init(int root, int n){
get_par(root, -1);
for(int l = 1; l < MAXLOG; ++l) for(int i = 0; i < MAXN; ++i){
if(st[i][l-1] == -1) st[i][l] = -1;
else st[i][l] = st[st[i][l-1]][l-1];
}
}
int lca(int x, int y){
if(d[x] < d[y]) swap(x, y);
int falta_subir = (d[x] - d[y]);
for(int i = MAXLOG-1; i >= 0; --i) if((1<<i) <= falta_subir){
falta_subir -= (1<<i);
x = st[x][i];
}
if(x == y) return x;
for(int i = MAXLOG-1; i >= 0; --i) if(st[x][i] != st[y][i])
x = st[x][i], y = st[y][i];
return st[x][0];
}
int dist(int a, int b){
int x = lca(a, b);
return (d[a] - d[x]) + (d[b] - d[x]);
}
}
namespace solver{
int v[2*MAXN];
set<int> v2pos[MAXN];
int n;
void set_val(int i, int x){
v2pos[v[i]].erase(i);
v[i] = x;
v2pos[v[i]].insert(i);
}
int get_idx(int i){ return i * 2; }
void add(int i, int x){
i = get_idx(i);
set_val(i, x);
// for(int j = 0; j <= get_idx(n - 1); ++j){
// cout << v[j] << " ";
// }cout << "\n";
if(i > 0){
set_val(i - 1, lca::lca(v[i - 2], v[i]));
}
if(i < get_idx(n - 1)){
set_val(i + 1, lca::lca(v[i], v[i + 2]));
}
}
pair<int,int> query(int l, int r, int x){
l = get_idx(l);
r = get_idx(r);
auto it = v2pos[x].lower_bound(l);
if(it == v2pos[x].end() || *it > r) return {-1, -1};
else{
int i = *it;
if(*it % 2 == 1){
return {(i - 1) / 2, (i + 1) / 2};
}else{
return {i / 2, i / 2};
}
}
}
}
int main(){
int n, m, q; cin >> n >> m >> q;
for(int i = 0; i < n - 1; ++i){
int a, b; cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
lca::init(0, n);
solver::n = m;
for(int i = 0; i <= solver::get_idx(m - 1); ++i){
solver::v2pos[0].insert(i);
}
for(int i = 0; i < m; ++i){
int x; cin >> x; x--;
solver::add(i, x);
}
for(int i = 0; i < q; ++i){
int id; cin >> id;
if(id == 1){
int i, v; cin >> i >> v;
i--; v--;
solver::add(i, v);
}else{
int l, r, x; cin >> l >> r >> x;
l--; r--; x--;
auto ret = solver::query(l, r, x);
if(ret.first == -1) cout << "-1 -1\n";
else{
cout << ret.first + 1 << " " << ret.second + 1 << "\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
30036 KB |
n=5 |
2 |
Correct |
45 ms |
30044 KB |
n=100 |
3 |
Correct |
45 ms |
29992 KB |
n=100 |
4 |
Correct |
44 ms |
30036 KB |
n=100 |
5 |
Correct |
44 ms |
29996 KB |
n=100 |
6 |
Correct |
43 ms |
30048 KB |
n=100 |
7 |
Correct |
45 ms |
30056 KB |
n=100 |
8 |
Correct |
45 ms |
30044 KB |
n=100 |
9 |
Correct |
44 ms |
30052 KB |
n=100 |
10 |
Correct |
45 ms |
30036 KB |
n=100 |
11 |
Correct |
45 ms |
30044 KB |
n=100 |
12 |
Correct |
46 ms |
30044 KB |
n=100 |
13 |
Correct |
45 ms |
30036 KB |
n=100 |
14 |
Correct |
44 ms |
30056 KB |
n=100 |
15 |
Correct |
45 ms |
30036 KB |
n=100 |
16 |
Correct |
44 ms |
30044 KB |
n=100 |
17 |
Correct |
45 ms |
30156 KB |
n=100 |
18 |
Correct |
43 ms |
30036 KB |
n=100 |
19 |
Correct |
43 ms |
30056 KB |
n=100 |
20 |
Correct |
46 ms |
30044 KB |
n=100 |
21 |
Correct |
47 ms |
30036 KB |
n=100 |
22 |
Correct |
45 ms |
30008 KB |
n=100 |
23 |
Correct |
44 ms |
30044 KB |
n=100 |
24 |
Correct |
46 ms |
30044 KB |
n=100 |
25 |
Correct |
48 ms |
30000 KB |
n=100 |
26 |
Correct |
45 ms |
30016 KB |
n=12 |
27 |
Correct |
46 ms |
30056 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
30036 KB |
n=5 |
2 |
Correct |
45 ms |
30044 KB |
n=100 |
3 |
Correct |
45 ms |
29992 KB |
n=100 |
4 |
Correct |
44 ms |
30036 KB |
n=100 |
5 |
Correct |
44 ms |
29996 KB |
n=100 |
6 |
Correct |
43 ms |
30048 KB |
n=100 |
7 |
Correct |
45 ms |
30056 KB |
n=100 |
8 |
Correct |
45 ms |
30044 KB |
n=100 |
9 |
Correct |
44 ms |
30052 KB |
n=100 |
10 |
Correct |
45 ms |
30036 KB |
n=100 |
11 |
Correct |
45 ms |
30044 KB |
n=100 |
12 |
Correct |
46 ms |
30044 KB |
n=100 |
13 |
Correct |
45 ms |
30036 KB |
n=100 |
14 |
Correct |
44 ms |
30056 KB |
n=100 |
15 |
Correct |
45 ms |
30036 KB |
n=100 |
16 |
Correct |
44 ms |
30044 KB |
n=100 |
17 |
Correct |
45 ms |
30156 KB |
n=100 |
18 |
Correct |
43 ms |
30036 KB |
n=100 |
19 |
Correct |
43 ms |
30056 KB |
n=100 |
20 |
Correct |
46 ms |
30044 KB |
n=100 |
21 |
Correct |
47 ms |
30036 KB |
n=100 |
22 |
Correct |
45 ms |
30008 KB |
n=100 |
23 |
Correct |
44 ms |
30044 KB |
n=100 |
24 |
Correct |
46 ms |
30044 KB |
n=100 |
25 |
Correct |
48 ms |
30000 KB |
n=100 |
26 |
Correct |
45 ms |
30016 KB |
n=12 |
27 |
Correct |
46 ms |
30056 KB |
n=100 |
28 |
Correct |
47 ms |
30072 KB |
n=500 |
29 |
Correct |
45 ms |
30112 KB |
n=500 |
30 |
Correct |
46 ms |
30080 KB |
n=500 |
31 |
Correct |
45 ms |
30084 KB |
n=500 |
32 |
Correct |
44 ms |
30024 KB |
n=500 |
33 |
Correct |
45 ms |
30112 KB |
n=500 |
34 |
Correct |
44 ms |
30076 KB |
n=500 |
35 |
Correct |
47 ms |
30100 KB |
n=500 |
36 |
Correct |
46 ms |
30120 KB |
n=500 |
37 |
Correct |
45 ms |
30036 KB |
n=500 |
38 |
Correct |
45 ms |
30080 KB |
n=500 |
39 |
Correct |
46 ms |
30072 KB |
n=500 |
40 |
Correct |
45 ms |
30036 KB |
n=500 |
41 |
Correct |
46 ms |
30156 KB |
n=500 |
42 |
Correct |
45 ms |
30080 KB |
n=500 |
43 |
Correct |
45 ms |
30068 KB |
n=500 |
44 |
Correct |
45 ms |
30036 KB |
n=500 |
45 |
Correct |
46 ms |
30088 KB |
n=500 |
46 |
Correct |
48 ms |
30128 KB |
n=500 |
47 |
Correct |
48 ms |
30036 KB |
n=500 |
48 |
Correct |
45 ms |
30020 KB |
n=500 |
49 |
Correct |
45 ms |
30088 KB |
n=500 |
50 |
Correct |
45 ms |
30084 KB |
n=500 |
51 |
Correct |
45 ms |
30156 KB |
n=500 |
52 |
Correct |
44 ms |
30100 KB |
n=500 |
53 |
Correct |
46 ms |
30072 KB |
n=500 |
54 |
Correct |
45 ms |
30036 KB |
n=500 |
55 |
Correct |
45 ms |
30068 KB |
n=278 |
56 |
Correct |
48 ms |
30088 KB |
n=500 |
57 |
Correct |
45 ms |
30036 KB |
n=500 |
58 |
Correct |
46 ms |
30176 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
30036 KB |
n=5 |
2 |
Correct |
45 ms |
30044 KB |
n=100 |
3 |
Correct |
45 ms |
29992 KB |
n=100 |
4 |
Correct |
44 ms |
30036 KB |
n=100 |
5 |
Correct |
44 ms |
29996 KB |
n=100 |
6 |
Correct |
43 ms |
30048 KB |
n=100 |
7 |
Correct |
45 ms |
30056 KB |
n=100 |
8 |
Correct |
45 ms |
30044 KB |
n=100 |
9 |
Correct |
44 ms |
30052 KB |
n=100 |
10 |
Correct |
45 ms |
30036 KB |
n=100 |
11 |
Correct |
45 ms |
30044 KB |
n=100 |
12 |
Correct |
46 ms |
30044 KB |
n=100 |
13 |
Correct |
45 ms |
30036 KB |
n=100 |
14 |
Correct |
44 ms |
30056 KB |
n=100 |
15 |
Correct |
45 ms |
30036 KB |
n=100 |
16 |
Correct |
44 ms |
30044 KB |
n=100 |
17 |
Correct |
45 ms |
30156 KB |
n=100 |
18 |
Correct |
43 ms |
30036 KB |
n=100 |
19 |
Correct |
43 ms |
30056 KB |
n=100 |
20 |
Correct |
46 ms |
30044 KB |
n=100 |
21 |
Correct |
47 ms |
30036 KB |
n=100 |
22 |
Correct |
45 ms |
30008 KB |
n=100 |
23 |
Correct |
44 ms |
30044 KB |
n=100 |
24 |
Correct |
46 ms |
30044 KB |
n=100 |
25 |
Correct |
48 ms |
30000 KB |
n=100 |
26 |
Correct |
45 ms |
30016 KB |
n=12 |
27 |
Correct |
46 ms |
30056 KB |
n=100 |
28 |
Correct |
47 ms |
30072 KB |
n=500 |
29 |
Correct |
45 ms |
30112 KB |
n=500 |
30 |
Correct |
46 ms |
30080 KB |
n=500 |
31 |
Correct |
45 ms |
30084 KB |
n=500 |
32 |
Correct |
44 ms |
30024 KB |
n=500 |
33 |
Correct |
45 ms |
30112 KB |
n=500 |
34 |
Correct |
44 ms |
30076 KB |
n=500 |
35 |
Correct |
47 ms |
30100 KB |
n=500 |
36 |
Correct |
46 ms |
30120 KB |
n=500 |
37 |
Correct |
45 ms |
30036 KB |
n=500 |
38 |
Correct |
45 ms |
30080 KB |
n=500 |
39 |
Correct |
46 ms |
30072 KB |
n=500 |
40 |
Correct |
45 ms |
30036 KB |
n=500 |
41 |
Correct |
46 ms |
30156 KB |
n=500 |
42 |
Correct |
45 ms |
30080 KB |
n=500 |
43 |
Correct |
45 ms |
30068 KB |
n=500 |
44 |
Correct |
45 ms |
30036 KB |
n=500 |
45 |
Correct |
46 ms |
30088 KB |
n=500 |
46 |
Correct |
48 ms |
30128 KB |
n=500 |
47 |
Correct |
48 ms |
30036 KB |
n=500 |
48 |
Correct |
45 ms |
30020 KB |
n=500 |
49 |
Correct |
45 ms |
30088 KB |
n=500 |
50 |
Correct |
45 ms |
30084 KB |
n=500 |
51 |
Correct |
45 ms |
30156 KB |
n=500 |
52 |
Correct |
44 ms |
30100 KB |
n=500 |
53 |
Correct |
46 ms |
30072 KB |
n=500 |
54 |
Correct |
45 ms |
30036 KB |
n=500 |
55 |
Correct |
45 ms |
30068 KB |
n=278 |
56 |
Correct |
48 ms |
30088 KB |
n=500 |
57 |
Correct |
45 ms |
30036 KB |
n=500 |
58 |
Correct |
46 ms |
30176 KB |
n=500 |
59 |
Correct |
50 ms |
30300 KB |
n=2000 |
60 |
Correct |
50 ms |
30376 KB |
n=2000 |
61 |
Correct |
51 ms |
30368 KB |
n=2000 |
62 |
Correct |
51 ms |
30328 KB |
n=2000 |
63 |
Correct |
50 ms |
30308 KB |
n=2000 |
64 |
Correct |
50 ms |
30392 KB |
n=2000 |
65 |
Correct |
52 ms |
30312 KB |
n=2000 |
66 |
Correct |
52 ms |
30412 KB |
n=2000 |
67 |
Correct |
52 ms |
30332 KB |
n=2000 |
68 |
Correct |
52 ms |
30408 KB |
n=2000 |
69 |
Correct |
50 ms |
30284 KB |
n=2000 |
70 |
Correct |
54 ms |
30412 KB |
n=2000 |
71 |
Correct |
50 ms |
30304 KB |
n=2000 |
72 |
Correct |
50 ms |
30300 KB |
n=2000 |
73 |
Correct |
51 ms |
30284 KB |
n=2000 |
74 |
Correct |
49 ms |
30292 KB |
n=1844 |
75 |
Correct |
51 ms |
30284 KB |
n=2000 |
76 |
Correct |
50 ms |
30364 KB |
n=2000 |
77 |
Correct |
51 ms |
30300 KB |
n=2000 |
78 |
Correct |
52 ms |
30368 KB |
n=2000 |
79 |
Correct |
52 ms |
30304 KB |
n=2000 |
80 |
Correct |
52 ms |
30372 KB |
n=2000 |
81 |
Correct |
49 ms |
30328 KB |
n=2000 |
82 |
Correct |
50 ms |
30284 KB |
n=2000 |
83 |
Correct |
55 ms |
30352 KB |
n=2000 |
84 |
Correct |
67 ms |
30420 KB |
n=2000 |
85 |
Correct |
65 ms |
30380 KB |
n=2000 |
86 |
Correct |
64 ms |
30404 KB |
n=2000 |
87 |
Correct |
62 ms |
30300 KB |
n=2000 |
88 |
Correct |
63 ms |
30460 KB |
n=2000 |
89 |
Correct |
62 ms |
30408 KB |
n=2000 |
90 |
Correct |
64 ms |
30388 KB |
n=2000 |
91 |
Correct |
61 ms |
30284 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
30036 KB |
n=5 |
2 |
Correct |
45 ms |
30044 KB |
n=100 |
3 |
Correct |
45 ms |
29992 KB |
n=100 |
4 |
Correct |
44 ms |
30036 KB |
n=100 |
5 |
Correct |
44 ms |
29996 KB |
n=100 |
6 |
Correct |
43 ms |
30048 KB |
n=100 |
7 |
Correct |
45 ms |
30056 KB |
n=100 |
8 |
Correct |
45 ms |
30044 KB |
n=100 |
9 |
Correct |
44 ms |
30052 KB |
n=100 |
10 |
Correct |
45 ms |
30036 KB |
n=100 |
11 |
Correct |
45 ms |
30044 KB |
n=100 |
12 |
Correct |
46 ms |
30044 KB |
n=100 |
13 |
Correct |
45 ms |
30036 KB |
n=100 |
14 |
Correct |
44 ms |
30056 KB |
n=100 |
15 |
Correct |
45 ms |
30036 KB |
n=100 |
16 |
Correct |
44 ms |
30044 KB |
n=100 |
17 |
Correct |
45 ms |
30156 KB |
n=100 |
18 |
Correct |
43 ms |
30036 KB |
n=100 |
19 |
Correct |
43 ms |
30056 KB |
n=100 |
20 |
Correct |
46 ms |
30044 KB |
n=100 |
21 |
Correct |
47 ms |
30036 KB |
n=100 |
22 |
Correct |
45 ms |
30008 KB |
n=100 |
23 |
Correct |
44 ms |
30044 KB |
n=100 |
24 |
Correct |
46 ms |
30044 KB |
n=100 |
25 |
Correct |
48 ms |
30000 KB |
n=100 |
26 |
Correct |
45 ms |
30016 KB |
n=12 |
27 |
Correct |
46 ms |
30056 KB |
n=100 |
28 |
Correct |
47 ms |
30072 KB |
n=500 |
29 |
Correct |
45 ms |
30112 KB |
n=500 |
30 |
Correct |
46 ms |
30080 KB |
n=500 |
31 |
Correct |
45 ms |
30084 KB |
n=500 |
32 |
Correct |
44 ms |
30024 KB |
n=500 |
33 |
Correct |
45 ms |
30112 KB |
n=500 |
34 |
Correct |
44 ms |
30076 KB |
n=500 |
35 |
Correct |
47 ms |
30100 KB |
n=500 |
36 |
Correct |
46 ms |
30120 KB |
n=500 |
37 |
Correct |
45 ms |
30036 KB |
n=500 |
38 |
Correct |
45 ms |
30080 KB |
n=500 |
39 |
Correct |
46 ms |
30072 KB |
n=500 |
40 |
Correct |
45 ms |
30036 KB |
n=500 |
41 |
Correct |
46 ms |
30156 KB |
n=500 |
42 |
Correct |
45 ms |
30080 KB |
n=500 |
43 |
Correct |
45 ms |
30068 KB |
n=500 |
44 |
Correct |
45 ms |
30036 KB |
n=500 |
45 |
Correct |
46 ms |
30088 KB |
n=500 |
46 |
Correct |
48 ms |
30128 KB |
n=500 |
47 |
Correct |
48 ms |
30036 KB |
n=500 |
48 |
Correct |
45 ms |
30020 KB |
n=500 |
49 |
Correct |
45 ms |
30088 KB |
n=500 |
50 |
Correct |
45 ms |
30084 KB |
n=500 |
51 |
Correct |
45 ms |
30156 KB |
n=500 |
52 |
Correct |
44 ms |
30100 KB |
n=500 |
53 |
Correct |
46 ms |
30072 KB |
n=500 |
54 |
Correct |
45 ms |
30036 KB |
n=500 |
55 |
Correct |
45 ms |
30068 KB |
n=278 |
56 |
Correct |
48 ms |
30088 KB |
n=500 |
57 |
Correct |
45 ms |
30036 KB |
n=500 |
58 |
Correct |
46 ms |
30176 KB |
n=500 |
59 |
Correct |
50 ms |
30300 KB |
n=2000 |
60 |
Correct |
50 ms |
30376 KB |
n=2000 |
61 |
Correct |
51 ms |
30368 KB |
n=2000 |
62 |
Correct |
51 ms |
30328 KB |
n=2000 |
63 |
Correct |
50 ms |
30308 KB |
n=2000 |
64 |
Correct |
50 ms |
30392 KB |
n=2000 |
65 |
Correct |
52 ms |
30312 KB |
n=2000 |
66 |
Correct |
52 ms |
30412 KB |
n=2000 |
67 |
Correct |
52 ms |
30332 KB |
n=2000 |
68 |
Correct |
52 ms |
30408 KB |
n=2000 |
69 |
Correct |
50 ms |
30284 KB |
n=2000 |
70 |
Correct |
54 ms |
30412 KB |
n=2000 |
71 |
Correct |
50 ms |
30304 KB |
n=2000 |
72 |
Correct |
50 ms |
30300 KB |
n=2000 |
73 |
Correct |
51 ms |
30284 KB |
n=2000 |
74 |
Correct |
49 ms |
30292 KB |
n=1844 |
75 |
Correct |
51 ms |
30284 KB |
n=2000 |
76 |
Correct |
50 ms |
30364 KB |
n=2000 |
77 |
Correct |
51 ms |
30300 KB |
n=2000 |
78 |
Correct |
52 ms |
30368 KB |
n=2000 |
79 |
Correct |
52 ms |
30304 KB |
n=2000 |
80 |
Correct |
52 ms |
30372 KB |
n=2000 |
81 |
Correct |
49 ms |
30328 KB |
n=2000 |
82 |
Correct |
50 ms |
30284 KB |
n=2000 |
83 |
Correct |
55 ms |
30352 KB |
n=2000 |
84 |
Correct |
67 ms |
30420 KB |
n=2000 |
85 |
Correct |
65 ms |
30380 KB |
n=2000 |
86 |
Correct |
64 ms |
30404 KB |
n=2000 |
87 |
Correct |
62 ms |
30300 KB |
n=2000 |
88 |
Correct |
63 ms |
30460 KB |
n=2000 |
89 |
Correct |
62 ms |
30408 KB |
n=2000 |
90 |
Correct |
64 ms |
30388 KB |
n=2000 |
91 |
Correct |
61 ms |
30284 KB |
n=2000 |
92 |
Correct |
1262 ms |
66028 KB |
n=200000 |
93 |
Correct |
1416 ms |
70852 KB |
n=200000 |
94 |
Correct |
1292 ms |
74140 KB |
n=200000 |
95 |
Correct |
1158 ms |
65568 KB |
n=200000 |
96 |
Correct |
1170 ms |
65792 KB |
n=200000 |
97 |
Correct |
1334 ms |
70216 KB |
n=200000 |
98 |
Correct |
1146 ms |
65672 KB |
n=200000 |
99 |
Correct |
1310 ms |
65920 KB |
n=200000 |
100 |
Correct |
1182 ms |
65724 KB |
n=200000 |
101 |
Correct |
1284 ms |
75136 KB |
n=200000 |
102 |
Correct |
1144 ms |
66920 KB |
n=200000 |
103 |
Correct |
1138 ms |
66936 KB |
n=200000 |
104 |
Correct |
1143 ms |
66912 KB |
n=200000 |
105 |
Correct |
1146 ms |
67248 KB |
n=200000 |
106 |
Correct |
1202 ms |
67304 KB |
n=200000 |
107 |
Correct |
1173 ms |
67316 KB |
n=200000 |
108 |
Correct |
1287 ms |
65804 KB |
n=200000 |
109 |
Correct |
1323 ms |
65964 KB |
n=200000 |
110 |
Correct |
1318 ms |
65916 KB |
n=200000 |
111 |
Correct |
1176 ms |
65204 KB |
n=200000 |
112 |
Correct |
1260 ms |
74436 KB |
n=200000 |
113 |
Correct |
1365 ms |
69740 KB |
n=200000 |
114 |
Correct |
1211 ms |
65212 KB |
n=200000 |
115 |
Correct |
1427 ms |
67744 KB |
n=200000 |
116 |
Correct |
1129 ms |
65784 KB |
n=200000 |
117 |
Correct |
1320 ms |
74712 KB |
n=200000 |
118 |
Correct |
1378 ms |
68608 KB |
n=200000 |
119 |
Correct |
1160 ms |
65784 KB |
n=200000 |
120 |
Correct |
1396 ms |
74364 KB |
n=200000 |
121 |
Correct |
1366 ms |
74312 KB |
n=200000 |
122 |
Correct |
1213 ms |
74716 KB |
n=200000 |
123 |
Correct |
1075 ms |
67132 KB |
n=200000 |
124 |
Correct |
411 ms |
43280 KB |
n=25264 |