Left-leaning tree.
Each child node maintains a large heap, traversing a son to merge himself, and merging to find that the money is not enough to delete the top of the team.
//Achen#include <algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<vector>#include<queue>#include<ctime>#include<cmath>Const intn=100007; typedefLong LongLL;using namespacestd;intn,root,rt[n],ch[n][2],sz[n],f[n]; LL m,v[n],cs[n],w[n],ans;template<typename t>voidRead (T &x) {CharCh=getchar (); x=0; T f=1; while(ch!='-'&& (ch<'0'|| Ch>'9')) ch=GetChar (); if(ch=='-') f=-1, ch=GetChar (); for(; ch>='0'&&ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; x*=F;}intEcnt,fir[n],nxt[n],to[n],dis[n];voidAddintUintv) {nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;}#defineLC Ch[x][0]#defineRC Ch[x][1]voidUpdateintx) {sz[x]=sz[lc]+sz[rc]+1; v[x]=v[lc]+v[rc]+cs[x];}intMergeintXinty) {if(! (X*y))returnx^y; if(cs[x]<Cs[y]) swap (x, y); RC=merge (Rc,y); if(dis[lc]<DIS[RC]) swap (LC,RC); if(!RC) dis[x]=0; Elsedis[x]=dis[rc]+1; Update (x); returnx;}voidDfsintx) { for(intI=fir[x];i;i=Nxt[i]) {DFS (to[i]); RT[X]=merge (Rt[x],rt[to[i]]); while(v[rt[x]]>m) rt[x]=merge (ch[rt[x]][0],ch[rt[x]][1]); } ans=max (ans,w[x]*Sz[rt[x]]);}intMain () {#ifdef DEBUG freopen (". in","R", stdin); Freopen (". out","W", stdout);#endifread (n); read (m); for(intI=1; i<=n;i++) {read (f[i]); Read (Cs[i]); Read (W[i]); if(!f[i]) root=i; ElseAdd (f[i],i); Rt[i]=i; V[i]=Cs[i]; Sz[i]=1; } dfs (root); printf ("%lld\n", ans); return 0;}
View Code
"BZOJ2809" "APIO2012" dispatching