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

    来自FallDream 的博客,未经允许,请勿转载,谢谢qaq


    给定一棵有n个节点的无根树和m个操作,操作有2类:

    1、将节点a到节点b路径上所有点都染成颜色c

    2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。

    请你写一个程序依次完成这m个操作。

    n,m<=100000

     

    题解:颜色信息考虑维护区间左右端点和答案,明显可以合并,所以考虑树剖之后线段树维护。复杂度$O(nlog^{2}n)$

    #include<iostream>
    #include<cstdio>
    #define pa pair<int,int>
    #define mp(x,y) make_pair(x,y)
    #define MN 100000
    using namespace std;
    inline int read()
    {
        int x = 0 , f = 1; char ch = getchar();
        while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
        while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
        return x * f;
    }
    
    struct data{
        int l,r,x;
        data(int _x=0):l(_x),r(_x){x=1;}
        data(int x,int y,int z):l(x),r(y),x(z){}
        data operator+(data b){return data(l,b.r,x+b.x-(r==b.l));}
        data operator~(){return data(r,l,x);}
    };
    struct node{int l,r,val;bool tag;data x;}T[MN*4+6];
    struct edge{int to,next;}e[MN*2+5];
    int n,m,head[MN+5],a[MN+5],cnt=0,fa[MN+5],top[MN+5],mx[MN+5],size[MN+5],dfn[MN+5],dn=0,p[MN+5],dep[MN+5];
    pa q1[MN+5],q2[MN+5];int top1,top2;
    char op[5];
    inline void ins(int f,int t)
    {
        e[++cnt]=(edge){t,head[f]};head[f]=cnt;
        e[++cnt]=(edge){f,head[t]};head[t]=cnt;
    }
    
    void dfs1(int x,int f)
    {
        fa[x]=f;size[x]=1;mx[x]=0;
        for(int i=head[x];i;i=e[i].next)
            if(e[i].to!=f)
            {
                dep[e[i].to]=dep[x]+1;dfs1(e[i].to,x);
                size[x]+=size[e[i].to];
                if(size[e[i].to]>size[mx[x]]) mx[x]=e[i].to;
            }
    }
    
    void dfs2(int x,int tp)
    {
        top[x]=tp;p[dfn[x]=++dn]=x;
        if(mx[x]) dfs2(mx[x],tp);
        for(int i=head[x];i;i=e[i].next)
            if(e[i].to!=fa[x]&&e[i].to!=mx[x])
                dfs2(e[i].to,e[i].to);
    }
    
    void pushdown(int x)
    {
        int l=x<<1,r=x<<1|1;
        T[l].x=T[r].x=data(T[x].val);
        T[l].val=T[r].val=T[x].val;T[x].val=0;
        T[l].tag=T[r].tag=1;T[x].tag=0;
    }
    
    void build(int x,int l,int r)
    {
        if((T[x].l=l)==(T[x].r=r))
        {
            T[x].x=data(a[p[l]]);
            return;
        }
        int mid=l+r>>1;
        build(x<<1,l,mid);build(x<<1|1,mid+1,r);
        T[x].x=T[x<<1].x+T[x<<1|1].x;
    }
    
    void renew(int x,int l,int r,int c)
    {
        if(T[x].l==l&&T[x].r==r)
        {
            T[x].x=data(c);T[x].val=c;T[x].tag=1;
            return;
        }
        if(T[x].tag) pushdown(x);
        int mid=T[x].l+T[x].r>>1;
        if(r<=mid) renew(x<<1,l,r,c);
        else if(l>mid) renew(x<<1|1,l,r,c);
        else renew(x<<1,l,mid,c),renew(x<<1|1,mid+1,r,c);
        T[x].x=T[x<<1].x+T[x<<1|1].x;
    }
    
    void Renew(int y,int x,int c)
    {
        while(top[x]!=top[y])
        {
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            renew(1,dfn[top[x]],dfn[x],c);
            x=fa[top[x]];
        }
        renew(1,min(dfn[y],dfn[x]),max(dfn[x],dfn[y]),c);
    }
    
    data query(int x,int l,int r)
    {
        if(T[x].l==l&&T[x].r==r) return T[x].x;
        if(T[x].tag) pushdown(x);
        int mid=T[x].l+T[x].r>>1;
        if(r<=mid) return query(x<<1,l,r);
        else if(l>mid) return query(x<<1|1,l,r);
        else return query(x<<1,l,mid)+query(x<<1|1,mid+1,r);
    }
    
    int Query(int x,int y)
    {
        top1=top2=0;data ans;bool flag=true;
        while(top[x]!=top[y])
        {
            if(dep[top[x]]>dep[top[y]])    
            {
                q1[++top1]=mp(dfn[top[x]],dfn[x]);
                x=fa[top[x]];
            }
            else
            {
                q2[++top2]=mp(dfn[top[y]],dfn[y]);
                y=fa[top[y]];
            }
        }    
        for(int i=1;i<=top1;i++) 
            ans=flag?(flag=false,~query(1,q1[i].first,q1[i].second)):(ans+~query(1,q1[i].first,q1[i].second));
        if(dep[x]<dep[y]) ans=flag?(flag=false,query(1,dfn[x],dfn[y])):(ans+query(1,dfn[x],dfn[y]));
        else ans=flag?(flag=false,~query(1,dfn[y],dfn[x])):(ans+~query(1,dfn[y],dfn[x]));
        for(int i=top2;i;i--) ans=ans+query(1,q2[i].first,q2[i].second);
        return ans.x;
    }
    
    int main()
    {
        n=read();m=read();
        for(int i=1;i<=n;i++ )a[i]=read();
        for(int i=1;i<n;i++) ins(read(),read());
        dfs1(1,0);dfs2(1,1);build(1,1,n);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",op+1);int x=read(),y=read();
            if(op[1]=='Q') printf("%d
    ",Query(x,y));
            else Renew(x,y,read());
        }
        return 0;
    }

     

  • 相关阅读:
    hdu 4622 Reincarnation 字符串hash 模板题
    NYOJ 994 海盗分金 逆向递推
    hdu 4679 Terrorist’s destroy 树形DP
    Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
    hdu 5535 Cake 构造+记忆化搜索
    poj 3415 Common Substrings 后缀数组+单调栈
    poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串
    poj 2774 Long Long Message 后缀数组LCP理解
    hdu 3518 Boring counting 后缀数组LCP
    poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
  • 原文地址:https://www.cnblogs.com/FallDream/p/bzoj2243.html
Copyright © 2011-2022 走看看