#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 1,MOD = 998244353;
int n,m,q,a[N],up[N][25],tin[N],tout[N],timer = 0,b[N];
vector<int> g[N];
multiset<pair<int,int>> s[N * 4],t[N * 4];
pair<int,int> cur,cur1;
void dfs(int v,int pr = 1){
tin[v] = ++timer;
up[v][0] = pr;
for(int i = 1;i <= 20;i++){
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:g[v]){
if(to != pr) dfs(to,v);
}
tout[v] = timer;
}
bool is(int v,int u){
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v,int u){
if(is(v,u)) return v;
if(is(u,v)) return u;
for(int i = 18;i >= 0;i--){
if(!is(up[v][i],u)){
v = up[v][i];
}
}
return up[v][0];
}
void build(int v = 1,int tl = 1,int tr = m - 1){
if(tl == tr){
s[v].insert(make_pair(b[tl],tl));
}else{
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);
build(v + v + 1,tm + 1,tr);
s[v] = s[v + v];
for(auto k:s[v + v + 1]){
s[v].insert(k);
}
}
}
void build1(int v = 1,int tl = 1,int tr = m){
if(tl == tr){
t[v].insert({a[tl],tl});
}else{
int tm = (tl + tr) >> 1;
build1(v + v,tl,tm);
build1(v + v + 1,tm + 1,tr);
t[v] = t[v + v];
for(auto k:t[v + v + 1]){
t[v].insert(k);
}
}
}
void upd1(int pos,int val,int v = 1,int tl = 1,int tr = m){
t[v].erase({a[pos],pos});
t[v].insert({val,pos});
if(tl == tr) return;
int tm = (tl + tr) >> 1;
if(pos <= tm) upd1(pos,val,v+v,tl,tm);
else upd1(pos,val,v+v+1,tm+1,tr);
}
bool find1(int l,int r,int val,int v = 1,int tl = 1,int tr = m){
if(tl > r || l > tr || l > r) return false;
if(tl >= l && tr <=r){
auto it = t[v].lower_bound({val,0});
if(it == t[v].end()) return false;
if((*it).first != val) return false;
cur1 = *it;
return 1;
}else{
int tm = (tl + tr) >> 1;
return (find1(l,r,val,v+v,tl,tm) | find1(l,r,val,v+v+1,tm+1,tr));
}
}
void upd(int pos,int val,int v = 1,int tl = 1,int tr = m - 1){
s[v].erase({b[pos],pos});
s[v].insert({val,pos});
if(tl == tr) return;
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,v+v,tl,tm);
else upd(pos,val,v+v+1,tm+1,tr);
}
bool find(int l,int r,int val,int v = 1,int tl = 1,int tr = m-1){
if(tl > r || l > tr || l > r) return false;
if(tl >= l && tr <=r){
auto it = s[v].lower_bound({val,0});
if(it == s[v].end()) return false;
if((*it).first != val) return false;
cur = *it;
return 1;
}else{
int tm = (tl + tr) >> 1;
return (find(l,r,val,v+v,tl,tm) | find(l,r,val,v+v+1,tm+1,tr));
}
}
void test(){
cin >> n >> m >> q;
for(int i = 1;i <= n - 1;i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
for(int i = 1;i <= m;i++){
cin >> a[i];
if(i > 1) b[i - 1] = lca(a[i - 1],a[i]);
}
if(m > 1) build();
build1();
while(q--){
int tp;
cin >> tp;
if(tp == 1){
int pos,v;
cin >> pos >> v;
if(pos < m){
int x = lca(v,a[pos + 1]);
upd(pos,x);
b[pos] = x;
}
if(pos > 1){
int x = lca(a[pos - 1],v);
upd(pos - 1,x);
b[pos - 1] = x;
}
upd1(pos,v);
a[pos] = v;
}else{
int l,r,v;
cin >> l >> r >> v;
if(l == r){
if(v == a[l]){
cout << l << ' ' << r << '\n';
}else{
cout << -1 << ' ' << -1 << '\n';
}
continue;
}
if(find(l,r - 1,v)){
cout << cur.second << ' ' << cur.second + 1 << '\n';
}else{
if(find1(l,r,v)){
cout << cur1.second << ' ' << cur1.second << '\n';
continue;
}
cout << -1 << ' ' << -1 << '\n';
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) test();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
120024 KB |
n=5 |
2 |
Correct |
55 ms |
120180 KB |
n=100 |
3 |
Correct |
59 ms |
120180 KB |
n=100 |
4 |
Correct |
52 ms |
120076 KB |
n=100 |
5 |
Correct |
53 ms |
120168 KB |
n=100 |
6 |
Correct |
56 ms |
120184 KB |
n=100 |
7 |
Correct |
53 ms |
120184 KB |
n=100 |
8 |
Correct |
62 ms |
120200 KB |
n=100 |
9 |
Correct |
54 ms |
120140 KB |
n=100 |
10 |
Correct |
50 ms |
120168 KB |
n=100 |
11 |
Correct |
54 ms |
120152 KB |
n=100 |
12 |
Correct |
56 ms |
120128 KB |
n=100 |
13 |
Correct |
52 ms |
120104 KB |
n=100 |
14 |
Correct |
58 ms |
120184 KB |
n=100 |
15 |
Correct |
54 ms |
120104 KB |
n=100 |
16 |
Correct |
51 ms |
120160 KB |
n=100 |
17 |
Correct |
57 ms |
120140 KB |
n=100 |
18 |
Correct |
52 ms |
120068 KB |
n=100 |
19 |
Correct |
53 ms |
120120 KB |
n=100 |
20 |
Correct |
52 ms |
120176 KB |
n=100 |
21 |
Correct |
51 ms |
120144 KB |
n=100 |
22 |
Correct |
51 ms |
120064 KB |
n=100 |
23 |
Correct |
50 ms |
120144 KB |
n=100 |
24 |
Correct |
55 ms |
120140 KB |
n=100 |
25 |
Correct |
54 ms |
120140 KB |
n=100 |
26 |
Correct |
54 ms |
120044 KB |
n=12 |
27 |
Correct |
53 ms |
120184 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
120024 KB |
n=5 |
2 |
Correct |
55 ms |
120180 KB |
n=100 |
3 |
Correct |
59 ms |
120180 KB |
n=100 |
4 |
Correct |
52 ms |
120076 KB |
n=100 |
5 |
Correct |
53 ms |
120168 KB |
n=100 |
6 |
Correct |
56 ms |
120184 KB |
n=100 |
7 |
Correct |
53 ms |
120184 KB |
n=100 |
8 |
Correct |
62 ms |
120200 KB |
n=100 |
9 |
Correct |
54 ms |
120140 KB |
n=100 |
10 |
Correct |
50 ms |
120168 KB |
n=100 |
11 |
Correct |
54 ms |
120152 KB |
n=100 |
12 |
Correct |
56 ms |
120128 KB |
n=100 |
13 |
Correct |
52 ms |
120104 KB |
n=100 |
14 |
Correct |
58 ms |
120184 KB |
n=100 |
15 |
Correct |
54 ms |
120104 KB |
n=100 |
16 |
Correct |
51 ms |
120160 KB |
n=100 |
17 |
Correct |
57 ms |
120140 KB |
n=100 |
18 |
Correct |
52 ms |
120068 KB |
n=100 |
19 |
Correct |
53 ms |
120120 KB |
n=100 |
20 |
Correct |
52 ms |
120176 KB |
n=100 |
21 |
Correct |
51 ms |
120144 KB |
n=100 |
22 |
Correct |
51 ms |
120064 KB |
n=100 |
23 |
Correct |
50 ms |
120144 KB |
n=100 |
24 |
Correct |
55 ms |
120140 KB |
n=100 |
25 |
Correct |
54 ms |
120140 KB |
n=100 |
26 |
Correct |
54 ms |
120044 KB |
n=12 |
27 |
Correct |
53 ms |
120184 KB |
n=100 |
28 |
Correct |
55 ms |
120616 KB |
n=500 |
29 |
Correct |
57 ms |
120648 KB |
n=500 |
30 |
Correct |
61 ms |
120620 KB |
n=500 |
31 |
Correct |
55 ms |
120644 KB |
n=500 |
32 |
Correct |
55 ms |
120640 KB |
n=500 |
33 |
Correct |
56 ms |
120636 KB |
n=500 |
34 |
Correct |
55 ms |
120524 KB |
n=500 |
35 |
Correct |
61 ms |
120776 KB |
n=500 |
36 |
Correct |
53 ms |
120524 KB |
n=500 |
37 |
Correct |
55 ms |
120552 KB |
n=500 |
38 |
Correct |
55 ms |
120524 KB |
n=500 |
39 |
Correct |
54 ms |
120524 KB |
n=500 |
40 |
Correct |
56 ms |
120748 KB |
n=500 |
41 |
Correct |
59 ms |
120524 KB |
n=500 |
42 |
Correct |
55 ms |
120612 KB |
n=500 |
43 |
Correct |
56 ms |
120564 KB |
n=500 |
44 |
Correct |
56 ms |
120612 KB |
n=500 |
45 |
Correct |
56 ms |
120624 KB |
n=500 |
46 |
Correct |
61 ms |
120688 KB |
n=500 |
47 |
Correct |
56 ms |
120624 KB |
n=500 |
48 |
Correct |
55 ms |
120640 KB |
n=500 |
49 |
Correct |
65 ms |
120584 KB |
n=500 |
50 |
Correct |
58 ms |
120604 KB |
n=500 |
51 |
Correct |
55 ms |
120580 KB |
n=500 |
52 |
Correct |
57 ms |
120652 KB |
n=500 |
53 |
Correct |
64 ms |
120608 KB |
n=500 |
54 |
Correct |
55 ms |
120652 KB |
n=500 |
55 |
Correct |
55 ms |
120536 KB |
n=278 |
56 |
Correct |
57 ms |
120588 KB |
n=500 |
57 |
Correct |
59 ms |
120612 KB |
n=500 |
58 |
Correct |
55 ms |
120572 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
120024 KB |
n=5 |
2 |
Correct |
55 ms |
120180 KB |
n=100 |
3 |
Correct |
59 ms |
120180 KB |
n=100 |
4 |
Correct |
52 ms |
120076 KB |
n=100 |
5 |
Correct |
53 ms |
120168 KB |
n=100 |
6 |
Correct |
56 ms |
120184 KB |
n=100 |
7 |
Correct |
53 ms |
120184 KB |
n=100 |
8 |
Correct |
62 ms |
120200 KB |
n=100 |
9 |
Correct |
54 ms |
120140 KB |
n=100 |
10 |
Correct |
50 ms |
120168 KB |
n=100 |
11 |
Correct |
54 ms |
120152 KB |
n=100 |
12 |
Correct |
56 ms |
120128 KB |
n=100 |
13 |
Correct |
52 ms |
120104 KB |
n=100 |
14 |
Correct |
58 ms |
120184 KB |
n=100 |
15 |
Correct |
54 ms |
120104 KB |
n=100 |
16 |
Correct |
51 ms |
120160 KB |
n=100 |
17 |
Correct |
57 ms |
120140 KB |
n=100 |
18 |
Correct |
52 ms |
120068 KB |
n=100 |
19 |
Correct |
53 ms |
120120 KB |
n=100 |
20 |
Correct |
52 ms |
120176 KB |
n=100 |
21 |
Correct |
51 ms |
120144 KB |
n=100 |
22 |
Correct |
51 ms |
120064 KB |
n=100 |
23 |
Correct |
50 ms |
120144 KB |
n=100 |
24 |
Correct |
55 ms |
120140 KB |
n=100 |
25 |
Correct |
54 ms |
120140 KB |
n=100 |
26 |
Correct |
54 ms |
120044 KB |
n=12 |
27 |
Correct |
53 ms |
120184 KB |
n=100 |
28 |
Correct |
55 ms |
120616 KB |
n=500 |
29 |
Correct |
57 ms |
120648 KB |
n=500 |
30 |
Correct |
61 ms |
120620 KB |
n=500 |
31 |
Correct |
55 ms |
120644 KB |
n=500 |
32 |
Correct |
55 ms |
120640 KB |
n=500 |
33 |
Correct |
56 ms |
120636 KB |
n=500 |
34 |
Correct |
55 ms |
120524 KB |
n=500 |
35 |
Correct |
61 ms |
120776 KB |
n=500 |
36 |
Correct |
53 ms |
120524 KB |
n=500 |
37 |
Correct |
55 ms |
120552 KB |
n=500 |
38 |
Correct |
55 ms |
120524 KB |
n=500 |
39 |
Correct |
54 ms |
120524 KB |
n=500 |
40 |
Correct |
56 ms |
120748 KB |
n=500 |
41 |
Correct |
59 ms |
120524 KB |
n=500 |
42 |
Correct |
55 ms |
120612 KB |
n=500 |
43 |
Correct |
56 ms |
120564 KB |
n=500 |
44 |
Correct |
56 ms |
120612 KB |
n=500 |
45 |
Correct |
56 ms |
120624 KB |
n=500 |
46 |
Correct |
61 ms |
120688 KB |
n=500 |
47 |
Correct |
56 ms |
120624 KB |
n=500 |
48 |
Correct |
55 ms |
120640 KB |
n=500 |
49 |
Correct |
65 ms |
120584 KB |
n=500 |
50 |
Correct |
58 ms |
120604 KB |
n=500 |
51 |
Correct |
55 ms |
120580 KB |
n=500 |
52 |
Correct |
57 ms |
120652 KB |
n=500 |
53 |
Correct |
64 ms |
120608 KB |
n=500 |
54 |
Correct |
55 ms |
120652 KB |
n=500 |
55 |
Correct |
55 ms |
120536 KB |
n=278 |
56 |
Correct |
57 ms |
120588 KB |
n=500 |
57 |
Correct |
59 ms |
120612 KB |
n=500 |
58 |
Correct |
55 ms |
120572 KB |
n=500 |
59 |
Correct |
65 ms |
122572 KB |
n=2000 |
60 |
Correct |
68 ms |
122708 KB |
n=2000 |
61 |
Correct |
72 ms |
122716 KB |
n=2000 |
62 |
Correct |
68 ms |
122668 KB |
n=2000 |
63 |
Correct |
65 ms |
122668 KB |
n=2000 |
64 |
Correct |
67 ms |
122568 KB |
n=2000 |
65 |
Correct |
67 ms |
122572 KB |
n=2000 |
66 |
Correct |
69 ms |
122716 KB |
n=2000 |
67 |
Correct |
68 ms |
122640 KB |
n=2000 |
68 |
Correct |
69 ms |
122664 KB |
n=2000 |
69 |
Correct |
59 ms |
122600 KB |
n=2000 |
70 |
Correct |
58 ms |
122656 KB |
n=2000 |
71 |
Correct |
59 ms |
122664 KB |
n=2000 |
72 |
Correct |
60 ms |
122544 KB |
n=2000 |
73 |
Correct |
60 ms |
122660 KB |
n=2000 |
74 |
Correct |
65 ms |
122508 KB |
n=1844 |
75 |
Correct |
61 ms |
122656 KB |
n=2000 |
76 |
Correct |
65 ms |
122644 KB |
n=2000 |
77 |
Correct |
67 ms |
122592 KB |
n=2000 |
78 |
Correct |
68 ms |
122588 KB |
n=2000 |
79 |
Correct |
64 ms |
122552 KB |
n=2000 |
80 |
Correct |
73 ms |
122636 KB |
n=2000 |
81 |
Correct |
84 ms |
122572 KB |
n=2000 |
82 |
Correct |
62 ms |
122584 KB |
n=2000 |
83 |
Correct |
64 ms |
122608 KB |
n=2000 |
84 |
Correct |
61 ms |
122572 KB |
n=2000 |
85 |
Correct |
63 ms |
122560 KB |
n=2000 |
86 |
Correct |
63 ms |
122664 KB |
n=2000 |
87 |
Correct |
66 ms |
122600 KB |
n=2000 |
88 |
Correct |
65 ms |
122668 KB |
n=2000 |
89 |
Correct |
68 ms |
122612 KB |
n=2000 |
90 |
Correct |
67 ms |
122700 KB |
n=2000 |
91 |
Correct |
57 ms |
122536 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
120024 KB |
n=5 |
2 |
Correct |
55 ms |
120180 KB |
n=100 |
3 |
Correct |
59 ms |
120180 KB |
n=100 |
4 |
Correct |
52 ms |
120076 KB |
n=100 |
5 |
Correct |
53 ms |
120168 KB |
n=100 |
6 |
Correct |
56 ms |
120184 KB |
n=100 |
7 |
Correct |
53 ms |
120184 KB |
n=100 |
8 |
Correct |
62 ms |
120200 KB |
n=100 |
9 |
Correct |
54 ms |
120140 KB |
n=100 |
10 |
Correct |
50 ms |
120168 KB |
n=100 |
11 |
Correct |
54 ms |
120152 KB |
n=100 |
12 |
Correct |
56 ms |
120128 KB |
n=100 |
13 |
Correct |
52 ms |
120104 KB |
n=100 |
14 |
Correct |
58 ms |
120184 KB |
n=100 |
15 |
Correct |
54 ms |
120104 KB |
n=100 |
16 |
Correct |
51 ms |
120160 KB |
n=100 |
17 |
Correct |
57 ms |
120140 KB |
n=100 |
18 |
Correct |
52 ms |
120068 KB |
n=100 |
19 |
Correct |
53 ms |
120120 KB |
n=100 |
20 |
Correct |
52 ms |
120176 KB |
n=100 |
21 |
Correct |
51 ms |
120144 KB |
n=100 |
22 |
Correct |
51 ms |
120064 KB |
n=100 |
23 |
Correct |
50 ms |
120144 KB |
n=100 |
24 |
Correct |
55 ms |
120140 KB |
n=100 |
25 |
Correct |
54 ms |
120140 KB |
n=100 |
26 |
Correct |
54 ms |
120044 KB |
n=12 |
27 |
Correct |
53 ms |
120184 KB |
n=100 |
28 |
Correct |
55 ms |
120616 KB |
n=500 |
29 |
Correct |
57 ms |
120648 KB |
n=500 |
30 |
Correct |
61 ms |
120620 KB |
n=500 |
31 |
Correct |
55 ms |
120644 KB |
n=500 |
32 |
Correct |
55 ms |
120640 KB |
n=500 |
33 |
Correct |
56 ms |
120636 KB |
n=500 |
34 |
Correct |
55 ms |
120524 KB |
n=500 |
35 |
Correct |
61 ms |
120776 KB |
n=500 |
36 |
Correct |
53 ms |
120524 KB |
n=500 |
37 |
Correct |
55 ms |
120552 KB |
n=500 |
38 |
Correct |
55 ms |
120524 KB |
n=500 |
39 |
Correct |
54 ms |
120524 KB |
n=500 |
40 |
Correct |
56 ms |
120748 KB |
n=500 |
41 |
Correct |
59 ms |
120524 KB |
n=500 |
42 |
Correct |
55 ms |
120612 KB |
n=500 |
43 |
Correct |
56 ms |
120564 KB |
n=500 |
44 |
Correct |
56 ms |
120612 KB |
n=500 |
45 |
Correct |
56 ms |
120624 KB |
n=500 |
46 |
Correct |
61 ms |
120688 KB |
n=500 |
47 |
Correct |
56 ms |
120624 KB |
n=500 |
48 |
Correct |
55 ms |
120640 KB |
n=500 |
49 |
Correct |
65 ms |
120584 KB |
n=500 |
50 |
Correct |
58 ms |
120604 KB |
n=500 |
51 |
Correct |
55 ms |
120580 KB |
n=500 |
52 |
Correct |
57 ms |
120652 KB |
n=500 |
53 |
Correct |
64 ms |
120608 KB |
n=500 |
54 |
Correct |
55 ms |
120652 KB |
n=500 |
55 |
Correct |
55 ms |
120536 KB |
n=278 |
56 |
Correct |
57 ms |
120588 KB |
n=500 |
57 |
Correct |
59 ms |
120612 KB |
n=500 |
58 |
Correct |
55 ms |
120572 KB |
n=500 |
59 |
Correct |
65 ms |
122572 KB |
n=2000 |
60 |
Correct |
68 ms |
122708 KB |
n=2000 |
61 |
Correct |
72 ms |
122716 KB |
n=2000 |
62 |
Correct |
68 ms |
122668 KB |
n=2000 |
63 |
Correct |
65 ms |
122668 KB |
n=2000 |
64 |
Correct |
67 ms |
122568 KB |
n=2000 |
65 |
Correct |
67 ms |
122572 KB |
n=2000 |
66 |
Correct |
69 ms |
122716 KB |
n=2000 |
67 |
Correct |
68 ms |
122640 KB |
n=2000 |
68 |
Correct |
69 ms |
122664 KB |
n=2000 |
69 |
Correct |
59 ms |
122600 KB |
n=2000 |
70 |
Correct |
58 ms |
122656 KB |
n=2000 |
71 |
Correct |
59 ms |
122664 KB |
n=2000 |
72 |
Correct |
60 ms |
122544 KB |
n=2000 |
73 |
Correct |
60 ms |
122660 KB |
n=2000 |
74 |
Correct |
65 ms |
122508 KB |
n=1844 |
75 |
Correct |
61 ms |
122656 KB |
n=2000 |
76 |
Correct |
65 ms |
122644 KB |
n=2000 |
77 |
Correct |
67 ms |
122592 KB |
n=2000 |
78 |
Correct |
68 ms |
122588 KB |
n=2000 |
79 |
Correct |
64 ms |
122552 KB |
n=2000 |
80 |
Correct |
73 ms |
122636 KB |
n=2000 |
81 |
Correct |
84 ms |
122572 KB |
n=2000 |
82 |
Correct |
62 ms |
122584 KB |
n=2000 |
83 |
Correct |
64 ms |
122608 KB |
n=2000 |
84 |
Correct |
61 ms |
122572 KB |
n=2000 |
85 |
Correct |
63 ms |
122560 KB |
n=2000 |
86 |
Correct |
63 ms |
122664 KB |
n=2000 |
87 |
Correct |
66 ms |
122600 KB |
n=2000 |
88 |
Correct |
65 ms |
122668 KB |
n=2000 |
89 |
Correct |
68 ms |
122612 KB |
n=2000 |
90 |
Correct |
67 ms |
122700 KB |
n=2000 |
91 |
Correct |
57 ms |
122536 KB |
n=2000 |
92 |
Runtime error |
332 ms |
262144 KB |
Execution killed with signal 9 |
93 |
Halted |
0 ms |
0 KB |
- |