HYSBZ - 2243 [SDOI2011]染色(树链剖分)

点我看题

 题意:中文题题意很明显。

分析:树链剖分+线段树(区间查询,单点查询,区间合并+区间合并),要注意的是,在Update时,如果更新的不是一个结点蕴含的区间,要注意两个区间相邻结点颜色是否相同,在Find时,也要看两条链相邻结点颜色是否相同,这里的相邻指的是从一个点只要走一条边就能到达另一个结点。

参考代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson rt<<1
#define rson rt<<1|1
const int maxn = 1e5+10;
int n,m;
int col[maxn];//初始颜色
int head[maxn],tot;
struct Edge{
    int to,next;
}edge[maxn<<1];
int siz[maxn],fa[maxn],son[maxn],dep[maxn];
int p[maxn],fp[maxn],top[maxn],poi;
//线段树,区间更新区间查询
int sum[maxn<<2],lc[maxn<<2],rc[maxn<<2],tag[maxn<<2];//区间颜色和,左右颜色

void Init()
{
    tot = poi = 0;
    mem(head,-1);
    mem(son,-1);
}

void AddEdge( int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

void dfs1( int u, int pre, int d)
{
    siz[u] = 1;
    fa[u] = pre;
    dep[u] = d;
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != pre)
        {
            dfs1(v,u,d+1);
            siz[u] += siz[v];
            if( son[u] == -1 || siz[v] > siz[son[u]])
                son[u] = v;
        }
    }
}

void dfs2( int u, int tp)
{
    top[u] = tp;
    p[u] = ++poi;
    fp[p[u]] = u;
    if( son[u] == -1)
        return;
    dfs2(son[u],tp);
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != fa[u] && v!= son[u])
            dfs2(v,v);
    }
}

void PushUp( int rt)
{
    sum[rt] = sum[lson]+sum[rson]-(rc[lson]==lc[rson]);
    lc[rt] = lc[lson];
    rc[rt] = rc[rson];
}

void Build( int l, int r, int rt)
{
    tag[rt] = 0;
    if( l == r)
    {
        sum[rt] = 1;
        lc[rt] = rc[rt] = col[fp[l]];
        return;
    }
    int mid = (l+r)>>1;
    Build(l,mid,lson);
    Build(mid+1,r,rson);
    PushUp(rt);
}

void PushDown( int rt)
{
    if( tag[rt])
    {
        sum[lson] = sum[rson] = 1;
        lc[lson] = lc[rson] = rc[lson] = rc[rson] = lc[rt];
        tag[lson] = tag[rson] = tag[rt];
        tag[rt] = 0;
    }
}

void Update( int l, int r, int rt, int L, int R, int color)
{
    if( L == l && R == r)
    {
        sum[rt] = 1;
        lc[rt] = rc[rt] = color;
        tag[rt] = 1;
        return;
    }
    PushDown(rt);
    int mid = (l+r)>>1;
    if( R <= mid)
        Update(l,mid,lson,L,R,color);
    else if( L > mid)
        Update(mid+1,r,rson,L,R,color);
    else
    {
        Update(l,mid,lson,L,mid,color);
        Update(mid+1,r,rson,mid+1,R,color);
    }
    PushUp(rt);
}

int Query( int l, int r, int rt, int L, int R)
{
    if( L == l && R == r)
        return sum[rt];
    PushDown(rt);
    int mid = (l+r)>>1;
    if( R <= mid)
        return Query(l,mid,lson,L,R);
    else if( L > mid)
        return Query(mid+1,r,rson,L,R);
    else
        return Query(l,mid,lson,L,mid)+Query(mid+1,r,rson,mid+1,R)-(rc[lson]==lc[rson]);
}

void Change( int u, int v, int color)
{
    int t1 = top[u];
    int t2 = top[v];
    while( t1 != t2)
    {
        if( dep[t1] < dep[t2])
        {
            swap(u,v);
            swap(t1,t2);
        }
        Update(1,poi,1,p[t1],p[u],color);
        u = fa[t1];
        t1 = top[u];
    }
    if( dep[u] > dep[v])
        swap(u,v);
    Update(1,poi,1,p[u],p[v],color);
}

int Check( int l, int r, int rt, int pos)
{
    if( l == pos && r == pos)
        return lc[rt];
    PushDown(rt);
    int mid = (l+r)>>1;
    if( pos <= mid)
        return Check(l,mid,lson,pos);
    else
        return Check(mid+1,r,rson,pos);
}

int Find( int u, int v)
{
    int ans = 0;
    int t1 = top[u];
    int t2 = top[v];
    while( t1 != t2)
    {
        if( dep[t1] < dep[t2])
        {
            swap(u,v);
            swap(t1,t2);
        }
        ans += Query(1,poi,1,p[t1],p[u]);
        if( Check(1,poi,1,p[t1]) == Check(1,poi,1,p[fa[t1]]))
            ans--;
        u = fa[t1];
        t1 = top[u];
    }
    if( dep[u] > dep[v])
        swap(u,v);
    ans += Query(1,poi,1,p[u],p[v]);
    return ans;
}

int main()
{
    while( ~scanf("%d%d",&n,&m))
    {
        Init();
        for( int i = 1; i <= n; i++)
            scanf("%d",&col[i]);
        int u,v;
        for( int i = 1; i < n; i++)
        {
            scanf("%d%d",&u,&v);
            AddEdge(u,v);
            AddEdge(v,u);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        Build(1,poi,1);
        while( m--)
        {
            char op[3];
            int a,b,color;
            scanf("%s",op);
            if( op[0] == 'C')
            {
                scanf("%d%d%d",&a,&b,&color);
                Change(a,b,color);
            }
            else if( op[0] == 'Q')
            {
                scanf("%d%d",&a,&b);
                printf("%d\n",Find(a,b));
            }
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值