WAAAA调不出来了...
对拍都是对的...
但是下载下来的数据跑不过,数据只有 CW和QS...
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 2 * N;
int n, m, w[N], c[N];
struct tree
{
int l, r, ls, rs;
int sum, maxn;
}a[N * 40];
int root[N], tot;
int h[N], ne[M], e[M], idx;
void add(int a, int b)
{
e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}
int deep[N], fa[N], cnt[N], hson[N];
void dfs1(int x, int father)
{
deep[x] = deep[father] + 1, fa[x] = father, cnt[x] = 1;
for(int i = h[x]; i; i = ne[i])
if(e[i] != father)
{
int j = e[i];
dfs1(j, x);
if(cnt[j] > cnt[hson[x]]) hson[x] = j;
cnt[x] += cnt[j];
}
}
int dfn[N], viti, linktop[N];
void dfs2(int x, int top)
{
dfn[x] = ++viti, linktop[x] = top;
if(hson[x]) dfs2(hson[x], top);
for(int i = h[x]; i; i = ne[i])
if(e[i] != fa[x] && e[i] != hson[x])
dfs2(e[i], e[i]);
}
void pushup(int p)
{
a[p].sum = a[a[p].ls].sum + a[a[p].rs].sum;
a[p].maxn = max(a[a[p].ls].maxn, a[a[p].rs].maxn);
}
void insert(int p, int x, int d)
{
if(a[p].l == a[p].r)
{
a[p].sum = a[p].maxn = d;
return;
}
int mid = a[p].l + a[p].r >> 1;
if(x <= mid)
{
if(!a[p].ls) a[++tot] = (tree){a[p].l, mid, 0, 0, 0, 0}, a[p].ls = tot;
insert(a[p].ls, x, d);
}
else
{
if(!a[p].rs) a[++tot] = (tree){mid + 1, a[p].r, 0, 0, 0, 0}, a[p].rs = tot;
insert(a[p].rs, x, d);
}
pushup(p);
}
int getsum(int p, int l, int r)
{
if(l <= a[p].l && a[p].r <= r)
return a[p].sum;
int mid = a[p].l + a[p].r >> 1, sum = 0;
if(l <= mid && a[p].ls) sum += getsum(a[p].ls, l, r);
if(r > mid && a[p].rs) sum += getsum(a[p].rs, l, r);
return sum;
}
int getmax(int p, int l, int r)
{
if(l <= a[p].l && a[p].r <= r)
return a[p].maxn;
int mid = a[p].l + a[p].r >> 1, maxn = 0;
if(l <= mid && a[p].ls) maxn = max(maxn, getmax(a[p].ls, l, r));
if(r > mid && a[p].rs) maxn = max(maxn, getmax(a[p].rs, l, r));
return maxn;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= 1e5; i++)
a[++tot] = (tree){1, n, 0, 0, 0, 0}, root[i] = tot;
for(int i = 1; i <= n; i++)
scanf("%d%d", &w[i], &c[i]);
for(int i = 1, a, b; i < n; i++)
{
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
dfs1(1, 0);
dfs2(1, 1);
for(int i = 1; i <= n; i++)
insert(root[c[i]], dfn[i], w[i]);
while(m--)
{
char op[3];
int x, y;
scanf("%s%d%d", op, &x, &y);
if(op[0] == 'C' && op[1] == 'C')
{
insert(root[c[x]], dfn[x], 0);
insert(root[c[x] = y], dfn[x], w[x]);
}
else if(op[0] == 'C' && op[1] == 'W')
insert(root[c[x]], dfn[x], w[x] = y);
else if(op[0] == 'Q' && op[1] == 'S')
{
int ans = 0, t = c[x];
while(linktop[x] != linktop[y])
{
if(deep[x] < deep[y]) swap(x, y);
ans += getsum(root[t], dfn[linktop[x]], dfn[x]);
x = fa[linktop[x]];
}
if(deep[x] < deep[y]) swap(x, y);
ans += getsum(root[t], dfn[y], dfn[x]);
printf("%d\n", ans);
}
else
{
int ans = 0, t = c[x];
while(linktop[x] != linktop[y])
{
if(deep[x] < deep[y]) swap(x, y);
ans = max(ans, getmax(root[t], dfn[linktop[x]], dfn[x]));
x = fa[linktop[x]];
}
if(deep[x] < deep[y]) swap(x, y);
ans = max(ans, getmax(root[t], dfn[y], dfn[x]));
printf("%d\n", ans);
}
}
return 0;
}