제출 #894339

#제출 시각아이디문제언어결과실행 시간메모리
894339zeta7532Birthday gift (IZhO18_treearray)C++17
56 / 100
4051 ms51868 KiB
#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]--; while(Q--){ ll q; cin >> q; if(q==1){ ll pos,v; cin >> pos >> v; pos--,v--; A[pos]=v; } if(q==2){ ll l,r,v; cin >> l >> r >> v; l--,r--,v--; bool ok=false; ll nowl=l; while(1){ bool loop=false; if(nowl>r) break; ll nowv=A[nowl]; for(ll nowr=nowl;nowr<=r;nowr++){ nowv=lca.query(nowv,A[nowr]); if(lca.query(nowv,v)!=v){ nowl=nowr+1; break; } if(nowv==v){ cout << nowl+1 << " " << nowr+1 << endl; loop=true; ok=true; break; } if(nowr==r) loop=true; } if(loop) break; } if(ok) continue; cout << -1 << " " << -1 << endl; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...