Ref: 55806735
A left-leaning tree with a piece of the class that can be stacked and partially written
#include <iostream>#include <cstdio>using namespaceStdConst intn=600005;intn,m,tot,fa[n],len[n],rt[n],d[n],cnt;Long LongP[n],sum;intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}structqwe{intL,r,dis;Long LongV;} E[n];intHbintXintY) {if(!x| |! YreturnX+y;if(E[X].V<E[Y].V) Swap (x, y); E[X].R=HB (E[x].r,y);if(E[e[x].l].dis<e[e[x].r].dis) swap (E[X].L,E[X].R);if(!E[X].R) e[x].dis=0;ElseE[x].dis=e[e[x].r].dis+1;returnx;}intPopintx) {returnHB (E[X].L,E[X].R);}intMain () {N=read (), M=read (); for(intI=2; i<=n+m;i++) {Fa[i]=read (), Len[i]=read (); Sum+=len[i]; d[fa[i]]++; } for(intI=m+n;i>1; i--) {Long LongL=0, r=0;if(i<=n) { while(--d[i]) Rt[i]=pop (Rt[i]); R=E[RT[I]].V; Rt[i]=pop (Rt[i]); L=E[RT[I]].V; Rt[i]=pop (Rt[i]); } E[++tot].v=l+len[i]; E[++tot].v=r+len[i]; RT[I]=HB (RT[I],HB (Tot,tot-1)); RT[FA[I]]=HB (Rt[fa[i]],rt[i]); } while(d[1]--) rt[1]=pop (rt[1]); while(rt[1]) p[++cnt]=e[rt[1]].v,rt[1]=pop (rt[1]); for(intI=1; i<=cnt;i++) Sum-=p[i]; printf"%lld\n", sum);return 0;}
Bzoj 4585: [Apio2016] Fireworks show "left-leaning tree"