【bzoj3786】星系探索
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】星系探索的更多相关文章
- [BZOJ3786]星系探索
[BZOJ3786]星系探索 试题描述 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个 ...
- BZOJ3786 星系探索 【Splay维护dfs序】*
BZOJ3786 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均 ...
- [BZOJ3786]星系探索(伪ETT)
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1638 Solved: 506[Submit][Status][Discuss ...
- [BZOJ3786] 星系探索(括号序列+Splay)
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 2191 Solved: 644[Submit][Status][Discuss ...
- bzoj3786星系探索 splay
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1314 Solved: 425[Submit][Status][Discuss ...
- BZOJ3786星系探索——非旋转treap(平衡树动态维护dfs序)
题目描述 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球没有依赖星球. ...
- BZOJ3786:星系探索(Splay,括号序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- bzoj3786星系探索(splay维护dfs序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- BZOJ3786: 星系探索 Splay+DFS序
题目大意:给你一个树,支持三种操作,子树加,点到根的路径和,改变某一个点的父亲. 分析: 看起来像一个大LCT,但是很显然,LCT做子树加我不太会啊... 那么,考虑更换一个点的父亲这个操作很有意思, ...
- BZOJ3786: 星系探索(伪ETT)
题面 传送门 题解 坑啊--我好像把\(Splay\)的东西全忘光了-- \(ETT\)(\(Euler\ Tour\ Tree\))是一种可以资瓷比\(LCT\)更多功能的数据结构,然而不管是功能还 ...
随机推荐
- 【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心
题目描述 Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, a ...
- 在Linux上编译TCMalloc
TCMalloc(Thread-Caching Malloc)与标准glibc库的malloc实现一样的功能,但是TCMalloc在效率和速度效率都比标准malloc高很多.TCMalloc是goog ...
- (六)Redis有序集合Sorted set操作
Sorted set全部命令如下: zadd key score1 member1 score2 member2 ... # 将一个或多个member元素及其score值加入到有序集合key当中 z ...
- [SOJ #48]集合对称差卷积
题目大意:给你两个多项式$A,B$,求多项式$C$使得: $$C_n=\sum\limits_{x\oplus y=n}A_xB_y$$题解:$FWT$ 卡点:无 C++ Code: #include ...
- BZOJ2820:YY的GCD——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2820 Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- BZOJ2823 [AHOI2012]信号塔 【最小圆覆盖】
题目链接 BZOJ2823 题解 最小圆覆盖模板 都懒得再写一次 #include<iostream> #include<cstdio> #include<cmath&g ...
- [辅助软件] 微信小程序开发资源汇总 接入指南
https://github.com/justjavac/awesome-wechat-weapp https://github.com/justjavac/awesome-wechat-weapp ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
- 【Android开发】范例1-绘制Android的机器人
下面这个实例通过前面学过的Paint.Canvas等2D绘画技术来实现在手机屏幕上绘制Android机器人的小实例. 具体代码实现和效果: 用来显示自定义的绘图类的布局文件 res/layout/ma ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...