"BZOJ4999" This problem is Too simple! Description gives you a tree, each node has an initial value. The following two actions are now supported: 1. C i x (0<=x<2^31) indicates that the value of the I node is changed to X. 2. Q i J x (0<=x<2^31) indicates how many nodes on the path of the I node to the J node have a value of x. The first line of input has two integers n,q (1≤n≤100,000;1≤q≤200,000), which represent the number of nodes and the number of operands respectively. The following row of n integers represents the initial value of each node at the beginning. Next N-1 line, two integer x, y for each line, indicates that the X node is directly connected to the Y node (describing a tree). In the next Q line, each line represents an action, and the description of the operation is given in the title description. Output represents a single line of answers for each of the Q outputs. Sample Input5 6
10 20 30) 40 50
1 2
1 3
3 4
3 5
Q 2 3 40
C 1 40
Q 2 3 40
Q 4 5 30
C 3 10
Q 4 5Sample Output0
1
1
0
the puzzle: Because each color is independent of each other, you can consider offline. Turn the modify operation into a delete operation of one color and a join operation of another color, and then sort all actions and inquiries by color and time, processing each color individually. Then we just need to multiply the lca+ to dynamically maintain the weights on all points to the root path. The single-point weight +a equals the weights on all points in its subtree are +a, so the DFS sequence is maintained with a tree-like array.
Because the array opened small and WA a day ~
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int maxn=200010;int n,m,q,cnt,now,tot;struct node{int col,x,val,tim;} P[maxn*5];struct query{int col,a,b,tim,org;} Q[maxn];char Str[10];int to[maxn<<1],next[maxn<<1],head[maxn],p1[maxn],p2[maxn],dep[maxn],fa[19][ Maxn],v[maxn];int log[maxn],s[maxn],vis[maxn],ans[maxn];void Add (int a,int b) {to[cnt]=b,next[cnt]=head[a],head[a]= cnt++;} void Dfs (int x) {p1[x]=++p2[0];for (int i=head[x];i!=-1;i=next[i]) if (to[i]!=fa[0][x]) fa[0][to[i]]=x,dep[to[i]]=dep[ X]+1,dfs (To[i]);p 2[x]=p2[0];} BOOL Cmpp (node A,node b) {return (A.col==b.col)? ( A.TIM<B.TIM):(a.col<b.col);} BOOL Cmpq (QUERY A,query b) {return (A.col==b.col)? ( A.TIM<B.TIM):(a.col<b.col);} void Updata (int x,int val) {for (int i=x;i<=n;i+=i&-i) {if (Vis[i]<now) Vis[i]=now,s[i]=0;s[i]+=val;}} int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) {if (Vis[i]<now) vis[i]=now,s[i]=0;ret+=s[i];} return ret;} int LCA (int A,int b) {if (Dep[a]<dep[b]) swap (A, b), for (int i=log[dep[a]-dep[b]];i>=0;i--) if (Dep[fa[i][a]]>=dep[b]) a =fa[i][a];if (a==b) return a;for (int i=log[dep[a]];i>=0;i--) if (Fa[i][a]!=fa[i][b]) A=fa[i][a],b=fa[i][b];return Fa[0][a];} inline Int rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} int main () {n=rd (), m=rd (); int I,j,a,b,c;memset (head,-1,sizeof (head)); for (i=1;i<=n;i++) v[i]=p[++tot].col=rd (), p [Tot].x=i,p[tot].tim=0,p[tot].val=1;for (i=2;i<=n;i++) a=rd (), B=rd (), add (b), add (b,a);d Ep[1]=1,dfs (1); for (i= 2;i<=n;i++) log[i]=log[i>>1]+1;for (j=1; (1<<j) <=n;j++) for (i=1;i<=n;i++) fa[j][i]=fa[j-1][fa[ J-1][i]];for (i=1;i<=m;i++) {scanf ("%s", str), A=rd (), B=rd (), if (str[0]== ' C ') {tot+=2,p[tot-1].x=p[tot].x=a,p[ tot-1].col=v[a],v[a]=p[tot].col=b;p[tot-1].tim=p[tot].tim=i,p[tot-1].val=-1,p[tot].val=1;} Elseq[++q].a=a,q[q].b=b,q[q].col=rd (), q[q].org=q, Q[q].tim=i;} Sort (p+1,p+tot+1,cmpp); sort (Q+1,Q+Q+1,CMPQ); for (i=j=1;i<=q;i++) {for (;(p [j].col<q[i].col| | (P[j].col==q[i].col&&p[j].tim<q[i].tim)) &&j<=tot;j++) {now=p[j].col;updata (P1[p[j].x],p[j].val), Updata (P2[p[j].x]+1,-p[j].val);} if (now==q[i].col) {A=q[i].a,b=q[i].b,c=lca (A, B), Ans[q[i].org]=query (P1[a]) +query (P1[b])-query (P1[c])-query (p1[ FA[0][C]]);}} for (i=1;i<=q;i++) printf ("%d\n", Ans[i]); return 0;}
"BZOJ4999" This problem is Too simple! Offline + tree-like array +lca