This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define mid (l+r)/2
#define inf 1e15
#define MOD 998244353
#define MX 200005
using namespace std;
vi edges[MX];
int par[19][MX], dep[MX];
void dfs(int node, int pare){
par[0][node]=pare;
for(auto i:edges[node]){
if(i!=pare){
dep[i]=dep[node]+1;
dfs(i, node);
}
}
}
int lca(int v, int u){
if(dep[u]>dep[v]) swap(v,u);
for(int i=dep[v]-dep[u]; i>0; i-=i&-i) v=par[__lg(i&-i)][v];
if(v==u) return v;
for(int i=18; i>=0; i--){
if(par[i][v]!=par[i][u]){
v=par[i][v];
u=par[i][u];
}
}
return par[0][v];
}
void solve(){
int n,m,q; cin >> n >> m >> q;
for(int i=1; i<n; i++){
int a,b; cin >> a >> b;
edges[a].pb(b);
edges[b].pb(a);
}
par[0][1]=1;
dep[1]=0;
dfs(1, 0);
for(int i=1; i<=18; i++){
for(int j=1; j<=n; j++){
par[i][j] = par[i-1][par[i-1][j]];
}
}
int arr[m+1];
set<ii> ans[n+1];
for(int i=1; i<=m; i++){
cin >> arr[i];
ans[arr[i]].insert({i,i});
if(i>1){
ans[lca(arr[i], arr[i-1])].insert({i-1, i});
}
}
for(int i=1; i<=q; i++){
int ty; cin >> ty;
if(ty==1){
int pos,v; cin >> pos >> v;
ans[arr[pos]].erase({pos,pos});
if(pos>1) ans[lca(arr[pos], arr[pos-1])].erase({pos-1,pos});
if(pos<n) ans[lca(arr[pos], arr[pos+1])].erase({pos,pos+1});
arr[pos]=v;
ans[arr[pos]].insert({pos,pos});
if(pos>1) ans[lca(arr[pos], arr[pos-1])].insert({pos-1,pos});
if(pos<n) ans[lca(arr[pos], arr[pos+1])].insert({pos,pos+1});
}
else{
int l,r,v; cin >> l >> r >> v;
auto it=(ans[v].lower_bound({l,l}));
if(it==ans[v].end() || (*it).nd>r) cout << "-1 -1\n";
else{
assert((*it).st>=l && (*it).nd<=r);
cout << (*it).st spc (*it).nd << endl;
}
}
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
/*#else
freopen("248.in","r",stdin);
freopen("248.out","w",stdout);*/
#endif
int t=1;
//cin >> t;
while(t--) solve();
}
# | 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... |