#ifndef call_from_test #include using namespace std; #define call_from_test #ifndef call_from_test #include using namespace std; #endif //BEGIN CUT HERE struct LevelAncestor{ int n,h; vector > G,par,lad; vector dep,nxt,len,pth,ord,hs; LevelAncestor(){} LevelAncestor(int n): n(n),G(n),dep(n),nxt(n,-1),len(n),pth(n),ord(n),hs(n+1,0){ h=1; while((1<(n,-1)); for(int i=2;i<=n;i++) hs[i]=hs[i>>1]+1; } void add_edge(int u,int v){ G[u].emplace_back(v); G[v].emplace_back(u); } void dfs(int v,int p,int d,int f){ if(nxt[v]<0){ par[0][nxt[v]=v]=p; len[v]=dep[v]=d; for(int u:G[v]){ if(u==p) continue; dfs(u,v,d+1,0); if(len[v]dep[v]) swap(u,v); for(int k=0;k>k&1){ v=par[k][v]; } } if(u==v) return u; for(int k=h-1;k>=0;k--){ if(par[k][u]!=par[k][v]){ u=par[k][u]; v=par[k][v]; } } return par[0][u]; } int up(int v,int d){ if(d==0) return v; v=par[hs[d]][v]; d-=1LL< ls; vector> H; EulerTourForBFS(int n):super(n),ls(n),H(n){} using super::par; using super::dep; void build(int r=0){ super::build(r); int pos=0; queue que; que.emplace(r); while(!que.empty()){ int v=que.front();que.pop(); ls[v]=pos++; H[dep[v]].emplace_back(v); for(int u:super::G[v]){ if(u==par[0][v]) continue; que.emplace(u); } } } int idx(int v){return ls[v];} int find(int v,int d,int a){ int l=-1,r=H[d].size(); while(l+1>1; int p=super::up(H[d][m],d-dep[v]); if(ls[v]+a<=ls[p]) r=m; else l=m; } return ls[H[d][0]]+r; } template void exec(int v,int d,F f){ if(dep[v]+d>=n or H[dep[v]+d].empty()) return; int l=find(v,dep[v]+d,0); int r=find(v,dep[v]+d,1); if(l using namespace std; #endif //BEGIN CUT HERE template struct SegmentTree{ using F = function; using G = function; using H = function; int n,height; F f; G g; H h; T ti; E ei; vector dat; vector laz; SegmentTree(F f,G g,H h,T ti,E ei): f(f),g(g),h(h),ti(ti),ei(ei){} void init(int n_){ n=1;height=0; while(n &v){ int n_=v.size(); init(n_); for(int i=0;i>i); } inline void recalc(int k){ while(k>>=1) dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1)); } void update(int a,int b,E x){ if(a>=b) return; thrust(a+=n); thrust(b+=n-1); for(int l=a,r=b+1;l>=1,r>>=1){ if(l&1) laz[l]=h(laz[l],x),l++; if(r&1) --r,laz[r]=h(laz[r],x); } recalc(a); recalc(b); } void set_val(int a,T x){ thrust(a+=n); dat[a]=x;laz[a]=ei; recalc(a); } T query(int a,int b){ if(a>=b) return ti; thrust(a+=n); thrust(b+=n-1); T vl=ti,vr=ti; for(int l=a,r=b+1;l>=1,r>>=1) { if(l&1) vl=f(vl,reflect(l++)); if(r&1) vr=f(reflect(--r),vr); } return f(vl,vr); } template int find(int st,C &check,T &acc,int k,int l,int r){ if(l+1==r){ acc=f(acc,reflect(k)); return check(acc)?k-n:-1; } propagate(k); int m=(l+r)>>1; if(m<=st) return find(st,check,acc,(k<<1)|1,m,r); if(st<=l&&!check(f(acc,dat[k]))){ acc=f(acc,dat[k]); return -1; } int vl=find(st,check,acc,(k<<1)|0,l,m); if(~vl) return vl; return find(st,check,acc,(k<<1)|1,m,r); } template int find(int st,C &check){ T acc=ti; return find(st,check,acc,1,0,n); } }; //END CUT HERE #ifndef call_from_test signed CFR569_C(){ cin.tie(0); ios::sync_with_stdio(0); int n,m; cin>>n>>m; vector as(n),bs(m); for(int i=0;i>as[i]; for(int i=0;i>bs[i]; auto f=[](int a,int b){return max(a,b);}; auto g=[](int a,int b){return a+b;}; int ti=0,ei=0; SegmentTree seg(f,g,g,ti,ei); const int sz = 1<<20; seg.build(vector(sz,0)); for(int i=0;i>q; auto check=[](int d){return d>0;}; for(int i=0;i>t>>k>>v; k--; if(t==1){ seg.update(sz-as[k],sz,-1); as[k]=v; seg.update(sz-as[k],sz,+1); } if(t==2){ seg.update(sz-bs[k],sz,+1); bs[k]=v; seg.update(sz-bs[k],sz,-1); } int pos=seg.find(0,check); cout<<(pos<0?pos:sz-pos)<<"\n"; } cout<>n; EulerTourForBFS G(n); for(int i=1;i>u>>v; G.add_edge(u,v); } G.build(); vector as(n); for(int i=0;i>as[i]; using ll = long long; auto f=[&](ll a,ll b){return a+b;}; auto g=[&](ll a,ll b){return a*b;}; SegmentTree seg(f,g,g,0,1); vector vs(n); for(int i=0;i>q; for(int i=0;i>x; ll sum=0; auto apply= [&](int l,int r){ sum+=seg.query(l,r); seg.update(l,r,0); }; int p=G.par[0][x]; if(~p){ int pp=G.par[0][p]; if(~pp) G.exec(pp,0,apply); G.exec(p,0,apply); G.exec(p,1,apply); } G.exec(x,0,apply); G.exec(x,1,apply); G.exec(x,2,apply); seg.set_val(G.idx(x),sum); cout<