[BZOJ2243]染色 做题笔记

·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
题目来源http://www.lydsy.com/JudgeOnline/problem.php?id=2243
线段树维护色块数量,区间最左端的颜色,区间最右端的颜色。合并区间时,如果左区间的最右端的颜色等于右区间的最左端的颜色,注意-1。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100010,inf=0x7fffffff;
#define lch now<<1
#define rch now<<1|1
struct Tree {
    int col,l,r,num,lc,rc;
}I[N<<2];
int n,m,num,a,b,tot,L,R,val,pos,cnt;
int xx,yy,head[N],ver[N<<1],nxt[N<<1];
int son[N],top[N],w[N],C[N];
int dep[N],fa[N],sz[N],id[N];
int Lc,Rc,u,v,c;
void add(int u,int v) {
    ver[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
void dfs_1 (int u,int from) {
    sz[u]=1,son[u]=0,fa[u]=from;
    for (int i=head[u];i;i=nxt[i]) {
        int v=ver[i];
        if (v==fa[u]) continue;
        dep[v]=dep[u]+1; fa[v]=u;
        dfs_1(v,u); sz[u]+=sz[v];
        if (!son[u]||sz[son[u]]<sz[v]) son[u]=v;
    }
}
void dfs_2 (int u,int st) {
    w[u]=++cnt;top[u]=st;id[cnt]=u;
    if (son[u]) dfs_2(son[u],st);
    for (int i=head[u];i;i=nxt[i]) {
        int v=ver[i];
        if (v==son[u]||v==fa[u]) continue;
        dfs_2(v,v);
    }
}
void pushup (int now) {
    I[now].lc=I[lch].lc;I[now].rc=I[rch].rc;
    int ans=I[lch].num+I[rch].num;
    if (I[lch].rc==I[rch].lc) ans--;
    I[now].num=ans;
}
void pushdown (int now) {
    if (!I[now].col) return;//
    I[lch].col=I[rch].col=I[now].col;
    I[lch].num=I[rch].num=1;
    I[lch].lc=I[lch].rc=I[now].lc;//
    I[rch].lc=I[rch].rc=I[now].lc;//
    I[now].col=0;
}
void build (int now,int l,int r) {
    I[now].l=l;I[now].r=r;I[now].num=0;
    if (l==r) return ;
    int mid=(l+r)>>1;
    build(lch,l,mid);build(rch,mid+1,r);
}
void change (int now,int l,int r,int x) {
    if (I[now].l==l&&I[now].r==r) {
        I[now].num=I[now].col=1;
        I[now].lc=I[now].rc=x;
        return ;
    }
    pushdown(now);
    int mid=(I[now].l+I[now].r)>>1;
    if (r<=mid) change(lch,l,r,x);
    else if (l>mid) change(rch,l,r,x);
    else change(lch,l,mid,x),change(rch,mid+1,r,x);
    pushup(now);
}
int query (int now,int l,int r,int L,int R) {
    if (I[now].l==L) Lc=I[now].lc;
    if (I[now].r==R) Rc=I[now].rc;
    if (I[now].l==l&&I[now].r==r) return I[now].num;
    pushdown(now);
    int mid=(I[now].l+I[now].r)>>1;
    if (r<=mid) return query(lch,l,r,L,R);
    else if (l>mid) return query(rch,l,r,L,R);
    else {
        int ans=query(lch,l,mid,L,R)+query(rch,mid+1,r,L,R);
        if (I[lch].rc==I[rch].lc) ans--;
        return ans;
    }
    pushup(now);
}
int Get (int u,int v,int opt,int c) {
    int ans=0;
    if (opt==1) {
        while (top[u]!=top[v]) {
            if (dep[top[u]]<dep[top[v]]) swap(u,v);
            change(1,w[top[u]],w[u],c);
            u=fa[top[u]];
        }
        if (dep[u]>dep[v]) swap(u,v);
        change(1,w[u],w[v],c);
    }
    else {
        int ans1=-1,ans2=-1;
        while (top[u]!=top[v]) {
            if (dep[top[u]]<dep[top[v]]) swap(u,v),swap(ans1,ans2);
            int L=w[top[u]],R=w[u];
            ans+=query(1,L,R,L,R);
            if (Rc==ans1) ans--;
            u=fa[top[u]],ans1=Lc;
        }
        if (dep[u]<dep[v]) swap(u,v),swap(ans1,ans2);
        ans+=query(1,w[v],w[u],w[v],w[u]);
        if (Rc==ans1) ans--;//注意判断
        if (Lc==ans2) ans--;//
    }   
    return ans;
}
int main () {
    char str[30];
    scanf("%d%d",&n,&m);
    cnt=0;tot=1;
    for (int i=1;i<=n;i++) scanf("%d",&C[i]);
    for (int i=1;i<n;i++) {
        scanf("%d%d",&u,&v);
        add(u,v); add(v,u);
    }
    dfs_1(1,0);
    dfs_2(1,1);
    build(1,1,n);
    for (int i=1;i<=n;i++)
        change(1,w[i],w[i],C[i]);
    while (m--) {
        scanf("%s",str);
        if (str[0]=='C') {
            scanf("%d%d%d",&u,&v,&c);
            Get(u,v,1,c);
        }
        else {
            scanf("%d%d",&u,&v);
            printf("%d\n",Get(u,v,2,0));
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值