Submission #1112244

#TimeUsernameProblemLanguageResultExecution timeMemory
1112244SteveBroBirthday gift (IZhO18_treearray)C++17
100 / 100
643 ms76360 KiB
#include <bits/stdc++.h> using namespace std; const int nmax=2e5+1; vector <int> g[nmax]; int ab[nmax*8],t[nmax*2]; void build(int nod, int st, int dr) { if(st==dr) ab[nod]=t[st]; else { int mij=(st+dr)/2; build(nod*2,st,mij); build(nod*2+1,mij+1,dr); ab[nod]=min(ab[nod*2],ab[nod*2+1]); } } int k,n; bool ok[nmax]; int prim[nmax],l[nmax]; void tur(int nod) { t[++k]=nod; prim[nod]=k; ok[nod]=true; for(auto x : g[nod]) { if(!ok[x]) { tur(x); t[++k]=nod; } } } int a,b,rez; void qry(int nod, int st, int dr) { if(a<=st&&dr<=b) { rez=min(rez,ab[nod]); } else { int mij=(st+dr)/2; if(a<=mij) qry(nod*2,st,mij); if(b>mij) qry(nod*2+1,mij+1,dr); } } set <int> sol1[nmax],sol2[nmax]; void lca(int x, int y) { a=min(prim[x],prim[y]); b=max(prim[x],prim[y]); rez=n+1; qry(1,1,n*2-1); } std :: set <int> :: iterator it; int main() { ///freopen("test.in","r",stdin); ///freopen("test.out","w",stdout); ios :: sync_with_stdio(false); cin.tie(0); int m,q,i,x,y; cin>>n>>m>>q; for(i=1;i<n;i++) { cin>>x>>y; g[x].push_back(y); g[y].push_back(x); } tur(1); build(1,1,n*2-1); for(i=1;i<=m;i++) { cin>>l[i]; sol1[l[i]].insert(i); } for(i=1;i<m;i++) { lca(l[i],l[i+1]); sol2[rez].insert(i); } int tip,st,dr; bool found; while(q>0) { q--; cin>>tip; if(tip==1) { cin>>x>>y; sol1[l[x]].erase(x); sol1[y].insert(x); if(x!=1) { lca(l[x],l[x-1]); sol2[rez].erase(x-1); lca(y,l[x-1]); sol2[rez].insert(x-1); } if(x!=m) { lca(l[x],l[x+1]); sol2[rez].erase(x); lca(y,l[x+1]); sol2[rez].insert(x); } l[x]=y; } else { cin>>st>>dr>>x; found=false; if(!sol1[x].empty()) { it=sol1[x].lower_bound(st); if(it!=sol1[x].end()&&*it<=dr) { found=true; cout<<*it<<" "<<*it<<'\n'; } } if(!found&&!sol2[x].empty()) { it=sol2[x].lower_bound(st); if(it!=sol2[x].end()&&*it<dr) { found=true; cout<<*it<<" "<<*it+1<<'\n'; } } if(!found) cout<<"-1 -1"<<'\n'; } } 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...