Description



给定一棵有n个节点的无根树和m个操作,操作有2类:

1、将节点a到节点b路径上所有点都染成颜色c;

2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ 112221 ” 由3段组成:“ 11 ” 、“ 222 ” 和“ 1 ” 。

请你写一个程序依次完成这m个操作。


Input



第一行包含2个整数n和m,分别表示节点数和操作数;

第二行包含n个正整数表示n个节点的初始颜色

下面行每行包含两个整数x和y,表示xy之间有一条无向边。

下面行每行描述一个操作:

“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;

“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。


Output



对于每个询问操作,输出一行答案。


Sample Input



6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C 2 1 1 Q 3 5 C 5 1 2 Q 3 5


Sample Output



3 1 2


Hint



数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。


差不多学会套路了,树链剖分到线段树上瞎搞即可。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define inone(x) scanf("%d", &x)
#define intwo(x,y) scanf("%d%d", &x, &y)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int n, m, x, y, z;
int ft[N], nt[N], u[N], v[N], sz;
int mx[N], ct[N], fa[N], cnt;
int top[N], dep[N], F[N], G[N];
int cov[N << 1];
char s[N];
struct point
{
int l, r, c;
point(int l = 0, int r = 0, int c = 0) :l(l), r(r), c(c) {};
}f[N << 1];

void dfs(int x, int f)
{
fa[x] = f; dep[x] = dep[f] + 1;
ct[x] = 1; mx[x] = 0;
loop(i, ft[x], nt)
{
if (u[i] == f) continue;
dfs(u[i], x);
ct[x] += ct[u[i]];
mx[x] = ct[mx[x]] < ct[u[i]] ? u[i] : mx[x];
}
}

void Dfs(int x, int t)
{
top[x] = t ? x : top[fa[x]];
F[x] = ++cnt; G[cnt] = x;
if (mx[x]) Dfs(mx[x], 0);
loop(i, ft[x], nt)
{
if (u[i] == fa[x] || u[i] == mx[x]) continue;
Dfs(u[i], 1);
}
}

point merge(point a, point b)
{
return point(a.l, b.r, a.c + b.c - (int)(a.r == b.l));
}

void build(int x, int l, int r)
{
cov[x] = -1;
if (l == r) { f[x] = point(v[G[l]], v[G[r]], 1); return; }
int mid = l + r >> 1;
build(lson); build(rson);
f[x] = merge(f[x << 1], f[x << 1 | 1]);
}

void push(int x)
{
cov[x << 1] = cov[x << 1 | 1] = cov[x];
f[x << 1] = point(cov[x], cov[x], 1);
f[x << 1 | 1] = point(cov[x], cov[x], 1);
cov[x] = -1;
}

void change(int x, int l, int r, int ll, int rr, int v)
{
if (ll <= l&&r <= rr)
{
cov[x] = v;
f[x] = point(v, v, 1);
}
else
{
if (cov[x] != -1) push(x);
int mid = l + r >> 1;
if (ll <= mid) change(lson, ll, rr, v);
if (rr > mid) change(rson, ll, rr, v);
f[x] = merge(f[x << 1], f[x << 1 | 1]);
}
}

void change(int x, int y, int z)
{
for (; top[x] != top[y]; x = fa[top[x]])
{
if (dep[top[x]] < dep[top[y]]) swap(x, y);
change(1, 1, n, F[top[x]], F[x], z);
}
if (dep[x] > dep[y]) swap(x, y);
change(1, 1, n, F[x], F[y], z);
}

point query(int x, int l, int r, int ll, int rr)
{
if (ll <= l&&r <= rr) return f[x];
if (cov[x] != -1) push(x);
int mid = l + r >> 1;
point res = point(0, 0, 0);
if (ll <= mid) res = query(lson, ll, rr);
if (rr > mid) res = res.c ? merge(res, query(rson, ll, rr)) : query(rson, ll, rr);
return res;
}

void query(int x, int y)
{
point tx = point(-1, -1, 0), ty = point(-1, -1, 0), tz;
while (top[x] != top[y])
{
if (dep[top[x]] > dep[top[y]])
{
tx = merge(query(1, 1, n, F[top[x]], F[x]), tx);
x = fa[top[x]];
}
else
{
ty = merge(query(1, 1, n, F[top[y]], F[y]), ty);
y = fa[top[y]];
}
}
swap(tx.l, tx.r);
if (dep[x] > dep[y])
{
tz = query(1, 1, n, F[y], F[x]);
swap(tz.l, tz.r);
}
else tz = query(1, 1, n, F[x], F[y]);

tx = merge(tx, merge(tz, ty));
printf("%d\n", tx.c);
}

int main()
{
while (intwo(n, m) != EOF)
{
ct[0] = dep[0] = cnt = sz = 0;
rep(i, 1, n) ft[i] = -1, inone(v[i]);
rep(i, 2, n)
{
intwo(x, y);
u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; nt[sz] = ft[y]; ft[y] = sz++;
}
dfs(1, 0); Dfs(1, 1); build(1, 1, n);
while (m--)
{
scanf("%s", s);
if (s[0] == 'C')
{
intwo(x, y); inone(z);
change(x, y, z);
}
else
{
intwo(x, y);
query(x, y);
}
}
}
return 0;
}