#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp(i,j) i^=j,j^=i,i^=j
#define ls(i) (i<<1)
#define rs(i) ((i<<1)|1)
int n, m;
struct __V__{
vector<int> e;
int d;
int h;
int id;
int fa;
int top;
int son;
int size;
}v[100009];
int rk[100009];
struct V{
int l, r, lazy = -1;
int ans = 0, lc = -1, rc = -2;
};
inline V hb(V a, V b) {
V c;
c.l = a.l;
c.r = b.r;
c.ans = a.ans + b.ans - (a.rc == b.lc);
c.lc = a.lc, c.rc = b.rc;
return c;
}
struct TREE{
V tree[400009];
inline void build(int i, int l, int r) {
tree[i].l = l;
tree[i].r = r;
if(l == r) {
tree[i].ans = 1;
tree[i].lc = tree[i].rc = v[rk[l]].d;
return;
}
int mid = (l + r) >> 1;
build(ls(i), l, mid);
build(rs(i), mid +1, r);
tree[i] = hb(tree[ls(i)], tree[rs(i)]);
}
inline void pd(int i) {
tree[ls(i)].ans = 1;
tree[ls(i)].lc = tree[i].lazy;
tree[ls(i)].rc = tree[i].lazy;
tree[ls(i)].lazy = tree[i].lazy;
tree[rs(i)].ans = 1;
tree[rs(i)].lc = tree[i].lazy;
tree[rs(i)].rc = tree[i].lazy;
tree[rs(i)].lazy = tree[i].lazy;
tree[i].lazy = -1;
}
inline void change(int i, int l, int r, int c) {
if(l <= tree[i].l && tree[i].r <= r) {
tree[i].ans = 1;
tree[i].lc = c;
tree[i].rc = c;
tree[i].lazy = c;
return;
}
if(tree[i].lazy >= 0) pd(i);
int mid = (tree[i].l + tree[i].r) >> 1;
if(l <= mid) change(ls(i), l, r, c);
if(mid < r) change(rs(i), l, r, c);
tree[i] = hb(tree[ls(i)], tree[rs(i)]);
}
inline V ask(int i, int l, int r){
if(l <= tree[i].l && tree[i].r <= r) {
return tree[i];
}
if(tree[i].lazy >= 0) pd(i);
int mid = (tree[i].l + tree[i].r) >> 1;
if(l <= mid && mid < r) {
V _x = ask(ls(i), l, r), _y = ask(rs(i), l, r);
return hb(_x, _y);
}
if(l <= mid) return ask(ls(i), l, r);
return ask(rs(i), l, r);
}
};
inline void dfs(int u, int fa, int d) {
v[u].h = d;
v[u].fa = fa;
v[u].size = 1;
int cnt = -114514;
for(auto to: v[u].e) {
if(to == fa) continue;
dfs(to, u, d + 1);
v[u].size += v[to].size;
if(cnt < v[to].size) v[u].son = to, cnt = v[to].size;
}
}
int _cnt;
inline void dfs2(int u, int top) {
v[u].id = ++_cnt;
rk[_cnt] = u;
v[u].top = top;
if(!v[u].son) return;
dfs2(v[u].son, top);
for(auto to: v[u].e) {
if(to == v[u].fa) continue;
if(to == v[u].son) continue;
dfs2(to, to);
}
}
TREE tree;
inline void cg(int x, int y, long long c) {
while(v[x].top != v[y].top) {
if(v[v[x].top].h < v[v[y].top].h) x ^= y, y ^= x, x ^= y;
tree.change(1, v[v[x].top].id, v[x].id, c);
x = v[v[x].top].fa;
}
if(v[x].id > v[y].id) x ^= y, y ^= x, x ^= y;
tree.change(1, v[x].id, v[y].id, c);
}
inline V ask(int x, int y) {
V l, r;
long long fx = v[x].top, fy = v[y].top;
while(fx != fy) {
if(v[fx].h >= v[fy].h) {
V _z = l, _y = tree.ask(1, v[fx].id, v[x].id);
l = hb(_y, _z), x = v[fx].fa, fx = v[x].top;
}
else {
V _z = r, _y = tree.ask(1, v[fy].id, v[y].id);
r = hb(_y, _z), y = v[fy].fa, fy = v[y].top;
}
}
if(v[x].id <= v[y].id) {
V _z = r, _y = tree.ask(1, v[x].id, v[y].id);
r = hb(_y, _z);
}
else {
V _z = l, _y = tree.ask(1, v[y].id, v[x].id);
l = hb(_y, _z);
}
sp(l.lc, l.rc);
return hb(l, r);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> v[i].d;
}
for(int i = 1, _u, _v; i < n; i++) {
cin >> _u >> _v;
v[_u].e.push_back(_v);
v[_v].e.push_back(_u);
}
dfs(1, 0, 1);
dfs2(1, 1);
tree.build(1, 1, n);
while(m--) {
char op;
int x, y, c;
cin >> op;
if(op == 'C') {
cin >> x >> y >> c;
cg(x, y, c);
}
if(op == 'Q') {
cin >> x >> y;
cout << ask(x, y).ans << "\n";
}
}
return 0;
}剪贴板
剪贴板 pjv575ea
剪贴板信息
剪贴板信息
- 收录
- 更新