开始以为区间合并不可做,后来发现这个区间合并是可以的,先查询出每个区间各自颜色段数,然后对于边界相邻的边做一遍特殊处理。
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
struct Edge {
int to ;
Edge * next;
}E[MAXN*2],*EE;
struct Gragh {
Edge * first;
}G[MAXN];
struct Tree {
int x,y;
int left_color,right_color;
int color_num;
int lazy;
}t[MAXN<<2];
int pre[MAXN],son[MAXN],siz[MAXN],dep[MAXN],top[MAXN],pos[MAXN],Rank[MAXN];
int N,Q,S;
int num[MAXN];
int idx[MAXN];
int tree_idx = 0;
int tot ;
void Init() {
EE = E;
dep[1] = 1;
pre[1] = 0;
tree_idx = 0;
memset(G,0,sizeof(G));
memset(son,0,sizeof(son));
}
void addedge(int u,int v) {
EE->to = v ; EE->next = G[u].first ; G[u].first = EE++;
EE->to = u ; EE->next = G[v].first ; G[v].first = EE++;
}
void dfs1(int u) {
siz[u] = 1;
son[u] = 0;
for(Edge * p = G[u].first ; p ; p = p -> next) {
if(p->to != pre[u]) {
pre[p->to] = u;
dep[p->to] = dep[u] + 1;
dfs1(p->to);
siz[u] += siz[p->to];
if(siz[p->to] > siz[son[u]])
son[u] = p -> to;
}
}
}
void dfs2(int u,int ancestor) {
top[u] = ancestor;
tree_idx ++;
pos[u] = tree_idx;
if(son[u] != 0) {
dfs2(son[u],ancestor);
}
for(Edge * p = G[u].first ; p ; p = p -> next) {
if(p->to != pre[u] && p->to != son[u]) {
dfs2(p->to,p->to);
}
}
}
void Push_Up(int rt) {
if(t[rt<<1].right_color == t[rt<<1|1].left_color) {
t[rt].color_num = t[rt<<1].color_num + t[rt<<1|1].color_num - 1;
}
else {
t[rt].color_num = t[rt<<1].color_num + t[rt<<1|1].color_num;
}
t[rt].right_color = t[rt<<1|1].right_color;
t[rt].left_color = t[rt<<1].left_color;
}
void Push_Down(int rt) {
if(t[rt].lazy) {
t[rt<<1|1].lazy = t[rt<<1].lazy = t[rt].lazy;
t[rt<<1].left_color = t[rt<<1].right_color = t[rt].lazy;
t[rt<<1|1].left_color = t[rt<<1|1].right_color = t[rt].lazy;
t[rt<<1|1].color_num = t[rt<<1].color_num = t[rt].color_num;
t[rt].lazy = 0;
}
}
void Build(int x,int y,int rt) {
t[rt].x = x ; t[rt].y = y; t[rt].lazy = 0;
if(x == y) {
t[rt].left_color = Rank[x];
t[rt].right_color = Rank[x];
t[rt].color_num = 1;
return ;
}
int mid = (x + y) >> 1;
Build(x,mid,rt<<1);
Build(mid+1,y,rt<<1|1);
Push_Up(rt);
}
void Update(int rt,int left,int right ,int lazy) {
if(left <= t[rt].x && right >= t[rt].y) {
t[rt].lazy = lazy;
t[rt].left_color = t[rt].right_color = lazy;
t[rt].color_num = 1;
return ;
}
Push_Down(rt);
int mid = (t[rt].x + t[rt].y) >> 1;
if(mid >= left) {
Update(rt<<1,left,right,lazy);
}
if(mid < right) {
Update(rt<<1|1,left,right,lazy);
}
Push_Up(rt);
}
int right_color = 0;
int ANS = 0;
void Query(int rt,int left,int right) {
if(left <= t[rt].x && right >= t[rt].y) {
if(right_color == t[rt].left_color) {
ANS = ANS + t[rt].color_num - 1;
}
else {
ANS = ANS + t[rt].color_num;
}
right_color = t[rt].right_color;
return ;
}
Push_Down(rt);
int mid = (t[rt].x + t[rt].y) >> 1;
if(mid >= left) {
Query(rt<<1,left,right);
}
if(mid < right){
Query(rt<<1|1,left,right);
}
Push_Up(rt);
}
int Query_One(int rt,int position,int flag) {
if(t[rt].x == t[rt].y) {
if(flag == 0) return t[rt].left_color;
else return t[rt].right_color;
}
Push_Down(rt);
int mid = (t[rt].x + t[rt].y) >> 1;
if(mid >= position) {
return Query_One(rt<<1,position,flag);
}
else {
return Query_One(rt<<1|1,position,flag);
}
Push_Up(rt);
}
void Change(int x,int y,int lazy) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
Update(1,pos[top[x]],pos[x],lazy);
x = pre[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
Update(1,pos[x],pos[y],lazy);
}
int Query_Tree(int x,int y) {
int ans = 0;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
ANS = 0; right_color = 0;
Query(1,pos[top[x]],pos[x]);
ans += ANS;
x = pre[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
ANS = 0; right_color = 0;
Query(1,pos[x],pos[y]);
ans += ANS;
return ans;
}
int inquire(int x,int y) {
int ans = 0;
int _left_color,_right_color;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
_left_color = Query_One(1,pos[top[x]],0);
_right_color = Query_One(1,pos[pre[top[x]]],1);
if(_left_color == _right_color) ans --;
x = pre[top[x]];
}
return ans;
}
int ReadInt() {
char c = getchar();
while(c > '9' || c < '0') c = getchar();
int ret = 0;
while(c <= '9' && c >= '0') {
ret = ret * 10 + c - '0';
c = getchar();
}
return ret;
}
void input() {
int u,v,value;
for(int i = 1 ; i <= N ; i++) {
scanf("%d",num+i);
}
for(int i = 1 ; i <= N - 1 ; i++) {
scanf("%d %d",&u,&v);
addedge(u,v);
}
dfs1(1);
dfs2(1,1);
for(int i = 1 ; i <= N ; i++) {
Rank[pos[i]] = num[i];
}
Build(1,N,1);
char op[5];
int color;
for(int i = 1 ; i <= Q ; i ++) {
scanf("%s",op) ;
if(op[0] == 'C') {
scanf("%d %d %d",&u,&v,&color);
Change(u,v,color);
}
else {
scanf("%d %d",&u,&v);
int ans = 0;
ans += Query_Tree(u,v);
ans += inquire(u,v);
printf("%d\n",ans);
}
}
}
int main(void) {
freopen("a.in","r",stdin);
while(~scanf("%d %d",&N,&Q)) {
Init();
input();
}
return 0;
}