【BZOJ 2243】染色 - 树链剖分+线段树

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int N=100001;

int n,m,color[N];   //Basic
struct G
{
    int v,nxt;
}map[N+N];
int hd[N],tt;   //Graph
int son[N],dep[N],pre[N],size[N];
int tid[N],top[N],num;  //Chain
struct T
{
    int l,r;
    int lc,rc,tag;
    int cnt;
}tr[N<<2];    //Segment Tree

inline int read(void)
{
    int s=0,f=1; char c=getchar();
    for (;c<'0'||c>'9';c=getchar()) if (c=='-') f=-1;
    for (;'0'<=c&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0';
    return s*f;
}

inline void ins(int u,int v)
{
    map[++tt].v=v;
    map[tt].nxt=hd[u];
    hd[u]=tt;
}

void find(int now,int ht)
{
    dep[now]=ht,size[now]=1;
    for (int k=hd[now];k;k=map[k].nxt)
        if (!size[map[k].v])
        {
            find(map[k].v,ht+1);
            size[now]+=size[map[k].v];
            pre[map[k].v]=now;
            if (!son[now]||size[son[now]]<size[map[k].v]) son[now]=map[k].v;
        }
}

void cut(int now,int anc)
{
    top[now]=anc,tid[now]=++num;
    if (son[now]) cut(son[now],anc);
    for (int k=hd[now];k;k=map[k].nxt)
        if (!tid[map[k].v]) cut(map[k].v,map[k].v);
}

inline void clear(int now)
{
    if (tr[now].tag==-1) return;
    tr[now<<1].tag=tr[now<<1|1].tag=tr[now].tag;
    tr[now<<1].lc=tr[now<<1].rc=tr[now].tag;
    tr[now<<1|1].lc=tr[now<<1|1].rc=tr[now].tag;
    tr[now<<1].cnt=tr[now<<1|1].cnt=1;
    tr[now].tag=-1;
}

void build(int now,int l,int r)
{
    tr[now].l=l;
    tr[now].r=r;
    tr[now].tag=-1;
    if (l^r)
    {
        int mid=l+r>>1;
        build(now<<1,l,mid);
        build(now<<1|1,mid+1,r);
    }
}

void ins(int now,int l,int r,int c)
{
    if (l<=tr[now].l&&tr[now].r<=r)
    {
        tr[now].tag=tr[now].lc=tr[now].rc=c;
        tr[now].cnt=1;
        return;
    }
    clear(now);

    int mid=tr[now].l+tr[now].r>>1;
    if (l<=mid) ins(now<<1,l,r,c);
    if (mid<r) ins(now<<1|1,l,r,c);

    tr[now].lc=tr[now<<1].lc;
    tr[now].rc=tr[now<<1|1].rc;
    tr[now].cnt=tr[now<<1].cnt+tr[now<<1|1].cnt-(tr[now<<1].rc==tr[now<<1|1].lc);
}

void init(void)
{
    n=read(),m=read();
    for (int i=1;i<=n;i++) color[i]=read();

    int u,v;
    for (int i=1;i<n;i++)
    {
        u=read(),v=read();
        ins(u,v),ins(v,u);
    }

    find(1,1);
    cut(1,1);

    build(1,1,n);
    for (int i=1;i<=n;i++) ins(1,tid[i],tid[i],color[i]);
}

inline int LCA(int x,int y)
{
    for (;top[x]^top[y];)
        if (dep[top[x]]>dep[top[y]])
            x=pre[top[x]];
        else y=pre[top[y]];
    return dep[x]<dep[y]?x:y;
}

int query(int now,int l,int r)
{
    if (l<=tr[now].l&&tr[now].r<=r) return tr[now].cnt;
    clear(now);
    int mid=tr[now].l+tr[now].r>>1,sum=0;
    if (l<=mid) sum+=query(now<<1,l,r);
    if (mid<r) sum+=query(now<<1|1,l,r);
    if (l<=mid&&mid<r) sum-=tr[now<<1].rc==tr[now<<1|1].lc;
    return sum;
}

int findcolor(int now,int loc)
{
    if (tr[now].l==tr[now].r) return tr[now].tag;
    clear(now);
    int mid=tr[now].l+tr[now].r>>1;
    return findcolor(now<<1|(loc<=mid?0:1),loc);
}

void work(void)
{
    char c; int x,y,z,p,res,uc,dc;
    for (int i=1;i<=m;i++)
    {
        scanf("\n%c",&c);
        if (c=='C')
        {
            x=read(),y=read(),z=read(),p=LCA(x,y);
            for (;top[x]^top[p];x=pre[top[x]]) ins(1,tid[top[x]],tid[x],z);
            for (;top[y]^top[p];y=pre[top[y]]) ins(1,tid[top[y]],tid[y],z);
            ins(1,tid[p],x^p?tid[x]:tid[y],z);
        }
        else
        {
            x=read(),y=read(),p=LCA(x,y);
            uc=dc=-1,res=0;
            for (;top[x]^top[p];x=pre[top[x]])
            {
                if (uc^-1) 
                {
                    dc=findcolor(1,tid[x]);
                    res-=uc==dc;
                }
                res+=query(1,tid[top[x]],tid[x]);
                uc=findcolor(1,tid[top[x]]);
            }
            dc=findcolor(1,tid[x]);
            res-=uc==dc;
            uc=dc=-1;
            for (;top[y]^top[p];y=pre[top[y]])
            {
                if (uc^-1)
                {
                    dc=findcolor(1,tid[y]);
                    res-=uc==dc;
                }
                res+=query(1,tid[top[y]],tid[y]);
                uc=findcolor(1,tid[top[y]]);
            }
            dc=findcolor(1,tid[y]);
            res-=uc==dc;
            res+=query(1,tid[p],x^p?tid[x]:tid[y]);
            printf("%d\n",res);
        }
    }
}

int main(void)
{   
    init();
    work();

    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值