List wants to travel
给一棵树,每条边有个颜色,两种操作:
先建立树剖然后用线段树维护,记录一个区间内的颜色段数,最左颜色和最右颜色,然后就可以区间合并了,需要注意合并的顺序,没想清楚很容易就wa了,我想的是总是按查询的时间顺序把区间挨个接起来,最后合并两边链的时候反转一个区间,接在另一个后面。
#include
#include
#include
#include
#include
using namespace std;
const int NO = -0xdead;
const int N = 80005;
vector<int>G[N];
int sav[N][3];
int dep[N], fa[N], son[N], sz[N];
void dfs1(int rt) {
sz[rt] = 1;
for(int k = 0; k < G[rt].size(); ++k) {
int v = G[rt][k];
if(v == fa[rt]) continue;
dep[v] = dep[rt]+1;
fa[v] = rt;
dfs1(v);
sz[rt] += sz[v];
if(son[rt] == 0 || sz[son[rt]] < sz[v]) son[rt] = v;
}
}
int w[N], atw[N], top[N], wcnt;
void dfs2(int rt, int tp) {
w[rt] = ++wcnt, atw[wcnt] = rt;
top[rt] = tp;
if(son[rt]) dfs2(son[rt], tp);
for(int k = 0; k < G[rt].size(); ++k) {
int v = G[rt][k];
if(v == fa[rt] || v == son[rt]) continue;
dfs2(v, v);
}
}
struct node{
int sum, l, r;
node(){ sum = 0, l = NO, r = NO; }
node operator + (const node& z) {
node res;
if(r == NO && z.l == NO) return res;
if(r == NO && z.l != NO) return z;
else if(r != NO && z.l == NO) return *this;
res.sum = sum+z.sum;
if(r == z.l) res.sum--;
res.l = l, res.r = z.r;
return res;
}
}tree[N<<2];
int lazy[N<<2];
inline void push_down(int rt) {
if(lazy[rt] != NO) {
tree[rt<<1].sum = tree[rt<<1|1].sum = 1;
tree[rt<<1].l = tree[rt<<1].r = lazy[rt];
tree[rt<<1|1].l = tree[rt<<1|1].r = lazy[rt];
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
lazy[rt] = NO;
}
}
void update(int rt, int l, int r, int ql, int qr, int v) {
if(ql <= l && qr >= r) {
tree[rt].sum = 1;
tree[rt].l = tree[rt].r = v;
lazy[rt] = v;
return;
}
push_down(rt);
int mid = (l+r) >> 1;
if(ql <= mid) update(rt<<1, l, mid, ql, qr, v);
if(qr > mid) update(rt<<1|1, mid+1, r, ql, qr, v);
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
node query(int rt, int l, int r, int ql, int qr) {
if(ql <= l && qr >= r) {
return tree[rt];
}
push_down(rt);
node res; int mid = (l+r) >> 1;
if(ql <= mid){
res = res+query(rt<<1, l, mid, ql, qr);
}
if(qr > mid){
res = res+query(rt<<1|1, mid+1, r, ql, qr);
}
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
return res;
}
int query(int va, int vb) {
int f1 = top[va], f2 = top[vb];
node a, b;
while(f1 != f2) {
if(dep[f1] > dep[f2]) {
a = query(1, 1, wcnt, w[f1], w[va])+a;
va = fa[f1], f1 = top[va];
}
else{
b = query(1, 1, wcnt, w[f2], w[vb])+b;
vb = fa[f2], f2 = top[vb];
}
}
if(va == vb){
swap(b.l, b.r);
a = b+a;
return a.sum;
}
if(dep[va] < dep[vb]) {
b = query(1, 1, wcnt, w[son[va]], w[vb])+b;
swap(b.l, b.r);
a = b+a;
return a.sum;
}
else {
a = query(1, 1, wcnt, w[son[vb]], w[va])+a;
swap(b.l, b.r);
a = b+a;
return a.sum;
}
}
void update(int va, int vb, int v) {
int f1 = top[va], f2 = top[vb];
while(f1 != f2) {
if(dep[f1] < dep[f2]){
swap(f1, f2);
swap(va, vb);
}
update(1, 1, wcnt, w[f1], w[va], v);
va = fa[f1], f1 = top[va];
}
if(va == vb) return;
if(dep[va] < dep[vb]) swap(va, vb);
update(1, 1, wcnt, w[son[vb]], w[va], v);
}
char s[15];
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 1; i <= n; ++i) G[i].clear();
for(int a, b, c, i = 1; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
G[a].push_back(b);
G[b].push_back(a);
sav[i][0] = a, sav[i][1] = b, sav[i][2] = c;
}
memset(son, 0, sizeof(son));
fa[1] = 0, dep[1] = 0;
dfs1(1); wcnt = 0;
dfs2(1, 1);
for(int i = 1; i <= wcnt; ++i) {
tree[i].sum = 0;
tree[i].l = tree[i].r = NO;
lazy[i] = NO;
}
for(int i = 1; i < n; ++i) {
if(dep[sav[i][0]] > dep[sav[i][1]]) swap(sav[i][0], sav[i][1]);
update(1, 1, wcnt, w[sav[i][1]], w[sav[i][1]], sav[i][2]);
}
while(m--) {
scanf("%s", s);
if(s[0] == 'Q') {
int l, r;
scanf("%d%d", &l, &r);
if(l == r) { puts("0"); continue; }
printf("%d\n", query(l, r));
}
else if(s[0] == 'C') {
int l, r, v;
scanf("%d%d%d", &l, &r, &v);
update(l, r, v);
}
}
}
return 0;
}