Test instructions: N-node tree with one color for each point. The existing M-type queries, each asking x y a B indicates the number of colors on the path x to Y and a color as B color. (n<=50000, m<=100000)
#include <bits/stdc++.h>using namespace Std;const int n=50005;int ihead[n], CNT, id[n], blo[n], f[n][16], Dep[n], CA L[n], Col[n], st[n], N, M, Ans[n], Ans, id;struct E {int next, to;} e[n<<1];struct Q {int x, y, a, B, id;} Q[n*2];bool CMP (const q &a, const q &b) {return blo[a.x]==blo[b.x]?id[a.y]<id[b.y]:blo[a.x]<blo[b.x];} void Add (int x, int y) {e[++cnt]= (e) {ihead[x], y}; ihead[x]=cnt; e[++cnt]= (e) {ihead[y], x}; ihead[y]=cnt;} void Dfs (int x) {id[x]=++id;for (int i=1; i<=15; ++i) f[x][i]=f[f[x][i-1]][i-1];for (int i=ihead[x]; i; i=e[i].next) if ( E[i].to!=f[x][0]) {dep[e[i].to]=dep[x]+1; f[e[i].to][0]=x; Dfs (e[i].to);}} int LCA (int x, int y) {if (Dep[x]<dep[y]) swap (x, y), int d=dep[x]-dep[y];for (int i=15; i>=0;-I.) if ((D>>i) & ; 1) x=f[x][i]; if (x==y) return x;for (int i=15; i>=0;-I.) if (F[x][i]!=f[y][i]) x=f[x][i], Y=f[y][i];return f[x][0];} void update (int x) {if (St[x]) {if (!) ( --CAL[COL[X]])--ans; }else {if (!cal[col[x]]) ++ans; ++cal[cOL[X]]; }ST[X]=!ST[X];} void Move (int x, int y) {while (x!=y) if (Dep[x]>dep[y]) update (x), x=f[x][0];else update (y), y=f[y][0];} BOOL Check (int a, int b) {return cal[a] && cal[b] && a!=b;}//a!=bvoid pre () {DFS (1); for (int i=1; i<= M ++i) {scanf ("%d%d%d%d", &q[i].x, &q[i].y, &Q[I].A, &q[i].b), q[i].id=i; if (Id[q[i].x]>id[q[i].y]) Swap (q[i].x, Q[I].Y); }int sq=sqrt (n+0.5); for (int i=1; i<=n; ++i) blo[i]= (i-1)/sq;sort (q+1, Q+1+M, CMP); q[0].x=q[0].y=1;} void work () {for (int i=1; i<=m; ++i) {Move (q[i-1].x, q[i].x); Move (Q[i-1].y, q[i].y); int Lca=lca (q[i].x, q[i].y); updat E (LCA); Ans[q[i].id]=ans;if (Check (Q[I].A, q[i].b))--ans[q[i].id];update (LCA);} for (int i=1; i<=m; ++i) printf ("%d\n", Ans[i]);} int main () {scanf ("%d%d", &n, &m), for (Int. I=1; i<=n; ++i) scanf ("%d", &col[i]); for (int i=1; i<=n; ++i) {int x, y; scanf ("%d%d", &x, &y); if (!x | |!y) continue;add (x, y);} Pre (); work (); return 0;}
Chunking on tree = =
First of all forcibly yy to the Dfs sequence and then block, and then I do not know what the ghost I do not believe that this can be done (because I first understand wrong = = if I fixed the L, then r traversed by the number of nodes is divided O (n) Ah = = up to 2n (come in and Out))
Finding the right one is what I started thinking about = =
But I overlooked a problem with wood there! There may be problems when you update your LCA! Draw your own pictures to know! What if it's hard to write if it's a special sentence?
I heard that VFK god Ben have the right to worship the next = = Kneeling Rotten
First set $s (U, v) $ to represent a collection of nodes $u$ to the $v$ path. Make the $+$ operation represents the modulus of the element 2 plus (that is, the number of occurrences of the modulo 2 addition, that is, XOR = =, that is, two times the occurrence of removing)
Easy to get $s (u, v) = s (root, U) + s (root, V) + LCA (U, v) $
LCA is the most troublesome, VFK gives a clever solution:
Set $t (U, v) = s (root, U) + s (Root, V) $
Then set $u ' $ to indicate the destination $u$ to reach:
$ $T (U, v) + T (U, v) = s (u, root) + s (U ', root) + S (V, Root) + S (V, Root) $$
That
$ $T (U ', v) = t (U, u ') + t (U, v) $$
Then we're going to transfer from $t (U, v) $ to $t (U ', v) $ just go once $t (U, u ') $ on the dot on the line spicy! (Pay attention to kick off LCA)
Instead of LCA, you can do it directly with the climbing method = = very simple = =
And then count the colors and open a counter. Spicy = =
Then it's not spicy = =
"Bzoj" 3757 apple tree