이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Muallif: Mansuraliyev Husanboy Murotali o'g'li >> NamPS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(a) a.begin(), a.end()
#define F first
#define S second
// 0-9 >> 48-57; A-Z>>65-90 and a-z>>97-122 respectively;
const int N=2e5+5;
vector<int> g[N]; int up[N][20],depth[N];
set<int> pos[N],pospair[N];
void dfs(int a, int p){
for(auto u:g[a]){
if(u!=p){
up[u][0]=a;
for(int i=1;i<20;i++){
up[u][i]=up[up[u][i-1]][i-1];
}
depth[u]=depth[a]+1;
dfs(u,a);
}
}
}
int lca(int a, int b){
if(depth[a]<depth[b]){
swap(a,b);
}
int k=depth[a]-depth[b];
for(int i=0;i<20;i++){
if((1<<i)&k){
a=up[a][i];
}
}
if(a==b) return a;
for(int i=19;i>=0;i--){
if(up[a][i]!=up[b][i]){
a=up[a][i],b=up[b][i];
}
}
return up[a][0];
}
void solve(){
int n,m,q; cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
int x,y; cin>>x>>y; g[x].push_back(y); g[y].push_back(x);
}
dfs(1,1);
// for(int i=1;i<=n;i++) cout<<depth[i]<<endl;
int a[n+1];
for(int i=1;i<=m;i++){
cin>>a[i]; pos[a[i]].insert(i);
}
for(int i=1;i<m;i++){
pospair[lca(a[i],a[i+1])].insert(i);
}
/* int aa,bb; cin>>aa>>bb;
cout<<lca(aa,bb)<<" lca"<<endl;*/
while(q--){
int type; cin>>type;
if(type==1){
int x,y; cin>>x>>y;
pos[a[x]].erase(x);
pos[y].insert(x);
if(x>1){
pospair[lca(a[x-1],a[x])].erase(x-1);
pospair[lca(a[x-1],y)].insert(x-1);
}
if(x<m){
pospair[lca(a[x],a[x+1])].erase(x);
pospair[lca(y,a[x+1])].insert(x);
}
a[x]=y;
}else{
int l,r,x; cin>>l>>r>>x;
auto pp=pospair[x].lower_bound(l);
/* for(int i=1;i<=n;i++){
cout<<i<<": ";
for(auto u:pospair[i]){
cout<<u<<' ';
}cout<<endl;
}*/
auto p=pos[x].lower_bound(l);
if(p!=pos[x].end()&&*p<=r){
cout<<*p<<' '<<*p<<"\n";
}else
if(pp!=pospair[x].end()&&*pp<r){
cout<<*pp<<' '<<*pp+1<<"\n";
}else cout<<"-1 -1\n";
}
}
}
int main(){
ios;
//int t=1; cin>>t; while(t--)
solve();
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |