ETT模版题。

真正的Eular-Tour-Tree维护的是树的欧拉序。

由于各种原因,没人知道怎么维护欧拉序,所以我写的是个假的,维护dfs序的。

本质还是用Splay维护序列。

然后因为我常数太差,压着线跑过去的……

#include<bits/stdc++.h>
#define N 100010
using namespace std;
typedef long long ll;
const int M=;
int n,m,a[M];
struct Edge{int u,v,next;}G[M];
int head[M],tot=,tpos[M],rt;
inline void addedge(int u,int v){
G[++tot].u=u;G[tot].v=v;G[tot].next=head[u];head[u]=tot;
G[++tot].u=v;G[tot].v=u;G[tot].next=head[v];head[v]=tot;
}
int size[M],c[M][],val[M],addv[M],fa[M],w[M],s[M],top;
ll sumv[M];
inline void pushup(int x){
size[x]=size[c[x][]]+size[c[x][]]+w[x];
sumv[x]=sumv[c[x][]]+sumv[c[x][]]+w[x]*val[x];
}
inline void puttag(int x,ll v){val[x]+=v;addv[x]+=v;sumv[x]+=v*size[x];}
inline void pushdown(int x){
if(!addv[x])return ;
if(c[x][])puttag(c[x][],addv[x]);
if(c[x][])puttag(c[x][],addv[x]);
addv[x]=;
}
inline void rotate(int x,int &k){
int y=fa[x],z=fa[y],l,r;
if(c[y][]==x)l=;else l=;r=l^;
if(y==k)k=x;else{if(c[z][]==y)c[z][]=x;else c[z][]=x;}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}
inline void splay(int x,int &k){
s[top=]=x;for(int i=x;i!=rt;i=fa[i])s[++top]=fa[i];
for(int i=top;i;--i)pushdown(s[i]);
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k)
if(c[z][]==y^c[y][]==x)rotate(x,k);
else rotate(y,k);
rotate(x,k);
}
}
inline void ins(int x,int v,int ww){
if(!rt)rt=x;else fa[x]=rt,c[rt][]=x;
val[x]=v;w[x]=ww;pushup(x);splay(x,rt);
}
inline int tmax(int x){while(c[x][])x=c[x][];return x;}
inline int tmin(int x){while(c[x][])x=c[x][];return x;}
inline ll query(int x){
splay(x,rt);return sumv[c[x][]]+1LL*w[x]*val[x];
}
inline void change(int x,int y){
splay(x,rt);int t1=tmax(c[rt][]);
splay(x+N,rt);int t2=tmin(c[rt][]);
splay(t1,rt);splay(t2,c[t1][]);
int z=c[t2][];fa[z]=c[t2][]=;
splay(y,rt);int t3=tmin(c[y][]);
splay(t3,c[y][]);fa[z]=t3;c[t3][]=z;
}
inline void add(int x,int v){
splay(x,rt);int t1=tmax(c[rt][]);
splay(x+N,rt);int t2=tmin(c[rt][]);
splay(t1,rt);splay(t2,c[t1][]);
puttag(c[t2][],v);
}
void build(int u,int f){
ins(u+,a[u],);
for(int i=head[u];i;i=G[i].next){
int v=G[i].v;if(v==f)continue;
build(v,u);
}
ins(u+N+,a[u],-);
}
int main(){
n=io.read();
for(int i=;i<=n;i++){int u=io.read();addedge(u,i);}
for(int i=;i<=n;i++)a[i]=io.read();
ins(,,);build(,);ins(N*+,,);
m=io.read();char s[];
while(m--){
scanf("%s",s);
if(s[]=='Q'){int x=read();printf("%lld\n",query(x+));}
if(s[]=='C'){int x=read(),y=read();change(x+,y+);}
if(s[]=='F'){int x=read(),y=read();add(x+,y);}
}
return ;
}

【bzoj3786】星系探索的更多相关文章

  1. [BZOJ3786]星系探索

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

  2. BZOJ3786 星系探索 【Splay维护dfs序】*

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

  3. [BZOJ3786]星系探索(伪ETT)

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1638  Solved: 506[Submit][Status][Discuss ...

  4. [BZOJ3786] 星系探索(括号序列+Splay)

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 2191  Solved: 644[Submit][Status][Discuss ...

  5. bzoj3786星系探索 splay

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1314  Solved: 425[Submit][Status][Discuss ...

  6. BZOJ3786星系探索——非旋转treap(平衡树动态维护dfs序)

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

  7. BZOJ3786:星系探索(Splay,括号序)

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

  8. bzoj3786星系探索(splay维护dfs序)

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

  9. BZOJ3786: 星系探索 Splay+DFS序

    题目大意:给你一个树,支持三种操作,子树加,点到根的路径和,改变某一个点的父亲. 分析: 看起来像一个大LCT,但是很显然,LCT做子树加我不太会啊... 那么,考虑更换一个点的父亲这个操作很有意思, ...

  10. BZOJ3786: 星系探索(伪ETT)

    题面 传送门 题解 坑啊--我好像把\(Splay\)的东西全忘光了-- \(ETT\)(\(Euler\ Tour\ Tree\))是一种可以资瓷比\(LCT\)更多功能的数据结构,然而不管是功能还 ...

随机推荐

  1. 通过logger命令记录日志

    通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...

  2. ural1297 求最长回文子串 | 后缀数组

    #include<cstdio> #include<algorithm> #include<cstring> #define N 20005 using names ...

  3. Python3简单入门

    在Mac和Linux上运行Python时,请打开终端,然后运行python3 Mac OSX 正确地同时安装Python 2.7 和Python3:  http://www.jianshu.com/p ...

  4. [IOI2007]Miners 矿工配餐

    link 其实就是一个比较简单的$IOI$题.简单$dp$就行,设$5$维$dp$即可 最后在滚动一下,判一下可行性即可. #include<iostream> #include<c ...

  5. Jokewithpermutation (DFS)

    Problem J. Jokewithpermutation Input file: joke.inOutput file: joke.out Joey had saved a permutation ...

  6. bzoj 1106 [POI2007]立方体大作战tet 树状数组优化

    [POI2007]立方体大作战tet Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 821  Solved: 601[Submit][Status][ ...

  7. 在Linux防火墙上过滤外来的ICMP timestamp

    ICMP timestamp请求响应漏洞 解决方案:  * 在您的防火墙上过滤外来的ICMP timestamp(类型13)报文以及外出的ICMP timestamp回复报文.     具体解决方式就 ...

  8. javascript如何判断对象为空

    1.自定义jQuery的isEmptyObject()方法. function isEmptyObject(e) { var t; for (t in e) return !1; return !0 ...

  9. mysql 联合索引匹配原则

    读mysql文档有感 看了mysql关于索引的文档,网上有一些错误的博客文档,这里我自己记一下. 几个重要的概念 1.对于mysql来说,一条sql中,一个表无论其蕴含的索引有多少,但是有且只用一条. ...

  10. 【算法日记】2.算法中的大O符号

    大O符号是一种算法复杂度的相对表示方式. 1.大O表示算法的操作数,表示出算法运行的快慢 2.大O表示法指出了最糟糕情况下的运行时间,例如 简单查找的运行时间O(n),意味着在最糟糕的情况下,必须运行 ...