1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| #include <bits/stdc++.h> using namespace std;
const int N = 1e5 + 10; int n, m, cnt, idx, lazy[N<<2]; int head[N<<1], dep[N], siz[N]; int tp[N], son[N], fa[N], id[N]; int w[N], wl[N];
struct Edge { int to, nxt; } e[N<<1]; struct seg { int lc, rc, val; seg(int lc, int rc, int val): lc(lc), rc(rc), val(val) {} seg() {} } tree[N<<2];
#define ls ((rt)<<1) #define rs ((rt)<<1|1)
void pushup(int rt) { tree[rt].lc = tree[ls].lc; tree[rt].rc = tree[rs].rc; tree[rt].val = tree[ls].val + tree[rs].val; if (tree[ls].rc == tree[rs].lc) --tree[rt].val; return ; }
seg Merge(seg L, seg R) { seg cur; cur.lc = L.lc, cur.rc = R.rc; cur.val = L.val + R.val; if (L.rc == R.lc) --cur.val; return cur; }
void pushdown(int rt, int l, int r) { if (!lazy[rt]) return ; int mid = (l+r) >> 1; lazy[ls] = lazy[rs] = lazy[rt]; tree[ls] = seg(lazy[rt], lazy[rt], 1); tree[rs] = seg(lazy[rt], lazy[rt], 1); lazy[rt] = 0; return ; }
void upd(int rt, int l, int r, int L, int R, int col) { if (L<=l && r<=R) { lazy[rt] = col; tree[rt] = seg(col, col, 1); return ; } pushdown(rt, l, r); int mid = (l+r) >> 1; if (L<=mid) upd(ls, l, mid, L, R, col); if (R>mid) upd(rs, mid+1, r, L, R, col); pushup(rt); return ; }
seg query(int rt, int l, int r, int L, int R) { if (L<=l && r<=R) return tree[rt]; pushdown(rt, l, r); int mid = (l+r) >> 1, tag = 0; seg ans1, ans2; if (L<=mid) ans1 = query(ls, l, mid, L, R), ++tag; if (R>mid) ans2 = query(rs, mid+1, r, L, R), tag += 2; if (tag == 1) return ans1; if (tag == 2) return ans2; return Merge(ans1, ans2); }
void addEdge(int u, int v) { e[++cnt].to = v; e[cnt].nxt = head[u]; head[u] = cnt; return ; }
void dfs1(int cur, int f) { dep[cur] = dep[f] + 1; fa[cur] = f, siz[cur] = 1; int heavyson = -1; for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (to == f) continue; dfs1(to, cur), siz[cur] += siz[to]; if (siz[to] > heavyson) heavyson = siz[to], son[cur] = to; } return ; }
void dfs2(int cur, int Tp) { tp[cur] = Tp, id[cur] = ++idx, wl[idx] = w[cur]; if (!son[cur]) return ; dfs2(son[cur], Tp); for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (to == fa[cur] || to == son[cur]) continue; dfs2(to, to); } return ; }
void up(int l, int r, int col) { while (tp[l] ^ tp[r]) { if (dep[tp[l]] < dep[tp[r]]) swap(l, r); upd(1, 1, n, id[tp[l]], id[l], col); l = fa[tp[l]]; } if (dep[l] > dep[r]) swap(l, r); upd(1, 1, n, id[l], id[r], col); return ; }
const seg NUL = seg(0, 0, 0); int qu(int l, int r) { int belong = 0; seg ans[2]; ans[0] = ans[1] = NUL; while (tp[l] ^ tp[r]) { if (dep[tp[l]] < dep[tp[r]]) { belong = 1 - belong; swap(l, r); } seg cur = query(1, 1, n, id[tp[l]], id[l]); if (belong) ans[1] = Merge(cur, ans[1]); else ans[0] = seg(ans[0].lc, cur.lc, ans[0].val+cur.val-(ans[0].rc==cur.rc)); l = fa[tp[l]]; } if (dep[l] > dep[r]) { belong = 1 - belong; swap(l, r); } seg cur = query(1, 1, n, id[l], id[r]); if (!belong) ans[1] = Merge(cur, ans[1]); else ans[0] = seg(ans[0].lc, cur.lc, ans[0].val+cur.val-(ans[0].rc==cur.rc)); return Merge(ans[0], ans[1]).val; }
void build(int rt, int l, int r) { if (l==r) { tree[rt].val = 1; tree[rt].lc = tree[rt].rc = wl[l]; return ; } int mid = (l+r) >> 1; build(ls, l, mid), build(rs, mid+1, r); pushup(rt); return ; }
void read(int &ret) { ret = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { ret = (ret<<1) + (ret<<3) + (ch^48); ch = getchar(); } return ; }
int main() { read(n), read(m); for (int i=1; i<=n; ++i) read(w[i]); for (int i=1; i<n; ++i) { int u, v; read(u), read(v); addEdge(u, v), addEdge(v, u); } dfs1(1, 1), dfs2(1, 1); build(1, 1, n); while (m--) { char opt; cin >> opt; int u, v, color; read(u), read(v); if (opt == 'C') read(color), up(u, v, color); else printf("%d\n", qu(u, v)); } return 0; }
|