BZOJ 3786: 星系探索 欧拉游览树

本文深入探讨了Euler-Tour-Tree数据结构,利用Splay_Tree维护欧拉序,实现高效路径查询与更新操作。通过具体代码解析,展示了如何构建及优化此数据结构,适用于解决树形结构上的动态问题。

一个叫 Euler-Tour-Tree 的数据结构,说白了就是用 Splay_Tree 维护欧拉序

#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>

using namespace std;

void setIO(string a){ 
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

#define maxn 300000
#define ll long long

int euler[maxn], w[maxn], cnt=1;
int head[maxn],to[maxn],nex[maxn],edges,root;
ll sumv[maxn], val[maxn], lazy[maxn];
void addedge(int u,int v){
    nex[++edges]=head[u],head[u]=edges,to[edges]=v;
}

void dfs(int u){
    euler[++cnt]=u*2, val[u*2]=w[u];
    for(int v=head[u];v;v=nex[v])
        dfs(to[v]);
    euler[++cnt]=u*2+1, val[u*2+1]=-w[u];
}

struct Splay_Tree{

    int f[maxn],ch[maxn][2],sta[maxn],siz[maxn];
    int rson(int x)
    {
        return ch[x][1];
    }

    int lson(int x)
    {
        return ch[x][0];
    }

    int get(int x)
    {
        return ch[f[x]][1]==x;
    }

    void update(int x,int c)
    {
        val[x]+=(x%2==0?1:-1)*c;
        lazy[x]+=c;
        sumv[x]+=siz[x]*c;
    }

    void pushup(int x)
    { 
        sumv[x]=sumv[lson(x)]+sumv[rson(x)]+val[x];
        siz[x]=siz[lson(x)]+siz[rson(x)];
        siz[x]+=(x>=200099 ? 0: (x%2==0?1:-1));
    }

    void pushdown(int x)
    {
        if(lazy[x]) update(lson(x),lazy[x]), update(rson(x),lazy[x]),lazy[x]=0;
    }   
    
    int pre(int x)
    {
        splay(x,root);
        x=lson(root);
        while(rson(x)) x=rson(x);
        return x;
    }

    int las(int x)
    {
        splay(x,root);
        x=rson(root);
        while(lson(x)) x=lson(x);
        return x;
    }

    void build(int l,int r,int &o,int fa)
    {
        if(l>r)return;
        int mid=(l+r)>>1;
        o=euler[mid], f[o]=fa;
        build(l,mid-1,ch[o][0],o);
        build(mid+1,r,ch[o][1],o);
        pushup(o);
    }

    void rotate(int x)
    {
        int old=f[x], oldf=f[old], which=get(x);
        ch[old][which]=ch[x][which^1], f[ch[old][which]]=old;
        ch[x][which^1]=old,f[old]=x,f[x]=oldf;
        if(oldf) ch[oldf][ch[oldf][1]==old]=x;
        pushup(old),pushup(x);
    }

    void splay(int x,int &tar)
    {
        int v=0,u=x,a=f[tar];
        while(u!=a) sta[++v]=u,u=f[u];
        while(v) pushdown(sta[v--]);
        for(int fa;(fa=f[x])!=a;rotate(x))
            if(f[fa]!=a) rotate(get(fa)==get(x)?fa:x);
        tar=x;
    }

    void opt1(int a)
    {
        int x=las(a*2);
        splay(200100,root),splay(x,ch[root][1]);
        printf("%lld\n",sumv[ch[ch[root][1]][0]]);
    }

    void opt2(int a,int b)
    {
        int x=pre(a*2),y=las(a*2+1),tmp;
        splay(x,root),splay(y,ch[root][1]);
        tmp=ch[ch[root][1]][0], ch[ch[root][1]][0]=f[tmp]=0;
        pushup(ch[root][1]), pushup(root);

        x=las(b*2);
        splay(b*2,root), splay(x,ch[root][1]);
        ch[ch[root][1]][0]=tmp,f[tmp]=ch[root][1];
        pushup(ch[root][1]),pushup(root);
    }

    void opt3(int a,int b)
    {
        int x=pre(a*2),y=las(a*2+1);
        splay(x,root),splay(y,ch[root][1]);
        update(ch[ch[root][1]][0],b);
        pushup(ch[root][1]),pushup(root);
    }

}tree;

int main(){
    //setIO("input");
    int n,x,m;
    scanf("%d",&n);
    for(int i=2;i<=n;++i)
        scanf("%d",&x),addedge(x,i);
    for(int i=1;i<=n;++i) scanf("%d",&w[i]);

    dfs(1);

    euler[1]=200100, euler[++cnt]=200101;
    tree.build(1,cnt,root,0);

    scanf("%d",&m);

    while(m--)
    {
        char opt[10];
        int a,b;
        scanf("%s",opt);
        if(opt[0]=='Q')
        {
            scanf("%d",&a);
            tree.opt1(a);
        }
        if(opt[0]=='C')
        {
            scanf("%d%d",&a,&b);
            tree.opt2(a,b);
        }
        if(opt[0]=='F')
        {
            scanf("%d%d",&a,&b);
            tree.opt3(a,b);
        }
    }
    return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值