# 1103 POI2007 大都市meg

树链剖分水过，单点修改，树状数组即可。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 250100
using namespace std;

int n, m, nowplace = 0;
int p[N] = {0}, next[N], v[N], bnum = 0;
int son[N] = {0}, fa[N], w[N], top[N], deep[N] = {0}, num[N];
int t[N] = {0};

int lowbit(int x) { return x & -x; }

void dfs_1(int now, int fat)
{
int k = p[now]; num[now] = 1; int maxson = 0;
fa[now] = fat; deep[now] = deep[fat] + 1;
while (k)
{
dfs_1(v[k], now);
if (num[v[k]] > maxson)
{
maxson = num[v[k]];
son[now] = v[k];
}
num[now] += num[v[k]];
k = next[k];
}
}

void dfs_2(int now, int nowtop)
{
int k = p[now]; w[now] = ++nowplace; top[now] = nowtop;
if (son[now]) dfs_2(son[now], nowtop);
while (k)
{
if (v[k] != son[now])
dfs_2(v[k], v[k]);
k = next[k];
}
}

{
while (now <= n)
{
now += lowbit(now);
}
return;
}

{
int ans = 0;
while (now)
{
ans += t[now];
now -= lowbit(now);
}
return ans;
}

{
int f1 = top[u], f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
else
{
int ans = 0;
return ans;
}
}

int main()
{
scanf("%d", &n);
for (int i = 1; i < n; ++i)
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
}
dfs_1(1, 0); dfs_2(1, 1);
for (int i = 2; i <= n; ++i) add(w[i], 1);
scanf("%d", &m); m = m+n-1;
for (int i = 1; i <= m; ++i)
{
char s[2]; scanf("%s", s);
if (s[0] == 'A')
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
}
else
{
int x; scanf("%d", &x);
}
}
return 0;
}

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 250010
using namespace std;

int n, m, nowplace = 0;
int p[N] = {0}, next[N], v[N], bnum = 0;
int t[N*2] = {0};
int first[N], second[N];

int lowbit(int x) { return x & -x; }

{
while (now <= 2*n)
{
now += lowbit(now);
}
}

void dfs(int now)
{
int k = p[now]; add(++nowplace, 1);
first[now] = nowplace;
while (k)
{
dfs(v[k]);
k = next[k];
}
second[now] = nowplace;
}

{
int ans = 0;
while (now)
{
ans += t[now];
now -= lowbit(now);
}
return ans;
}

int main()
{
scanf("%d", &n);
for (int i = 1; i < n; ++i)
{
int x, y; scanf("%d%d", &x, &y);
if (x > y) swap(x, y);
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
}
dfs(1);
scanf("%d", &m); m = m+n-1;
for (int i = 1; i <= m; ++i)
{
char s[2]; scanf("%s", s);
if (s[0] == 'A')
{
int x, y; scanf("%d%d", &x, &y);
if (x < y) swap(x, y);
}