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>
#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;
}
# | 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... |