#include using namespace std; using Int = long long; //BEGIN CUT HERE struct HLDecomposition { int n,pos; vector > G; vector vid, head, sub, par, dep, inv, type; HLDecomposition(){} HLDecomposition(int n): n(n),pos(0),G(n),vid(n,-1),head(n),sub(n,1), par(n,-1),dep(n,0),inv(n),type(n){} void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } void build(vector rs={0}) { int c=0; for(int r:rs){ dfs_sz(r); head[r]=r; dfs_hld(r,c++); } } void dfs_sz(int v) { for(int &u:G[v]){ if(u==par[v]) continue; par[u]=v; dep[u]=dep[v]+1; dfs_sz(u); sub[v]+=sub[u]; if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]); } } void dfs_hld(int v,int c) { vid[v]=pos++; inv[vid[v]]=v; type[v]=c; for(int u:G[v]){ if(u==par[v]) continue; head[u]=(u==G[v][0]?head[v]:u); dfs_hld(u,c); } } // for_each(vertex) // [l,r] <- attention!! template void for_each(int u, int v, const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); f(max(vid[head[v]],vid[u]),vid[v]); if(head[u]!=head[v]) v=par[head[v]]; else break; } } template T for_each(int u,int v,T ti,const Q &q,const F &f){ T l=ti,r=ti; while(1){ if(vid[u]>vid[v]){ swap(u,v); swap(l,r); } l=f(l,q(max(vid[head[v]],vid[u]),vid[v])); if(head[u]!=head[v]) v=par[head[v]]; else break; } return f(l,r); } // for_each(edge) // [l,r] <- attention!! template void for_each_edge(int u, int v,const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]!=head[v]){ f(vid[head[v]],vid[v]); v=par[head[v]]; }else{ if(u!=v) f(vid[u]+1,vid[v]); break; } } } int lca(int u,int v){ while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]==head[v]) return u; v=par[head[v]]; } } int distance(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } }; //END CUT HERE struct BiconectedGraph{ typedef pair P; int n; vector > G,C,T; vector ord,low,belong; vector

