【bzoj2243】【SDOI2011】染色

题目描述

给定一棵有n个节点的无根树和m个操作,操作有2类:
1、将节点a到节点b路径上所有点都染成颜色c;
2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),
如“112221”由3段组成:“11”、“222”和“1”。
请你写一个程序依次完成这m个操作。


输入

第一行包含2个整数n和m,分别表示节点数和操作数;
第二行包含n个正整数表示n个节点的初始颜色
下面 行每行包含两个整数x和y,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。


输出

对于每个询问操作,输出一行答案。


样例输入

6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5


样例输出

3
1
2

数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。



题解

这道题相较模版只有一点小变动,在线段树上合并区间的时候,如果左区间的右端点=右区间的左端点,那么区间颜色段数-1;在链上修改的时候,当从一条链跳到另一条链的时候,如果当前链的链头与跳到的链的链尾颜色一样的话,颜色数也要-1。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=1e5+5;

int fir[maxn],to[maxn*2],nex[maxn*2],ecnt;
int wt[maxn],id[maxn],top[maxn],sz[maxn],dep[maxn],fa[maxn],son[maxn];
int n,m,w[maxn],x,y,z,cnt;
char op;

struct SegmentTree{
    int l,r,cl,cr,v,set;
}st[maxn*4];

void add_edge(int u,int v){
    nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;
}

void dfs1(int x,int f,int deep){
    dep[x]=deep;
    fa[x]=f;
    sz[x]=1;
    int maxson=-1;
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==f) continue;
        dfs1(v,x,deep+1);
        sz[x]+=sz[v];
        if(sz[v]>maxson) maxson=sz[v],son[x]=v;
    }
}

void dfs2(int x,int topf){
    top[x]=topf;
    id[x]=++cnt;
    wt[cnt]=w[x];
    if(!son[x]) return ;
    dfs2(son[x],topf);
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}

void pushup(int root){
    st[root].v=st[root*2].v+st[root*2+1].v;
    if(st[root*2].cr==st[root*2+1].cl) st[root].v--;
    st[root].cl=st[root*2].cl;
    st[root].cr=st[root*2+1].cr;
}

void build(int root,int l,int r){
    st[root].l=l;st[root].r=r;st[root].set=-1;
    if(l==r) st[root].v=1,st[root].cl=st[root].cr=wt[l];
    else{
        int m=l+r>>1;
        build(root*2,l,m);
        build(root*2+1,m+1,r);
        pushup(root);
    }
}

void pushdown(int root){
    if(st[root].set!=-1){
        st[root*2].v=1;
        st[root*2+1].v=1;
        st[root*2].set=st[root*2+1].set=st[root*2].cl=st[root*2].cr=st[root*2+1].cl=st[root*2+1].cr=st[root].set;
        st[root].set=-1;
    }
}

void change(int root,int l,int r,int val){
    if(st[root].l>r||st[root].r<l) return ;
    if(st[root].l>=l&&st[root].r<=r){
        st[root].set=val;
        st[root].v=1;
        st[root].cl=val;
        st[root].cr=val;
    }
    else{
        pushdown(root);
        change(root*2,l,r,val);
        change(root*2+1,l,r,val);
        pushup(root);
    }
}

int query(int root,int l,int r){
    if(st[root].l>=l&&st[root].r<=r) return st[root].v;
    int ans=0;
    pushdown(root);
    int m=st[root].l+st[root].r>>1;
    if(l<=m) ans+=query(root*2,l,r);  
    if(r>m) ans+=query(root*2+1,l,r);
    if(l<=m&&r>m&&st[root*2].cr==st[root*2+1].cl) ans--;
    return ans;
}

int find(int root,int x){
    if(st[root].l==st[root].r) return st[root].cl;
    else{
        pushdown(root);
        int m=st[root].l+st[root].r>>1;
        if(m>=x) return find(root*2,x);
        return find(root*2+1,x);
    }
}

void Change(int x,int y,int val){
    int f1=top[x],f2=top[y];
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        change(1,id[f1],id[x],val);
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    change(1,id[x],id[y],val);
}

int Query(int x,int y){
    int f1=top[x],f2=top[y],ans=0;
    while(f1!=f2){
         if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        ans+=query(1,id[f1],id[x]);
        int x1=find(1,id[f1]);
        int x2=find(1,id[fa[f1]]);
        if(x1==x2) ans--;
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans+=query(1,id[x],id[y]);
    return ans;
}

template<typename T>void read(T& aa){
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
    if(cc=='-') ff=-1,cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    aa*=ff;
}

int main(){
    read(n),read(m);
    for(int i=1;i<=n;i++) read(w[i]);
    for(int i=1;i<n;i++){
        read(x),read(y);
        add_edge(x,y);
        add_edge(y,x);
    }
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,n);
    while(m--){
        cin>>op>>x>>y;
        if(op=='C'){
            read(z);
            Change(x,y,z);
        }
        else printf("%d
",Query(x,y));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/rlddd/p/9554646.html