zoukankan      html  css  js  c++  java
  • BZOJ2243: [SDOI2011]染色

    【传送门:BZOJ2243


    简要题意:

      给出一棵无根树,每个节点有颜色,有两种操作:

      1.Q a b求出a到b的路径上的颜色段数

      2.C a b c将a到b路径上的所有点都变成c的颜色


    题解:

      树链剖分,很裸

      每个区间记录左端点的颜色,右端点的颜色,区间的颜色段数

      合并维护区间时,如果左子区间的右端点颜色与右子区间的左端点的颜色相同,则颜色段数--

      然后在树上跳轻重链的时候要注意对相邻区间的处理


    参考代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    struct node
    {
        int x,y,next;
    }a[210000];int len,last[110000];
    int col[110000];
    void ins(int x,int y)
    {
        len++;
        a[len].x=x;a[len].y=y;
        a[len].next=last[x];last[x]=len;
    }
    int tot[110000],dep[110000],fa[110000],son[110000];
    void pre_tree_node(int x)
    {
        tot[x]=1;son[x]=0;
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(y!=fa[x])
            {
                fa[y]=x;
                dep[y]=dep[x]+1;
                pre_tree_node(y);
                tot[x]+=tot[y];
                if(tot[y]>tot[son[x]]) son[x]=y;
            }
        }
    }
    int top[110000],ys[110000],to[110000],z;
    void pre_tree_edge(int x,int tp)
    {
        ys[x]=++z;to[z]=x;top[x]=tp;
        if(son[x]!=0) pre_tree_edge(son[x],tp);
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(y!=fa[x]&&y!=son[x]) pre_tree_edge(y,y);
        }
    }
    struct trnode
    {
        int l,r,lc,rc,s,lazy,cl,cr;
    }tr[210000];int trlen;
    void update(int now)
    {
        int lc=tr[now].lc,rc=tr[now].rc;
        if(lc!=-1)
        {
            tr[lc].s=1;
            tr[lc].cl=tr[lc].cr=tr[lc].lazy=tr[now].lazy;
        }
        if(rc!=-1)
        {
            tr[rc].s=1;
            tr[rc].cl=tr[rc].cr=tr[rc].lazy=tr[now].lazy;
        }
        tr[now].lazy=0;
    }
    void follow(int now)
    {
        int lc=tr[now].lc,rc=tr[now].rc;
        tr[now].s=tr[lc].s+tr[rc].s;
        if(tr[lc].cr==tr[rc].cl) tr[now].s--;
        tr[now].cl=tr[lc].cl;tr[now].cr=tr[rc].cr;
    }
    void bt(int l,int r)
    {
        trlen++;int now=trlen;
        tr[now].l=l;tr[now].r=r;tr[now].s=0;
        tr[now].lc=tr[now].rc=-1;
        if(l<r)
        {
            int mid=(l+r)/2;
            tr[now].lc=trlen+1;bt(l,mid);
            tr[now].rc=trlen+1;bt(mid+1,r);
        }
    }
    int getduan(int now,int l,int r)
    {
        if(tr[now].l==l&&tr[now].r==r) return tr[now].s;
        int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
        if(tr[now].lazy!=0) update(now);
        if(r<=mid) return getduan(lc,l,r);
        else if(l>mid) return getduan(rc,l,r);
        else
        {
            if(tr[lc].cr==tr[rc].cl) return getduan(lc,l,mid)+getduan(rc,mid+1,r)-1;
            else return getduan(lc,l,mid)+getduan(rc,mid+1,r);
        }
    }
    void change(int now,int l,int r,int c)
    {
        if(tr[now].l==l&&tr[now].r==r)
        {
            tr[now].cl=tr[now].cr=tr[now].lazy=c;
            tr[now].s=1;
            return ;
        }
        int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
        if(tr[now].lazy!=0) update(now);
        if(r<=mid) change(lc,l,r,c);
        else if(l>mid) change(rc,l,r,c);
        else change(lc,l,mid,c),change(rc,mid+1,r,c);
        follow(now);
    }
    int findcolor(int now,int x)
    {
        if(tr[now].l==tr[now].r) return tr[now].cl;
        int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
        if(tr[now].lazy!=0) update(now);
        if(x<=mid) return findcolor(lc,x);
        else return findcolor(rc,x);
    }
    int solve(int x,int y)
    {
        int tx=top[x],ty=top[y],ans=0;
        while(tx!=ty)
        {
            if(dep[tx]>dep[ty])
            {
                swap(tx,ty);
                swap(x,y);
            }
            ans+=getduan(1,ys[ty],ys[y]);
            if(findcolor(1,ys[ty])==findcolor(1,ys[fa[ty]])) ans--;
            y=fa[ty];ty=top[y];
        }
        if(dep[x]>dep[y])
        {
            swap(x,y);
        }
        return ans+getduan(1,ys[x],ys[y]);
    }
    void modify(int x,int y,int c)
    {
        int tx=top[x],ty=top[y];
        while(tx!=ty)
        {
            if(dep[tx]>dep[ty])
            {
                swap(tx,ty);
                swap(x,y);
            }
            change(1,ys[ty],ys[y],c);
            y=fa[ty];ty=top[y];
        }
        if(dep[x]>dep[y])
        {
            swap(x,y);
        }
        change(1,ys[x],ys[y],c);
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        len=0;memset(last,0,sizeof(last));
        for(int i=1;i<=n;i++) scanf("%d",&col[i]);
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            ins(x,y);ins(y,x);
        }
        fa[1]=0;dep[1]=0;pre_tree_node(1);
        z=0;pre_tree_edge(1,1);
        trlen=0;bt(1,z);
        char st[2];
        for(int i=1;i<=n;i++) change(1,ys[i],ys[i],col[i]);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%s%d%d",st+1,&x,&y);
            if(st[1]=='Q') printf("%d
    ",solve(x,y));
            else
            {
                int c;
                scanf("%d",&c);
                modify(x,y,c);
            }
        }
        return 0;
    }

     

  • 相关阅读:
    HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online
    LCA-tarjan understand 2
    LCA-tarjan understand
    MST-prim ElogV
    MST-kruskal ElogE+V
    transformjs 污染了 DOM?是你不了解它的强大
    检查浏览器支持Webp
    canvas实现平铺水印
    2016年总结--成长
    微信小程序体验(1):携程酒店机票火车票
  • 原文地址:https://www.cnblogs.com/Never-mind/p/8655411.html
Copyright © 2011-2022 走看看