#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)
using Graph = vector<vector<ll>>;
struct LCA{
vector<vector<ll>> par;
vector<ll> dis;
LCA(const Graph &G,ll root=0){init (G,root);}
void init(const Graph &G,ll root=0){
ll v=G.size();
ll k=1;
while((1<<k)<v) k++;
par.assign(k,vector<ll>(v,-1));
dis.assign(v,-1);
dfs(G,root,-1,0);
for(ll i=0;i+1<k;i++){
for(ll j=0;j<v;j++){
if(par[i][j]<0) par[i+1][j]=-1;
else par[i+1][j]=par[i][par[i][j]];
}
}
}
void dfs(const Graph &G,ll v,ll p,ll d){
par[0][v]=p;
dis[v]=d;
for(ll nv:G[v]){
if(nv!=p) dfs(G,nv,v,d+1);
}
}
ll query(ll u,ll v){
if(dis[u]<dis[v]) swap(u,v);
ll K=par.size();
for(ll k=0;k<K;k++){
if((dis[u]-dis[v])>>k&1){
u=par[k][u];
}
}
if(u==v) return u;
for(ll k=K-1;k>=0;k--){
if(par[k][u]!=par[k][v]){
u=par[k][u];
v=par[k][v];
}
}
return par[0][u];
}
ll get_dis(ll u,ll v){return dis[u]+dis[v]-2*dis[query(u,v)];}
};
int main() {
ll N,M,Q;
cin >> N >> M >> Q;
Graph G(N);
rep(i,N-1){
ll u,v;
cin >> u >> v;
u--,v--;
G[u].push_back(v);
G[v].push_back(u);
}
auto lca=LCA(G);
vector<ll> A(M);
rep(i,M) cin >> A[i],A[i]--;
set<pair<ll,pair<ll,ll>>> s;
rep(i,M) s.insert({A[i],{i,i}});
rep(i,M-1) s.insert({lca.query(A[i],A[i+1]),{i,i+1}});
s.insert({M,{M,M}});
while(Q--){
ll q;
cin >> q;
if(q==1){
ll i,v;
cin >> i >> v;
i--,v--;
s.erase({A[i],{i,i}});
if(i>0) s.erase({lca.query(A[i-1],A[i]),{i-1,i}});
if(i<M-1) s.erase({lca.query(A[i],A[i+1]),{i,i+1}});
A[i]=v;
s.insert({A[i],{i,i}});
if(i>0) s.insert({lca.query(A[i-1],A[i]),{i-1,i}});
if(i<M-1) s.insert({lca.query(A[i],A[i+1]),{i,i+1}});
}
if(q==2){
ll l,r,v;
cin >> l >> r >> v;
l--,r--,v--;
auto it=*s.lower_bound({v,{l,l}});
if(it.fi==v&&it.se.se<=r){
cout << it.se.fi+1 << " " << it.se.se+1 << endl;
}else cout << -1 << " " << -1 << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
344 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
344 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
344 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
1 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
344 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
1 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
344 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
1 ms |
348 KB |
n=100 |
23 |
Correct |
1 ms |
344 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
544 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
344 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
344 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
344 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
1 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
344 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
1 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
344 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
1 ms |
348 KB |
n=100 |
23 |
Correct |
1 ms |
344 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
544 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
2 ms |
348 KB |
n=500 |
29 |
Correct |
2 ms |
344 KB |
n=500 |
30 |
Correct |
2 ms |
348 KB |
n=500 |
31 |
Correct |
2 ms |
348 KB |
n=500 |
32 |
Correct |
2 ms |
348 KB |
n=500 |
33 |
Correct |
2 ms |
344 KB |
n=500 |
34 |
Correct |
2 ms |
348 KB |
n=500 |
35 |
Correct |
2 ms |
348 KB |
n=500 |
36 |
Correct |
2 ms |
348 KB |
n=500 |
37 |
Correct |
2 ms |
348 KB |
n=500 |
38 |
Correct |
2 ms |
348 KB |
n=500 |
39 |
Correct |
2 ms |
448 KB |
n=500 |
40 |
Correct |
2 ms |
344 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
344 KB |
n=500 |
43 |
Correct |
2 ms |
348 KB |
n=500 |
44 |
Correct |
2 ms |
608 KB |
n=500 |
45 |
Correct |
2 ms |
348 KB |
n=500 |
46 |
Correct |
2 ms |
344 KB |
n=500 |
47 |
Correct |
2 ms |
604 KB |
n=500 |
48 |
Correct |
2 ms |
600 KB |
n=500 |
49 |
Correct |
2 ms |
348 KB |
n=500 |
50 |
Correct |
2 ms |
348 KB |
n=500 |
51 |
Correct |
2 ms |
348 KB |
n=500 |
52 |
Correct |
2 ms |
376 KB |
n=500 |
53 |
Correct |
2 ms |
348 KB |
n=500 |
54 |
Correct |
2 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
348 KB |
n=278 |
56 |
Correct |
2 ms |
348 KB |
n=500 |
57 |
Correct |
2 ms |
344 KB |
n=500 |
58 |
Correct |
2 ms |
348 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
344 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
344 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
344 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
1 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
344 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
1 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
344 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
1 ms |
348 KB |
n=100 |
23 |
Correct |
1 ms |
344 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
544 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
2 ms |
348 KB |
n=500 |
29 |
Correct |
2 ms |
344 KB |
n=500 |
30 |
Correct |
2 ms |
348 KB |
n=500 |
31 |
Correct |
2 ms |
348 KB |
n=500 |
32 |
Correct |
2 ms |
348 KB |
n=500 |
33 |
Correct |
2 ms |
344 KB |
n=500 |
34 |
Correct |
2 ms |
348 KB |
n=500 |
35 |
Correct |
2 ms |
348 KB |
n=500 |
36 |
Correct |
2 ms |
348 KB |
n=500 |
37 |
Correct |
2 ms |
348 KB |
n=500 |
38 |
Correct |
2 ms |
348 KB |
n=500 |
39 |
Correct |
2 ms |
448 KB |
n=500 |
40 |
Correct |
2 ms |
344 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
344 KB |
n=500 |
43 |
Correct |
2 ms |
348 KB |
n=500 |
44 |
Correct |
2 ms |
608 KB |
n=500 |
45 |
Correct |
2 ms |
348 KB |
n=500 |
46 |
Correct |
2 ms |
344 KB |
n=500 |
47 |
Correct |
2 ms |
604 KB |
n=500 |
48 |
Correct |
2 ms |
600 KB |
n=500 |
49 |
Correct |
2 ms |
348 KB |
n=500 |
50 |
Correct |
2 ms |
348 KB |
n=500 |
51 |
Correct |
2 ms |
348 KB |
n=500 |
52 |
Correct |
2 ms |
376 KB |
n=500 |
53 |
Correct |
2 ms |
348 KB |
n=500 |
54 |
Correct |
2 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
348 KB |
n=278 |
56 |
Correct |
2 ms |
348 KB |
n=500 |
57 |
Correct |
2 ms |
344 KB |
n=500 |
58 |
Correct |
2 ms |
348 KB |
n=500 |
59 |
Correct |
6 ms |
856 KB |
n=2000 |
60 |
Correct |
7 ms |
1116 KB |
n=2000 |
61 |
Correct |
7 ms |
856 KB |
n=2000 |
62 |
Correct |
9 ms |
860 KB |
n=2000 |
63 |
Correct |
6 ms |
860 KB |
n=2000 |
64 |
Correct |
6 ms |
856 KB |
n=2000 |
65 |
Correct |
6 ms |
860 KB |
n=2000 |
66 |
Correct |
6 ms |
860 KB |
n=2000 |
67 |
Correct |
6 ms |
860 KB |
n=2000 |
68 |
Correct |
6 ms |
860 KB |
n=2000 |
69 |
Correct |
6 ms |
860 KB |
n=2000 |
70 |
Correct |
6 ms |
860 KB |
n=2000 |
71 |
Correct |
6 ms |
860 KB |
n=2000 |
72 |
Correct |
8 ms |
1232 KB |
n=2000 |
73 |
Correct |
8 ms |
860 KB |
n=2000 |
74 |
Correct |
5 ms |
860 KB |
n=1844 |
75 |
Correct |
7 ms |
860 KB |
n=2000 |
76 |
Correct |
6 ms |
960 KB |
n=2000 |
77 |
Correct |
6 ms |
832 KB |
n=2000 |
78 |
Correct |
6 ms |
860 KB |
n=2000 |
79 |
Correct |
5 ms |
860 KB |
n=2000 |
80 |
Correct |
7 ms |
860 KB |
n=2000 |
81 |
Correct |
6 ms |
1092 KB |
n=2000 |
82 |
Correct |
5 ms |
856 KB |
n=2000 |
83 |
Correct |
9 ms |
860 KB |
n=2000 |
84 |
Correct |
6 ms |
860 KB |
n=2000 |
85 |
Correct |
8 ms |
1080 KB |
n=2000 |
86 |
Correct |
6 ms |
856 KB |
n=2000 |
87 |
Correct |
6 ms |
856 KB |
n=2000 |
88 |
Correct |
6 ms |
1116 KB |
n=2000 |
89 |
Correct |
6 ms |
1116 KB |
n=2000 |
90 |
Correct |
6 ms |
1116 KB |
n=2000 |
91 |
Correct |
6 ms |
860 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
344 KB |
n=100 |
3 |
Correct |
1 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
1 ms |
344 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
344 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
1 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
1 ms |
348 KB |
n=100 |
13 |
Correct |
1 ms |
344 KB |
n=100 |
14 |
Correct |
1 ms |
348 KB |
n=100 |
15 |
Correct |
1 ms |
348 KB |
n=100 |
16 |
Correct |
1 ms |
344 KB |
n=100 |
17 |
Correct |
1 ms |
344 KB |
n=100 |
18 |
Correct |
1 ms |
348 KB |
n=100 |
19 |
Correct |
1 ms |
348 KB |
n=100 |
20 |
Correct |
1 ms |
348 KB |
n=100 |
21 |
Correct |
1 ms |
344 KB |
n=100 |
22 |
Correct |
1 ms |
348 KB |
n=100 |
23 |
Correct |
1 ms |
344 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
1 ms |
544 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
2 ms |
348 KB |
n=500 |
29 |
Correct |
2 ms |
344 KB |
n=500 |
30 |
Correct |
2 ms |
348 KB |
n=500 |
31 |
Correct |
2 ms |
348 KB |
n=500 |
32 |
Correct |
2 ms |
348 KB |
n=500 |
33 |
Correct |
2 ms |
344 KB |
n=500 |
34 |
Correct |
2 ms |
348 KB |
n=500 |
35 |
Correct |
2 ms |
348 KB |
n=500 |
36 |
Correct |
2 ms |
348 KB |
n=500 |
37 |
Correct |
2 ms |
348 KB |
n=500 |
38 |
Correct |
2 ms |
348 KB |
n=500 |
39 |
Correct |
2 ms |
448 KB |
n=500 |
40 |
Correct |
2 ms |
344 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
344 KB |
n=500 |
43 |
Correct |
2 ms |
348 KB |
n=500 |
44 |
Correct |
2 ms |
608 KB |
n=500 |
45 |
Correct |
2 ms |
348 KB |
n=500 |
46 |
Correct |
2 ms |
344 KB |
n=500 |
47 |
Correct |
2 ms |
604 KB |
n=500 |
48 |
Correct |
2 ms |
600 KB |
n=500 |
49 |
Correct |
2 ms |
348 KB |
n=500 |
50 |
Correct |
2 ms |
348 KB |
n=500 |
51 |
Correct |
2 ms |
348 KB |
n=500 |
52 |
Correct |
2 ms |
376 KB |
n=500 |
53 |
Correct |
2 ms |
348 KB |
n=500 |
54 |
Correct |
2 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
348 KB |
n=278 |
56 |
Correct |
2 ms |
348 KB |
n=500 |
57 |
Correct |
2 ms |
344 KB |
n=500 |
58 |
Correct |
2 ms |
348 KB |
n=500 |
59 |
Correct |
6 ms |
856 KB |
n=2000 |
60 |
Correct |
7 ms |
1116 KB |
n=2000 |
61 |
Correct |
7 ms |
856 KB |
n=2000 |
62 |
Correct |
9 ms |
860 KB |
n=2000 |
63 |
Correct |
6 ms |
860 KB |
n=2000 |
64 |
Correct |
6 ms |
856 KB |
n=2000 |
65 |
Correct |
6 ms |
860 KB |
n=2000 |
66 |
Correct |
6 ms |
860 KB |
n=2000 |
67 |
Correct |
6 ms |
860 KB |
n=2000 |
68 |
Correct |
6 ms |
860 KB |
n=2000 |
69 |
Correct |
6 ms |
860 KB |
n=2000 |
70 |
Correct |
6 ms |
860 KB |
n=2000 |
71 |
Correct |
6 ms |
860 KB |
n=2000 |
72 |
Correct |
8 ms |
1232 KB |
n=2000 |
73 |
Correct |
8 ms |
860 KB |
n=2000 |
74 |
Correct |
5 ms |
860 KB |
n=1844 |
75 |
Correct |
7 ms |
860 KB |
n=2000 |
76 |
Correct |
6 ms |
960 KB |
n=2000 |
77 |
Correct |
6 ms |
832 KB |
n=2000 |
78 |
Correct |
6 ms |
860 KB |
n=2000 |
79 |
Correct |
5 ms |
860 KB |
n=2000 |
80 |
Correct |
7 ms |
860 KB |
n=2000 |
81 |
Correct |
6 ms |
1092 KB |
n=2000 |
82 |
Correct |
5 ms |
856 KB |
n=2000 |
83 |
Correct |
9 ms |
860 KB |
n=2000 |
84 |
Correct |
6 ms |
860 KB |
n=2000 |
85 |
Correct |
8 ms |
1080 KB |
n=2000 |
86 |
Correct |
6 ms |
856 KB |
n=2000 |
87 |
Correct |
6 ms |
856 KB |
n=2000 |
88 |
Correct |
6 ms |
1116 KB |
n=2000 |
89 |
Correct |
6 ms |
1116 KB |
n=2000 |
90 |
Correct |
6 ms |
1116 KB |
n=2000 |
91 |
Correct |
6 ms |
860 KB |
n=2000 |
92 |
Correct |
1178 ms |
76924 KB |
n=200000 |
93 |
Correct |
1820 ms |
80392 KB |
n=200000 |
94 |
Correct |
1805 ms |
82120 KB |
n=200000 |
95 |
Correct |
1117 ms |
76724 KB |
n=200000 |
96 |
Correct |
1154 ms |
77056 KB |
n=200000 |
97 |
Correct |
1857 ms |
79772 KB |
n=200000 |
98 |
Correct |
1141 ms |
76656 KB |
n=200000 |
99 |
Correct |
1401 ms |
76912 KB |
n=200000 |
100 |
Correct |
1217 ms |
76736 KB |
n=200000 |
101 |
Correct |
1734 ms |
82572 KB |
n=200000 |
102 |
Correct |
816 ms |
78204 KB |
n=200000 |
103 |
Correct |
787 ms |
78236 KB |
n=200000 |
104 |
Correct |
794 ms |
78132 KB |
n=200000 |
105 |
Correct |
793 ms |
78800 KB |
n=200000 |
106 |
Correct |
828 ms |
78312 KB |
n=200000 |
107 |
Correct |
783 ms |
78400 KB |
n=200000 |
108 |
Correct |
1415 ms |
77408 KB |
n=200000 |
109 |
Correct |
1335 ms |
76956 KB |
n=200000 |
110 |
Correct |
1331 ms |
77112 KB |
n=200000 |
111 |
Correct |
1076 ms |
76352 KB |
n=200000 |
112 |
Correct |
1760 ms |
82344 KB |
n=200000 |
113 |
Correct |
1807 ms |
79672 KB |
n=200000 |
114 |
Correct |
1062 ms |
76616 KB |
n=200000 |
115 |
Correct |
1893 ms |
78360 KB |
n=200000 |
116 |
Correct |
1152 ms |
77012 KB |
n=200000 |
117 |
Correct |
1760 ms |
82568 KB |
n=200000 |
118 |
Correct |
1900 ms |
78892 KB |
n=200000 |
119 |
Correct |
1096 ms |
77200 KB |
n=200000 |
120 |
Correct |
1609 ms |
81476 KB |
n=200000 |
121 |
Correct |
1618 ms |
82192 KB |
n=200000 |
122 |
Correct |
1696 ms |
82380 KB |
n=200000 |
123 |
Correct |
795 ms |
78388 KB |
n=200000 |
124 |
Correct |
295 ms |
19808 KB |
n=25264 |