vjudge上已经关闭了,要去洛谷提交才行。
板子题目,注意线段树查询的时候,被分段了,也要去判断颜色。在这里wa了一发。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e5 + 10;
struct node { int to, next; }edge[2*N];
int head[N];
int id[N], rk[N], son[N], fa[N], siz[N], dep[N], top[N], wi[N];
int n, m;
struct Tree { int lcol, rcol, sum; }tree[N<<2];
int lazy[N << 2];
int cnt = 0;
int num = 0;
void addedge(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void pushup(int root)
{
tree[root].sum = tree[lrt].sum + tree[rrt].sum;
if (tree[lrt].rcol == tree[rrt].lcol)tree[root].sum--;
tree[root].lcol = tree[lrt].lcol;
tree[root].rcol = tree[rrt].rcol;
}
void pushdown(int root)
{
if (lazy[root] != -1)
{
lazy[lrt] = lazy[rrt] = lazy[root];
tree[lrt].lcol = tree[lrt].rcol = lazy[root];
tree[rrt].lcol = tree[rrt].rcol = lazy[root];
tree[lrt].sum = tree[rrt].sum = 1;
lazy[root] = -1;
}
}
void build(int l, int r, int root)
{
lazy[root] = -1;
if (l == r) {
tree[root].lcol = tree[root].rcol = wi[rk[l]];
tree[root].sum = 1;
return;
}
int mid = (l + r) >> 1;
build(lson); build(rson);
pushup(root);
}
void update(int l, int r, int root, int lf, int rt, int c)
{
if (lf <= l && r <= rt)
{
tree[root].sum = 1;
tree[root].lcol = tree[root].rcol = c;
lazy[root] = c;
return;
}
pushdown(root);
int mid = (l + r) >> 1;
if (lf <= mid)update(lson, lf, rt, c);
if (rt > mid)update(rson, lf, rt, c);
pushup(root);
}
int querry(int l, int r, int root, int lf, int rt)
{
if (lf <= l && r <= rt)
{
return tree[root].sum;
}
pushdown(root);
int mid = (l + r) >> 1;
int ans = 0;
bool left=0;
if (lf <= mid)ans += querry(lson, lf, rt), left = 1;
if (rt > mid) {
ans += querry(rson, lf, rt);
if (left)
{
if (tree[lrt].rcol == tree[rrt].lcol)ans--;
}
}
return ans;
}
int querrycol(int l, int r, int root, int pos)
{
if (l == r)
{
return tree[root].lcol;
}
pushdown(root);
int mid = (l + r) >> 1;
if (pos <= mid)return querrycol(lson, pos);
else return querrycol(rson, pos);
}
void dfs1(int u, int f, int d)
{
fa[u] = f;
dep[u] = d;
siz[u] = 1;
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (v == f)continue;
dfs1(v, u, d + 1);
siz[u] += siz[v];
if (son[u] == -1 || siz[son[u]] < siz[v])
{
son[u] = v;
}
}
}
void dfs2(int u, int tp)
{
id[u] = ++num;
rk[num] = u;
top[u] = tp;
if (son[u] == -1)return;
dfs2(son[u], tp);
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (v == fa[u]|| v == son[u])continue;
dfs2(v, v);
}
}
void rgupdate(int x, int y,int c)
{
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])swap(x, y);
update(1, n, 1, id[top[x]], id[x], c);
x = fa[top[x]];
}
if (dep[x] < dep[y])swap(x, y);
update(1, n, 1, id[y], id[x], c);
}
int rgquerry(int x, int y)
{
int ans = 0;
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])swap(x, y);
ans += querry(1, n, 1, id[top[x]], id[x]);
if (querrycol(1,n,1,id[top[x]])==querrycol(1,n,1,id[fa[top[x]]]))ans--;
x = fa[top[x]];
}
if (dep[x] < dep[y])swap(x, y);
ans += querry(1, n, 1, id[y], id[x]);
return ans;
}
int main()
{
n = read(), m = read();
memset(head, -1, sizeof(head));
memset(son, -1, sizeof(son));
upd(i, 1, n)wi[i] = read();
int u, v;
upd(i, 1, n-1) {
u = read(), v = read();
addedge(u, v); addedge(v, u);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, n, 1);
char op[2];
int c;
while (m--)
{
cin >> op;
if (op[0] == 'C')
{
u = read(), v = read(); c = read();
rgupdate(u, v, c);
}
else
{
u = read(), v = read();
int temp = rgquerry(u, v);
printf("%d\n", temp);
}
}
return 0;
}