Left side of the tree to mark, No.
#include <cstdio>#include<vector>using namespaceStd;typedefLong Longll;Const intN =300005;intn,m,e,x,tt,c[n],d[n],a[n],hd[n],nxt[n],to[n],rt[n],f[n],g[n];ll H[n],v[n],s[n];vector<int>Vc[n];structnd {intL,r,d,id; ll W,ad,mu;} T[n];voidAddintXintY) {To[++e] = y, nxt[e] = hd[x], hd[x] =e;}voidPdintx) {t[t[x].l].mu*= t[x].mu, T[t[x].l].ad *= t[x].mu, T[T[X].L].W *=t[x].mu; T[t[x].r].mu*= t[x].mu, T[t[x].r].ad *= t[x].mu, T[T[X].R].W *=t[x].mu; T[t[x].l].ad+ = T[x].ad, t[t[x].r].ad + = t[x].ad, t[t[x].l].w + = t[x].ad, T[T[X].R].W + =T[x].ad; T[x].mu=1, T[x].ad =0;}intMrgintXinty) {if(!x | |!y)returnx+y; if(T[x].w >t[y].w) Swap (x, y); PD (x); T[X].R=MRG (T[X].R, y); if(T[t[x].l].d <t[t[x].r].d) Swap (T[X].L, T[X].R); T[X].D= t[t[x].r].d+1; returnx;}voidDfsintx) { for(inti =0; I < vc[x].size (); i++) t[++tt].id = Vc[x][i], T[TT].W = S[vc[x][i]], t[tt].mu =1, rt[x] =MRG (rt[x], TT); for(inti = hd[x]; I i = Nxt[i]) d[to[i] = d[x]+1, DFS (To[i]), rt[x] =MRG (Rt[x], rt[to[i]); while(Rt[x] && T[RT[X]].W <h[x]) f[x]+ +, G[t[rt[x]].id] = x, PD (Rt[x]), rt[x] =MRG (T[RT[X]].L, T[RT[X]].R); if(A[x]) t[rt[x]].mu *= v[x], t[rt[x]].ad *= v[x], T[RT[X]].W *=V[x]; ElseT[rt[x]].ad + = V[x], T[RT[X]].W + =v[x];}intMain () {scanf ("%d%d", &n, &m); for(inti =1; I <= N; i++) scanf ("%lld", &H[i]); for(inti =2; I <= N; i++) scanf ("%d%d%lld", &x, &a[i], &V[i]), add (x, I); for(inti =1; I <= m; i++) scanf ("%lld%d", &s[i], &C[i]), vc[c[i]].push_back (i); d[1] =1, DFS (1); for(inti =1; I <= N; i++) printf ("%d\n", F[i]); for(inti =1; I <= m; i++) printf ("%d\n", d[c[i]]-D[g[i]]); return 0;}
Left-leaning tree (BZOJ4003)