Find the Dfs sequence and the depth of each point
Consider the first point as a two-dimensional point (St[i],d[i])
The modification operation is equivalent to the horizontal axis in [st[x],en[x]], the ordinate in the [D[x],d[x]+y] range of the point color is modified to C
k-d Tree Maintenance with support tags, time complexity $o (N\log n+q\sqrt{n}) $.
#include <cstdio> #include <algorithm> #define N 100010int T,n,c,m,x,y,i,g[n],nxt[n],st[n],en[n],dfn,d[n] , Id[n],tmp[n],ans,root,cmp_d,x1,y1,x2,y2;inline Void Read (int&a) {char c;while (! ((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')); a=c-' 0 '; while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*= Ten) +=c-' 0 ';} struct Node{int d[2],l,r,max[2],min[2],tag,col,f;} t[n];inline BOOL CMP (node A,node b) {return a.d[cmp_d]<b.d[cmp_d];} void Dfs (int x) {t[x].tag=x,t[x].d[0]=st[x]=++dfn,t[x].d[1]=d[x]; for (int i=g[x];i;i=nxt[i]) D[i]=d[x]+1,dfs (i); EN[X]=DFN;} inline void Umax (Int&a,int b) {if (a<b) a=b;} inline void Umin (Int&a,int b) {if (a>b) a=b;} inline void up (int x) {id[t[x].tag]=x,t[x].tag=0,t[x].col=1; if (T[X].L) {UMAX (t[x]. MAX[0],T[T[X].L]. Max[0]); Umin (T[x]. MIN[0],T[T[X].L]. Min[0]); UMAX (T[x]. MAX[1],T[T[X].L]. MAX[1]); Umin (T[x]. MIN[1],T[T[X].L]. MIN[1]); } if (T[X].R) {UMAX (t[x]. MAX[0],T[T[X].R]. Max[0]); Umin (T[x]. MIN[0],T[T[X].R]. Min[0]); UMAX (T[x].MAX[1],T[T[X].R]. MAX[1]); Umin (T[x]. MIN[1],T[T[X].R]. MIN[1]); }}int Build (int l,int r,int d,int f) {int mid= (L+R) >>1; Cmp_d=d,std::nth_element (T+L+1,T+MID+1,T+R+1,CMP); T[mid].f=f; T[mid]. Max[0]=t[mid]. MIN[0]=T[MID].D[0]; T[mid]. Max[1]=t[mid]. MIN[1]=T[MID].D[1]; if (l!=mid) T[mid].l=build (l,mid-1,! D,MID); else t[mid].l=0; if (r!=mid) T[mid].r=build (mid+1,r,! D,MID); else t[mid].r=0; Return up (mid), mid;} inline void tag1 (int x,int y) {t[x].col=t[x].tag=y;} inline void pb (int x) {if (T[x].tag) {if (T[X].L) Tag1 (T[x].l,t[x].tag); if (T[X].R) Tag1 (T[x].r,t[x].tag); t[x].tag=0; }}inline void Change (int x) {if (t[x]. max[0]<x1| | T[X]. min[0]>x2| | T[X]. max[1]<y1| | T[X]. MIN[1]>Y2) return; if (T[x]. MIN[0]>=X1&&T[X]. MAX[0]<=X2&&T[X]. MIN[1]>=Y1&&T[X]. Max[1]<=y2) {tag1 (x,c); return;} PB (x); if (t[x].d[0]>=x1&&t[x].d[0]<=x2&&t[x].d[1]>=y1&&t[x].d[1]<=y2) t[x].col=c; if (T[X].L) change (T[X].L); if (T[X].R) change(T[X].R);} inline int Ask (int x) {int s=0,i=x; while (T[I].F) tmp[++s]=i=t[i].f; while (s) PB (tmp[s--]); return t[x].col;} int main () {for (read (T); t--;p rintf ("%d\n", ans)) {read (n), read (c), read (m); for (i=1;i<=n;i++) g[i]=0; for (i=2;i<=n;i++) read (x), nxt[i]=g[x],g[x]=i; DFS (1), Root=build (1,n,0,0), ans=0; for (i=1;i<=m;i++) {read (x), read (y), read (c); if (c) x1=st[x],x2=en[x],y1=d[x],y2=d[x]+y,change (root); Else ans= (1ll*ask (id[x]) *i+ans)%1000000007; }} return 0;}
BZOJ4154: [Ipsc2015]generating Synergy