Description
In the old days, the countryside numbered 1 sequentially. n small villages, some villages have a two-way dirt road. From every village there is exactly one path to the village 1 (that is, Fort bitcoin). Also, for each village, its path to the fort is just numbered by the village numbering smaller than it. In addition, for all roads, they are not met in places other than villages. Now, more and more dirt roads have been transformed into highways. Blue Mary remembered her letter-sending experience during the transformation. She went from Fort Lauderdale to a village and some dirt roads were transformed into highways during the interval of two messengers. Now Blue Mary needs your help: Figure out the number of dirt roads she needs to walk every time she sends a messenger. Input
The first line is a number n (1 < = N < = 2 50000). The following n-1 lines, two integers per line a,b. The following line contains an integer m (1 < = m < = 2 50000), which indicates that Blue Mary once sent a letter to M during the transformation. The following n+m-1 lines, each of which has a number of information in two formats, represents a N+m-1 event that occurs chronologically: if this act a a B (a, if this behavior W A, it means that Blue Mary sent Messenger from Fort to village a. Output
There is a line of M, each containing an integer representing the number of dirt roads that were passed when a messenger was sent. Solving
There is nothing to say about the bare-tree chain.
Except to note that the input is stopped after encountering M W. Code
#include <cstdio> #include <cstring> #include <algorithm> #define N 250010 using namespace std; struct Node{int to,next;}
E[N*2];
int head[n],m,tim,n;
int size[n],son[n],top[n],fa[n],tid[n],dep[n];
void Init () {memset (head,0,sizeof head);
memset (son,-1,sizeof (son));
Tim = m = 0;
} void Add_edge (int from,int to) {e[++m].next = Head[from];
Head[from] = m;
E[m].to = to;
} void dfs1 (int v,int pa,int d) {size[v] = 1; fa[v] = PA; dep[v] = D;
for (int i = head[v];i;i=e[i].next) if (e[i].to!= pa) {DFS1 (e[i].to,v,d+1);
SIZE[V] + = size[e[i].to];
if (son[v] = =-1 | | size[son[v]] < size[e[i].to]) son[v] = e[i].to;
} void dfs2 (int v,int tp) {TOP[V] = TP; Tid[v] = ++tim;
if (son[v] = = 1) return;
DFS2 (SON[V],TP); for (int i = head[v];i;i=e[i].next) if (e[i].to!= fa[v] && e[i].to!= son[v]) DFS2 (e[i].to,e[i)
. to); } #define Lson O << 1 #define Rson o << 1 |
1 int sum[n << 2];
void build (int o,int l,int R) {if (L = = r) {Sum[o] = 1;
int mid = (l+r) >>1; Build (Lson,l,mid);
Build (Rson,mid+1,r);
Sum[o] = Sum[lson] + Sum[rson];
} void Change (int o,int l,int r,int pos) {if (L = = r) {Sum[o] = 0;
int mid = (l+r) >>1; if (POS <= mid) Change (LSON,L,MID,POS);
else change (rson,mid+1,r,pos);
Sum[o] = Sum[lson] + Sum[rson];
int query (int o,int l,int r,int ll,int rr) {if (ll <= l && RR >= R) return Sum[o];
int mid = (l+r) >>1,ans = 0;
if (ll <= mid) ans + = query (LSON,L,MID,LL,RR);
if (RR > Mid) ans + + query (RSON,MID+1,R,LL,RR);
return ans;
int Query (int x,int y) {int ans = 0;
while (Top[x]!= top[y]) ({if (dep[top[x)] < Dep[top[y]) swap (x,y);
Ans + + query (1,1,n,tid[top[x]],tid[x]);
x = fa[top[x]];
} if (Dep[x] > Dep[y]) swap (x,y); Ans + + query (1, 1,N,tid[x]+1,tid[y]);
return ans;
int main () {char opt[3];
int u,v;
scanf ("%d", &n);
Init ();
for (int i = 1;i < n;i++) {scanf ("%d%d", &u,&v); Add_edge (U,V);
Add_edge (V,u);
} DFS1 (1,0,0);
DFS2 (1,1);
Build (1,1,n);
scanf ("%d", &m);
int z = 0;
while (Z < m) {scanf ("%s%d", opt,&u);
if (opt[0] = = ' A ') {scanf ("%d", &v);
if (U > V) swap (u,v);
Change (1,1,n,tid[v]);
else {z++; printf ("%d\n", Query (1,u));
return 0; }