The first question does not explain, for the second question processing, may use the CDQ divide the treatment, assumes the division rule the question interval is [l,r], then we to the marking in [L,mid] The modification operation assigns a weight value, because in the current [L,r] [L,mid] 's modification operation only to [MID+1,R] the inquiry operation , so I modify the operation at least m-i time, so the weighted value is m-i, and for the [mid+1,r] interval of the query operation, also gives a weight w-m, where w is the value of the inquiry, we can preprocess the tree's DFS sequence and maintain a tree array, This allows us to insert or inquire about these operations from the weights to the small, Time complexity O (NLOGNLOGN).
Code, running time is almost the bottom line. (looking at the other blogger's puzzle seems to me a complex ... )
1#include <cstdio>2#include <algorithm>3 #defineN 5000104 #defineLB (x) (x&-x)5 using namespacestd;6 intDp,p[n],pre[n],tt[n],n,a,i,m,o,c[n],deep[n],fa[n];7 intL[n],r[n],ans[n];8 ints[n][ -];9 structg{Ten inttyp,l,r,w; One }b[n]; A structgg{ - intid,v; - }w[n]; the voidccintXintW) - { - while(x<=o) - { +c[x]+=W; -x+=lb (x); + } A } at voidDfsintx) - { - intI=P[x]; -l[x]=++o; - while(i) - { infa[tt[i]]=x; -deep[tt[i]]=deep[x]+1; to DFS (Tt[i]); +I=Pre[i]; - } ther[x]=++o; * } $ intLcaintXinty)Panax Notoginseng { - if(Deep[x]>deep[y]) x^=y^=x^=y; the inti; + for(i= +; i>=0; i--) A { the if(Deep[y]-deep[x]>= (1<<i)) + { -y=S[y][i]; $ } $ } - if(x==y)returnx; - for(i= +; i>=0; i--) the { - if(s[x][i]!=S[y][i])Wuyi { thex=S[x][i]; -y=S[y][i]; Wu } - } About returnFa[x]; $ } - intSumintx) - { - intans=0; A while(x) + { theans+=C[x]; -x-=lb (x); $ } the returnans; the } the voidLinkintXinty) the { -dp++;p re[dp]=p[x];p [x]=dp;tt[dp]=y; in } the BOOLCMP (GG a,gg b) the { About if(a.v==B.V) the returnA.id>b.id; the returnA.v>B.V; the } + voidSolveintLintR) - { the intm,i,tot=0;Bayi if(L>=R)return; theM= (l+r) >>1; the for(i=l;i<=m;i++) - if(b[i].typ==2) - { theW[++tot].id=i; thew[tot].v=m-i; the } the for(i=m+1; i<=r;i++) - if(b[i].typ==1) the { theW[++tot].id=i; thew[tot].v=b[i].w-(I-m);94 } theSort (w+1, w+1+tot,cmp); the the for(i=1; i<=tot;i++)98 if(w[i].id<=m) About { -CC (L[B[W[I].ID].W],1);101CC (r[b[w[i].id].w],-1);102 }103 Else104 { the intu=B[W[I].ID].L;106 intv=B[W[I].ID].R;107 intLca=LCA (U,V);108Ans[w[i].id]+=sum (L[u]) +sum (L[v])-sum (L[lca])-sum (l[lca]-1);109 } the for(i=1; i<=tot;i++)111 if(w[i].id<=m) the {113CC (l[b[w[i].id].w],-1); theCC (R[B[W[I].ID].W],1); the } the 117Solve (l,m); Solve (m+1, R);118 }119 - intMain ()121 {122scanf"%d",&n);123 for(i=1; i<=n;i++)124 { thescanf"%d",&a);126 if(a) link (a,i);127 } -scanf"%d",&m);129 for(i=1; i<=m;i++) the {131scanf"%d",&B[i].typ); the if(b[i].typ==1)133scanf"%d%d%d",&b[i].l,&b[i].r,&B[I].W);134 Elsescanf"%d",&B[I].W);135 }136Dfs1);137 for(i=1; i<=n;i++) s[i][0]=Fa[i];138 for(intH=1;h< -; h++)139 { $ for(i=1; i<=n;i++)141 {142s[i][h]=s[s[i][h-1]][h-1];143 }144 }145Solve1, m);146 for(i=1; i<=m;i++)147 if(b[i].typ==1)148 {149 intans=LCA (B[I].L,B[I].R); Maxans=deep[b[i].l]+deep[b[i].r]-2*deep[ans]+1;151printf"%d%d\n", Ans,ans[i]); the }153}
bzoj4448 [Scoi2015] Intelligence transmission