Topic: Give a tree, ask any two points between the number of different colors, a person may be colored blind, will be A and B as a color.
Train of thought: compare bare tree on Mo Team, write come after, very slow, suspicion is the reason of block, then decisive find the year of the nominal turn, instantaneous rk1, probably looked at a glance, he seems to be directly with DFS Order + Manhattan distance the smallest spanning tree, why will score block fast?
Yesterday afternoon saw this question has been studying the tree in the correct posture, and then wrote a tree, and later saw a lot of cattle spoj COT2 of the problem, and later with the classmate discussed for a long time to understand.
First, the tree is divided into blocks, and then the interval is sorted, according to the first weight of the left end of the block of the number of the right end point in the DFS sequence position, the key is to transfer. There is a way to VFK. For any state, the path of [L,r] is represented on the tree, and the current state only has the color of these dots in the {x|x∈[l,r],x! = LCA (l,r)}, so there are probably two cases, one is two chains, no intermediate LCA, or a chain, no LCA at the top. And then all the time, such as moving from [L,r] to [x, Y], we only need violence from l->x,y->r, note that an array of tokens is recorded, and all points on the path are reversed when the transfer takes place. After this transfer is still {x|x∈[l,r],x! = LCA (L,r)} these points. When the answer is counted, the LCA is added back and then deleted.
Note: Look for LCA to multiply! Do not like I think write not write multiplication is O (n) and then T one night ...
CODE:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define MAX 100100using namespace std; inline char GetChar () {static const int L = (1 << 15); static char buffer[l],*s = buffer,*t = buffer; if (s = = t) {T = (s = buffer) + fread (Buffer,1,l,stdin); if (S = = T) return EOF; } return *s++;} inline int GetInt () {int C; while (!isdigit (c = GetChar ())); int x = c-' 0 '; while (isdigit (c = GetChar ())) x = (x << 1) + (x << 3) + C-' 0 '; return x;} int Block_size,belong[max],blocks;int Pos[max],cnt;int size[max],root; struct ask{int x,y,_id; int mixed,_mixed; BOOL operator < (const ASK &a) Const {if (belong[x] = = belong[a.x]) return pos[y] < POS[A.Y]; return belong[x] < belong[a.x]; } void Read (int p) {x = GetInt (), y = GetInt (); mixed = GetInt (), _mixed = GetInt (); if (Pos[x] > Pos[y]) Swap (x, y); _id = p; }}ask[max << 1]; int points,asks;int head[max],total;int _next[max << 1],aim[max << 1]; int deep[max],father[max][20]; int Src[max]; int Num[max],colors;bool V[max];int Ans[max]; inline void Add (int x,int y) {_next[++total] = head[x]; Aim[total] = y; HEAD[X] = total;} void DFS (int x,int last) {father[x][0] = last; POS[X] = ++cnt; DEEP[X] = Deep[last] + 1; for (int i = head[x]; I, i = _next[i]) {if (aim[i] = = last) continue; if (Size[belong[x]] < block_size) ++size[belong[x]],belong[aim[i]] = belong[x]; else ++size[++blocks],belong[aim[i]] = blocks; DFS (AIM[I],X); }}: inline void change (int x,int c) {if (!num[x]) ++num[x],++colors; else if (num[x] = = 1 && c = = 1)--num[x],--colors; else Num[x] + = C;} inline int Getlca (int x,int y) {if (Deep[x] < deep[y]) swap (x, y); for (int i = n; ~i; i) if (Deep[father[x][i]] >= deep[y]) x = Father[x][i]; if (x = = y) return x; for (int i = n; ~i; i) if (father[x][i]! = Father[y][i]) x = Father[x][i],y = Father[y][i]; return father[x][0];} inline void work (Int. X,int Y,int LCA) {for (; x! = LCA; x = father[x][0]) {change (src[x],v[x]? -1:1); V[X] ^= 1; } for (; Y! = LCA; y = father[y][0]) {change (Src[y],v[y]? -1:1); V[y] ^= 1; }} inline void Solve (int p) {static int l = Root,r = Root,lca; Work (L,ask[p].x,getlca (l,ask[p].x)); Work (R,ask[p].y,getlca (R,ASK[P].Y)); L = Ask[p].x,r = Ask[p].y; LCA = Getlca (l,r); Change (src[lca],1); ANS[ASK[P]._ID] = colors; if (ask[p].mixed! = ask[p]._mixed) ans[ask[p]._id] = num[ask[p].mixed] && num[ask[p]._mixed]; Change (src[lca],-1);} inline void sparsetable () {for (int j = 1, J < ++j) for (int i = 1; I <= points; ++i) father[ I][J] = father[father[i][j-1]][j-1];} int main () {//freopen ("applE.in "," R ", stdin); Freopen ("Apple.out", "w", stdout); CIN >> points >> asks; for (int i = 1; I <= points; ++i) Src[i] = GetInt (); for (int x,y,i = 1; I <= points; ++i) {x = GetInt (), y = GetInt (); Add (x, y), add (y,x); } block_size = sqrt (points + 1); SIZE[1] = 1; Belong[0] = 1; blocks = 1; DFS (0,max-1); root = aim[head[0]]; Sparsetable (); for (int i = 1; I <= asks; ++i) Ask[i]. Read (i); Sort (ask + 1,ask + asks + 1); for (int i = 1; I <= asks; ++i) Solve (i); for (int i = 1; I <= asks; ++i) printf ("%d\n", Ans[i]); return 0;}
Bzoj 3757 Apple tree on the MO team