B; BiconectedGraph(){} BiconectedGraph(int sz):n(sz),G(sz),C(sz),T(sz){} void add_edge(int u,int v){ G[u].push_back(v); G[v].push_back(u); } void input(int m,int offset=0){ int a,b; for(int i=0;i>a>>b; add_edge(a+offset,b+offset); } } bool is_bridge(int u,int v){ if(ord[u]>ord[v]) swap(u,v); return ord[u]=0){ low[v]=min(low[v],ord[u]); }else{ dfs(u,v,k); low[v]=min(low[v],low[u]); } if(is_bridge(u,v)) B.push_back(P(u,v)); } } void fill_component(int c,int v){ C[c].push_back(v); belong[v]=c; for(int u:G[v]){ if(belong[u]>=0||is_bridge(u,v)) continue; fill_component(c,u); } } void add_component(int v,int &k){ if(belong[v]>=0) return; fill_component(k++,v); } int build(){ int k=0; ord.resize(n); low.resize(n); belong.resize(n); fill(ord.begin(),ord.end(),-1); fill(belong.begin(),belong.end(),-1); for(int v=0;v=0) continue; dfs(v,-1,k); } k=0; for(int i=0;i<(int)B.size();i++){ add_component(B[i].first,k); add_component(B[i].second,k); } for(int v=0;v struct SegmentTree{ using F = function; using G = function; int n; F f; G g; T ti; vector dat; SegmentTree(){}; SegmentTree(int n_,F f,G g,T ti): f(f),g(g),ti(ti){ init(n_); } void init(int n_){ n=1; while(n v){ for(int i=0;i>=1; dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } } inline T query(int a,int b){ T vl=ti,vr=ti; for(int l=a+n,r=b+n;l>=1,r>>=1) { if(l&1) vl=f(vl,dat[l++]); if(r&1) vr=f(dat[--r],vr); } return f(vl,vr); } template int find(int a,int b,C &check,int k,int l,int r){ if(!check(dat[k])||r<=a||b<=l) return -1; if(k>=n) return k-n; int m=(l+r)>>1; int vl=find(a,b,check,(k<<1)|0,l,m); if(~vl) return vl; return find(a,b,check,(k<<1)|1,m,r); } template int find(int a,int b,C &check){ return find(a,b,check,1,0,n); } }; template struct Chien{ 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; Chien(int n_,F f,G g,H h,T ti,E ei): f(f),g(g),h(h),ti(ti),ei(ei){init(n_);} void init(int n_){ n=1;height=0; while(n v){ for(int i=0;i>i); for(int i=height;i;i--) eval(b>>i); 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); } while(a>>=1) dat[a]=f(reflect((a<<1)|0),reflect((a<<1)|1)); while(b>>=1) dat[b]=f(reflect((b<<1)|0),reflect((b<<1)|1)); } T query(int a,int b){ a+=n;b+=n-1; for(int i=height;i;i--) eval(a>>i); for(int i=height;i;i--) eval(b>>i); 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); } }; //INSERT ABOVE HERE signed AOJ_GRL5C(){ int n; cin>>n; HLDecomposition lca(n); for(int i=0;i>k; for(int j=0;j>c; lca.add_edge(i,c); } } lca.build(); int q; cin>>q; while(q--){ int u,v; cin>>u>>v; cout< rmq(V, [](int a,int b){return max(a,b);}, [](int a,int b){a++;return b;}, -1); vector > pq(V); map m; int num=0; for(int i=0;iint{return rmq.query(l, r + 1);}; int ans=hl.for_each(s, t, -1, f, rmq.f); printf("%d\n",ans); if(~ans){ int k=m[ans]; pq[k].pop(); rmq.update(k,(!pq[k].empty()?pq[k].top():-1)); num--; } } } return 0; } /* verified on 2017/12/31 https://yukicoder.me/problems/no/529 */ signed AOJ_2667(){ int n,q; scanf("%d %d",&n,&q); HLDecomposition hld(n); for(int i=1;i; auto f=[](P a,P b){return P(a.first+b.first,a.second+b.second);}; auto g=[](P a,Int b){return P(a.first+b*a.second,a.second);}; auto h=[](Int a,Int b){return a+b;}; Chien seg(n,f,g,h,P(0,0),0); seg.build(n,vector

(n,P(0,1))); for(int i=0;i w(n); for(int i=0;i; for(int i=0;i; T ti(-1,-1,-1,-1,-1,-1,-1); P ei(-1,-114514); auto &par=hld.par; auto &vid=hld.vid;; auto con=[&](int a,int b){ return par[a]==b||par[b]==a; }; auto f=[&](T a,T b){ if(a>b) swap(a,b); if(get<0>(a)<0) return b; if(con(get<0>(a),get<1>(b))) swap(a,b); int al,ar,as,ava,avi,avl,avr; tie(al,ar,as,ava,avi,avl,avr)=a; int bl,br,bs,bva,bvi,bvl,bvr; tie(bl,br,bs,bva,bvi,bvl,bvr)=b; if(!con(ar,bl)){ if(con(ar,br)){ swap(bl,br); swap(bvl,bvr); }else if(con(al,bl)){ swap(al,ar); swap(avl,avr); }else{ return ti; } } int cl=al,cr=br,cs=as+bs; int cva=ava+bva,cvi=max(avi,bvi),cvl=avl,cvr=bvr; cvi=max(cvi,avr+bvl); cvl=max(cvl,ava+bvl); cvr=max(cvr,avr+bva); return T(cl,cr,cs,cva,cvi,cvl,cvr); }; auto g=[&](T a,P p){ if(p==ei) return a; int al,ar,as,ava,avi,avl,avr; tie(al,ar,as,ava,avi,avl,avr)=a; int v=p.first,b=p.second; if(~v) al=ar=v,as=1; if(b>=0) return T(al,ar,as,b*as,b*as,b*as,b*as); return T(al,ar,as,b*as,b,b,b); }; auto h=[&](P a,P b){get<0>(a);return b;}; Chien seg(n,f,g,h,ti,ei); vector vt(n); for(int i=0;i