传送门

就是个模板啦

记录每一个点的左端点颜色和右端点颜色和当前端点颜色段数。

合并时如果左孩子右端点和右孩子左端点不同就 ans--

在重链上跳的时候别忘记统计一下

——代码

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 400001
#define ls now << 1
#define rs now << 1 | 1
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
#define pushup(now) Lcol[now] = Lcol[ls], Rcol[now] = Rcol[rs], col[now] = col[ls] + col[rs] - (Rcol[ls] == Lcol[rs]) int n, m, cnt, tim;
int head[N], to[N << 1], next[N << 1];
int a[N], f[N], deep[N], size[N], son[N], tid[N], top[N], rank[N], L[N], R[N], Lcol[N], Rcol[N], col[N], laz[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dfs1(int u)
{
int i, v;
size[u] = 1;
deep[u] = deep[f[u]] + 1;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(v ^ f[u])
{
f[v] = u;
dfs1(v);
size[u] += size[v];
if(son[u] == -1 || size[son[u]] < size[v]) son[u] = v;
}
}
} inline void dfs2(int u, int t)
{
int i, v;
top[u] = t;
tid[u] = ++tim;
rank[tim] = u;
if(son[u] == -1) return;
dfs2(son[u], t);
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(v ^ f[u] && v ^ son[u]) dfs2(v, v);
}
} inline void build(int now, int l, int r)
{
L[now] = l;
R[now] = r;
if(l == r)
{
col[now] = 1;
Lcol[now] = Rcol[now] = a[rank[l]];
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(now);
} inline void pushdown(int now)
{
if(laz[now])
{
col[ls] = col[rs] = 1;
laz[ls] = laz[rs] = Lcol[ls] = Lcol[rs] = Rcol[ls] = Rcol[rs] = laz[now];
laz[now] = 0;
}
} inline void update(int now, int l, int r, int z)
{
if(l <= L[now] && R[now] <= r)
{
col[now] = 1;
laz[now] = Lcol[now] = Rcol[now] = z;
return;
}
pushdown(now);
int mid = (L[now] + R[now]) >> 1;
if(l <= mid) update(ls, l, r, z);
if(mid < r) update(rs, l, r, z);
pushup(now);
} inline void q_update(int u, int v, int z)
{
while(top[u] ^ top[v])
{
if(deep[top[u]] < deep[top[v]]) swap(u, v);
update(1, tid[top[u]], tid[u], z);
u = f[top[u]];
}
if(deep[u] > deep[v]) swap(u, v);
update(1, tid[u], tid[v], z);
} inline int q_color(int now, int x)
{
if(L[now] == R[now]) return Lcol[now];
pushdown(now);
int mid = (L[now] + R[now]) >> 1;
if(x <= mid) return q_color(ls, x);
else return q_color(rs, x);
} inline int query(int now, int l, int r)
{
if(l <= L[now] && R[now] <= r) return col[now];
pushdown(now);
int mid = (L[now] + R[now]) >> 1, ans = 0;
if(l <= mid) ans += query(ls, l, r);
if(mid < r) ans += query(rs, l, r);
if(l <= mid && mid < r) ans -= (Rcol[ls] == Lcol[rs]);
return ans;
} inline int q_query(int u, int v)
{
int ans = 0;
while(top[u] ^ top[v])
{
if(deep[top[u]] < deep[top[v]]) swap(u, v);
ans += query(1, tid[top[u]], tid[u]);
ans -= (q_color(1, tid[f[top[u]]]) == q_color(1, tid[top[u]]));
u = f[top[u]];
}
if(deep[u] > deep[v]) swap(u, v);
ans += query(1, tid[u], tid[v]);
return ans;
} int main()
{
int i, x, y, z;
char s[10];
n = read();
m = read();
memset(son, -1, sizeof(son));
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++) a[i] = read();
for(i = 1; i < n; i++)
{
x = read();
y = read();
add(x, y);
add(y, x);
}
dfs1(1);
dfs2(1, 1);
build(1, 1, n);
for(i = 1; i <= m; i++)
{
scanf("%s", s);
x = read();
y = read();
if(s[0] == 'C')
{
z = read();
q_update(x, y, z);
}
else printf("%d\n", q_query(x, y));
}
return 0;
}

  

最新文章

  1. 每天一个linux命令(46):vmstat命令
  2. 设计模式——抽象工厂(Abstract Factory)
  3. SSIS-包调用包错误的解决方案
  4. js实现图片预览
  5. 第二十篇:在SOUI中使用分层窗口
  6. [示例]NSPredicate基础-查询数组中负荷条件的子集
  7. Android Listener 监听的几种写法
  8. 用css3写出来的进度条
  9. WAMP下 搭建Zend Framework(Hello World)实例
  10. SQL条件表达式
  11. Craking the coding interview 面试题:完美随机洗牌
  12. shell检测interface是否已分配ip,qt调用shell脚本
  13. C函数调用与栈
  14. Divisors poj2992
  15. angular $cookies、$cookieStore
  16. scrapy中的response
  17. Linux取代ifconfig指令的ip指令
  18. Nginx 和 IIS 实现动静分离(转)
  19. VS2010 配置与调试
  20. 函数式编程之-拒绝空引用异常(Option类型)

热门文章

  1. javascript实现引用数据类型的深拷贝和浅拷贝详解
  2. codechef: ADAROKS2 ,Ada Rooks 2
  3. 树形DP Gym 100496H House of Representatives
  4. view 的继承关系
  5. Spring Boot (28) actuator与spring-boot-admin
  6. maven build过程中遇到的问题以及解决方案
  7. hihocoder1365 图片排版
  8. 60查找nanopim1plus的HDMI为720p输出的问题
  9. IBatis的分页研究
  10. Farseer.net轻量级开源框架 入门篇:Where条件的终极使用