• HYSBZ 2243(树链剖分)


    题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/D

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

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

    2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段);

    分析:树链剖分将信息映射到线段树后,线段树则要维护区间的两个端点值,在区间合并时”左孩子的右端点“与“右孩子的左端点”相同时总数减一。。。

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 10007
    #define inf 0x3f3f3f3f
    #define N 100010
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    
    struct edge
    {
        int to,next;
        edge(){}
        edge(int to,int next):to(to),next(next){}
    }e[N<<1];
    int head[N<<1],tot;
    int top[N];//top[v]表示v所在的重链的顶端节点
    int fa[N];//父亲节点
    int dep[N];//深度
    int sz[N];//si[v]表示以v为根节点的子树的节点数
    int son[N];//重儿子
    int p[N];//p[v]表示v与其父亲节点的连边在线段树中的位置
    int fp[N];//与p数组相反
    int pos;//所有链构成的线段树总长度
    int sum[N<<2],col[N<<2],lc[N<<2],rc[N<<2],a[N];
    void addedge(int u,int v)
    {
        e[tot]=edge(v,head[u]);
        head[u]=tot++;
    }
    void init()
    {
        tot=0;FILL(head,-1);
        pos=0;FILL(son,-1);
    }
    void dfs(int u,int f,int d)
    {
        dep[u]=d;sz[u]=1;fa[u]=f;
        for(int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].to;
            if(v==f)continue;
            dfs(v,u,d+1);
            sz[u]+=sz[v];
            if(son[u]==-1||sz[son[u]]<sz[v])son[u]=v;
        }
    }
    void getpos(int u,int sp)
    {
        top[u]=sp;
        p[u]=++pos;
        fp[pos]=u;
        if(son[u]==-1)return;
        getpos(son[u],sp);
        for(int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].to;
            if(v!=son[u]&&v!=fa[u])
            {
                getpos(v,v);
            }
        }
    }
    void Pushup(int rt)
    {
        int ls=rt<<1,rs=ls|1;
        sum[rt]=sum[ls]+sum[rs];
        lc[rt]=lc[ls];rc[rt]=rc[rs];
        if(lc[rs]==rc[ls])sum[rt]--;
    }
    void Pushdown(int rt)
    {
        if(col[rt])
        {
            int ls=rt<<1,rs=ls|1;
            col[ls]=col[rs]=col[rt];
            sum[ls]=sum[rs]=1;
            lc[ls]=rc[ls]=lc[rs]=rc[rs]=col[rt];
            col[rt]=0;
        }
    }
    void build(int l,int r,int rt)
    {
        col[rt]=0;
        if(l==r)
        {
            lc[rt]=rc[rt]=a[fp[l]];
            sum[rt]=1;
            return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        Pushup(rt);
    }
    void update(int L,int R,int c,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            lc[rt]=rc[rt]=c;
            sum[rt]=1;col[rt]=c;
            return;
        }
        Pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)update(L,R,c,lson);
        if(m<R)update(L,R,c,rson);
        Pushup(rt);
    }
    int query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)return sum[rt];
        Pushdown(rt);
        int m=(l+r)>>1;
        int res=0;
        if(R<=m)res=query(L,R,lson);
        else if(L>m)res=query(L,R,rson);
        else
        {
            res=query(L,m,lson)+query(m+1,R,rson);
            if(rc[rt<<1]==lc[rt<<1|1])res--;
        }
        return res;
    }
    int query1(int ps,int l,int r,int rt)
    {
        if(l==r)
        return lc[rt];
        Pushdown(rt);
        int m=(l+r)>>1;
        if(ps<=m)return query1(ps,lson);
        else return query1(ps,rson);
    }
    void update_tree(int u,int v,int c)
    {
        int fu=top[u],fv=top[v];
        while(fu!=fv)
        {
            if(dep[fu]<dep[fv])
            {
                swap(fu,fv);
                swap(u,v);
            }
            update(p[fu],p[u],c,1,pos,1);
            u=fa[fu];fu=top[u];
        }
        if(dep[u]>dep[v])swap(u,v);
        update(p[u],p[v],c,1,pos,1);
    }
    int lca(int u,int v)
    {
        int fu=top[u],fv=top[v];
        int res=0;
        while(fu!=fv)
        {
            if(dep[fu]<dep[fv])
            {
                swap(fu,fv);
                swap(u,v);
            }
            res+=query(p[fu],p[u],1,pos,1);
            if(query1(p[fu],1,pos,1)==query1(p[fa[fu]],1,pos,1))res--;
            u=fa[fu];fu=top[u];
        }
        if(dep[u]>dep[v])swap(u,v);
        res+=query(p[u],p[v],1,pos,1);
        return res;
    }
    int main()
    {
        int n,m,u,v,w;
        char op[10];
        while(scanf("%d%d",&n,&m)>0)
        {
            init();
            for(int i=1;i<=n;i++)scanf("%d",&a[i]);
            for(int i=1;i<n;i++)
            {
                scanf("%d%d",&u,&v);
                addedge(u,v);
                addedge(v,u);
            }
            dfs(1,0,0);
            getpos(1,1);
            build(1,pos,1);
            while(m--)
            {
                scanf("%s",op);
                if(op[0]=='C')
                {
                    scanf("%d%d%d",&u,&v,&w);
                    update_tree(u,v,w);
                }
                else
                {
                    scanf("%d%d",&u,&v);
                    printf("%d
    ",lca(u,v));
                }
            }
        }
    }
    View Code
  • 相关阅读:
    JAVA基础-抽象类和接口
    JAVA基础-多态
    JAVA基础-继承机制
    C++(二十七) — 深拷贝、浅拷贝、复制构造函数举例
    C++(二十六) — 构造函数、析构函数、对象数组、复制构造函数
    C++(二十五) — 类的封装、实现、设计
    C++(二十四) — 指向字符的指针为什么可以用字符串来初始化,而不是字符地址?
    C++(二十三) — 内存泄漏及指针悬挂
    C++(二十二) — 指针变量、函数指针、void指针
    C++(二十一) — 引用概念及本质
  • 原文地址:https://www.cnblogs.com/lienus/p/4244767.html
Copyright © 2020-2023  润新知