一个叫 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;
}

  

BZOJ 3786: 星系探索 欧拉游览树的更多相关文章

  1. 数据结构&图论:欧拉游览树

    ETT可以称为欧拉游览树,它是一种和欧拉序有关的动态树(LCT是解决动态树问题的一种方案,这是另一种) dfs序和欧拉序是把树问题转化到区间问题上然后再用数据结构去维护的利器 通过借助这两种形式能够完 ...

  2. BZOJ 3786: 星系探索 解题报告

    3786: 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅 ...

  3. BZOJ 3786 星系探索

    Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...

  4. BZOJ 3786: 星系探索 [伪ETT]

    传送门 数据,标程 题意: 一颗有根树,支持询问点到根路径权值和,子树加,换父亲 欧拉序列怎么求路径权值和? 一个点的权值只会给自己的子树中的点贡献,入栈权值正出栈权值负,求前缀和就行了! 和上题一样 ...

  5. bzoj 3786 星系探索 dfs+splay

    [BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...

  6. BZOJ 3786 星系探索 (splay+dfs序)

    题目大意:给你一棵树,支持一下三种操作 1.获取某节点到根节点的路径上所有节点的权值和 2.更换某棵子树的父亲 3.某子树内所有节点的权值都增加一个值w 当时想到了splay维护dfs序,查完题解发现 ...

  7. BZOJ 3786: 星系探索 ETT

    Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...

  8. BZOJ 3786 星系探索 ——Splay

    子树可以移动,唔. 还是用Splay维护DFS序即可. 子树的话直接截取出来就好了. 然后求前驱后继可能麻烦一些. 添加两个虚拟节点会比较好写. #include <map> #inclu ...

  9. BZOJ 2818 Gcd 线性欧拉

    题意:链接 方法:线性欧拉 解析: 首先列一下表达式 gcd(x,y)=z(z是素数而且x,y<=n). 然后我们能够得到什么呢? gcd(x/z,y/z)=1; 最好还是令y>=x 则能 ...

随机推荐

  1. 实现一个类似360的button

    通过改写一个buttonst类,实现360效果的button. 主要可以完成:frame,hover,face效果,并且支持menu,tooltips 1)派生新的类QButton.添加虚函数,设置自 ...

  2. angular4(2-1)angular脚手架引入第三方类库(jquery)

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 如何在angular4脚手架中引入第三方类库呢比如jquery.swiper.bootstrap...... 例如引入j ...

  3. xBIM 实战03 使用WPF技术实现IFC模型的加载与浏览

    系列目录    [已更新最新开发文章,点击查看详细]  WPF应用程序在底层使用 DirectX ,无论设计复杂的3D图形(这是 DirectX 的特长所在)还是绘制简单的按钮与文本,所有绘图工作都是 ...

  4. SPOJ 694/705 后缀数组

    思路: 论文题*n Σn-i-ht[i]+1 就是结果 O(n)搞定~ //By SiriusRen #include <cstdio> #include <cstring> ...

  5. var和let的区别

    //var 和let的区别 通过var定义的变量,作用域是整个封闭的函数,是全域的, 通过let定义的变量,作用域是在块级或者是子块中 for (let i = 0; i < 10; i++) ...

  6. 解决Windows下git需要每次都要ssh-add的问题

    顽皮的很: 不知道怎么回事,每次打开git提交代码都需要ssh-add一下秘钥才可以正常提交: 不然就报错权限之类的问题: 怎么才能更方便一些? 卸了重装!我没试... 再或者是在 git 的安装目录 ...

  7. NodeJS学习笔记 (3)域名解析-dns(ok)

    域名解析:dns.lookup() 比如我们要查询域名 www.qq.com 对应的ip,可以通过 dns.lookup() . var dns = require('dns'); dns.looku ...

  8. while循环,格式化输出%,运算符,数据类型的转换,编码的初识,

    1.内容总览 while循环 格式化输出 运算符 and or not 编码的初识 2. 具体内容 while 循环 where:程序中:你需要重复之前的动作,输入用户名密码时,考虑到while循环. ...

  9. GDOI2018爆炸记

    Day0 12:45p.m. 从初中部出发前回班探望了一下同学,受到热烈欢迎(?) 13:15p.m. 出发去中山,路上本来想用mac看fz的,结果ass字幕导入失败,心态爆炸*1:后来成功获取xfz ...

  10. BZOJ 3325 [SCOI2013]密码 (逆模拟Manacher+构造)

    题目大意:给你一个字符串每个位置和相邻两个位置为回文中心的最长回文串长度,让你构造一个合法的字典序最小的字符串 挺有意思的构造题 首先按照$Manacher$的思想还原$p$数组 定义$f_{ij}$ ...