BZOJ 3786 星系探索 ——Splay
子树可以移动,唔。
还是用Splay维护DFS序即可。
子树的话直接截取出来就好了。
然后求前驱后继可能麻烦一些。
添加两个虚拟节点会比较好写。
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define mp make_pair
#define maxn 300005 int n,fa[maxn],st[maxn],top=0,id[maxn],rt,m;
int pos[maxn][2],w[maxn],ch[maxn][2],pn[maxn],siz[maxn][2];
int sta[maxn],cnt=0;
ll val[maxn],sum[maxn],tag[maxn];
char opt[11]; vector <int> v[maxn]; void update(int x)
{
sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
siz[x][0]=siz[ch[x][0]][0]+siz[ch[x][1]][0];
siz[x][1]=siz[ch[x][0]][1]+siz[ch[x][1]][1];
if (pn[x]==-1) siz[x][0]++;
else if (pn[x]==1) siz[x][1]++;
} int build(int l,int r,int f)
{
if (l>r) return 0;
int mid=(l+r)/2;
fa[mid]=f;
if (st[mid]<0)
{
pos[-st[mid]][1]=mid;
val[mid]=-w[-st[mid]];
pn[mid]=-1;
}
else if (st[mid]>0)
{
pos[st[mid]][0]=mid;
val[mid]=w[st[mid]];
pn[mid]=1;
}
else if (st[mid]==0)
{
pos[st[mid]][0]=mid;
val[mid]=0;
pn[mid]=0;
}
ch[mid][0]=build(l,mid-1,mid);
ch[mid][1]=build(mid+1,r,mid);
update(mid);
return mid;
} void dfs(int o)
{
st[++top]=o;
for (int i=0;i<v[o].size();++i) dfs(v[o][i]);
st[++top]=-o;
} void Debug(int o)
{
if (!o) return ;
printf("|-----------------| \n");
printf("Node %d : \n",o);
printf("Fa %d Ch l - %d Ch r - %d\n",fa[o],ch[o][0],ch[o][1]);
printf("Val %lld Sum %lld Positive Negetive %d\n",val[o],sum[o],pn[o]);
Debug(ch[o][0]);
Debug(ch[o][1]);
} void pushdown(int x)
{
if (tag[x])
{
tag[ch[x][0]]+=tag[x];
tag[ch[x][1]]+=tag[x];
val[ch[x][0]]+=pn[ch[x][0]]*tag[x];
val[ch[x][1]]+=pn[ch[x][1]]*tag[x];
sum[ch[x][0]]+=1LL*(siz[ch[x][0]][1]-siz[ch[x][0]][0])*tag[x];
sum[ch[x][1]]+=1LL*(siz[ch[x][1]][1]-siz[ch[x][1]][0])*tag[x];
tag[x]=0;
}
} void rot(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1; r=l^1;
if (y==k) k=x;
else
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x;fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y);update(x);
} void splay(int x,int &k)
{
sta[cnt=1]=x;
for (int i=x;i!=k;i=fa[i]) sta[++cnt]=fa[i];
while (cnt) pushdown(sta[cnt--]); while(x!=k)
{
int y=fa[x];
if (y!=k)
{
int z=fa[y];
if (ch[z][0]==y^ch[y][0]==x) rot(x,k);
else rot(y,k);
}
rot(x,k);
}
} int pre(int x)
{
if (ch[x][0])
{
x=ch[x][0];
while (ch[x][1]) x=ch[x][1];
return x;
}
while (ch[fa[x]][0]==x) x=fa[x]; x=fa[x];
return x;
} int nxt(int x)
{
if (ch[x][1])
{
x=ch[x][1];
while (ch[x][0]) x=ch[x][0];
return x;
}
while (ch[fa[x]][1]==x) x=fa[x]; x=fa[x];
return x;
} int main()
{
scanf("%d",&n);
F(i,2,n)
{
int u; scanf("%d",&u);
v[u].push_back(i);
}
sta[++top]=0;dfs(1);sta[++top]=0;
F(i,1,n) scanf("%d",&w[i]);
rt=build(1,top,0);
scanf("%d",&m);
F(i,1,m)
{
scanf("%s",opt);
int x,y,tmp,L,R;
switch(opt[0])
{
case 'Q':
scanf("%d",&x);
splay(pre(pos[1][0]),rt);
splay(nxt(pos[x][0]),ch[rt][1]);
printf("%lld\n",sum[ch[ch[rt][1]][0]]);
break;
case 'C':
scanf("%d%d",&x,&y);
splay(pre(pos[x][0]),rt);
splay(nxt(pos[x][1]),ch[rt][1]);
tmp=ch[ch[rt][1]][0];
fa[tmp]=0; ch[ch[rt][1]][0]=0;
update(ch[rt][1]); update(rt);
L=pos[y][0];R=pos[y][0]+1;
splay(pos[y][0],rt);
splay(nxt(pos[y][0]),ch[rt][1]);
ch[ch[rt][1]][0]=tmp;
fa[tmp]=ch[rt][1];
update(ch[rt][1]); update(rt);
break;
case 'F':
scanf("%d%d",&x,&y);
splay(pre(pos[x][0]),rt);
splay(nxt(pos[x][1]),ch[rt][1]);
tag[ch[ch[rt][1]][0]]+=y; tmp=ch[ch[rt][1]][0];
val[tmp]+=y*pn[tmp];
sum[tmp]+=1LL*(siz[tmp][1]-siz[tmp][0])*y;
break;
}
}
}
BZOJ 3786 星系探索 ——Splay的更多相关文章
- BZOJ 3786 星系探索 (splay+dfs序)
题目大意:给你一棵树,支持一下三种操作 1.获取某节点到根节点的路径上所有节点的权值和 2.更换某棵子树的父亲 3.某子树内所有节点的权值都增加一个值w 当时想到了splay维护dfs序,查完题解发现 ...
- BZOJ 3786: 星系探索 解题报告
3786: 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅 ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- BZOJ 3786 星系探索
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- BZOJ 3786: 星系探索 ETT
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- BZOJ 3786: 星系探索 [伪ETT]
传送门 数据,标程 题意: 一颗有根树,支持询问点到根路径权值和,子树加,换父亲 欧拉序列怎么求路径权值和? 一个点的权值只会给自己的子树中的点贡献,入栈权值正出栈权值负,求前缀和就行了! 和上题一样 ...
- BZOJ 3786: 星系探索 欧拉游览树
一个叫 Euler-Tour-Tree 的数据结构,说白了就是用 Splay_Tree 维护欧拉序 #include <cstring> #include <algorithm> ...
- 【BZOJ-3786】星系探索 Splay + DFS序
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 647 Solved: 212[Submit][Status][Discuss] ...
- bzoj3786星系探索 splay
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1314 Solved: 425[Submit][Status][Discuss ...
随机推荐
- 融云SDK触达用户数破20亿 王者风范双倍展现
11月1日,融云SDK触达用户数突破20亿,业务增长速度及用户覆盖量再创即时通讯云领域新高.自去年11月10日公布SDK触达用户数破10亿以来,融云仅用了一年时间,便取得了触达用户数翻倍的成绩,迅猛的 ...
- jquery绑定事件的系统参数传递方法
如果是传递的事件自带函数,,可使用以下语法(以鼠标移动事件为例): init: function () { $(document).on("mousemove",loginOper ...
- WCFSVC文件的分离
项目结构图如下: 新建一个实现内容和接口的项目: 接口内部如下: using DataModel; using System; using System.Collections.Generic; us ...
- UVA439 knightMoves (A*启发搜索)
第一个A*,纪念下. A*要保证最短路一定要估价函数小于等于实际值,越接近越好 估价函数取Manhattan距离除以二. //Rey #include<cstdio> #include&l ...
- 使用Timer组件实现人物动画效果
实现效果: 知识运用: Graphics类的DrawImage方法 //在指定位置 按原始大小绘制指定的Image对象 public void DrawImage(Image image,Point ...
- MySQL使用INSERT插入多条记录
MySQL使用INSERT插入多条记录,应该如何操作呢?下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法,供您参考. 看到这个标题也许大家会问,这有什么好说的,调用多次INSERT语 ...
- RabbitMQ Server的安装、配置及常用命令
首先需要安装Erlang环境: http://www.rabbitmq.com/server.html 下载RabbitMQ Server的windows安装包并安装到D盘下: http://www. ...
- C#4.0中的dynamic关键字和ExpandoObject对象
dynamic最大的特点我想莫过于在它的类型在运行时才确定,这也是它与往静态类型关键字的最大区别.如果你在你的代码操作中用到了dynamic关键字去定义一个变量时,那么这个变量在编译的时候编译器不会对 ...
- iOS 骰子战争 Dice Wars
占坑 这个游戏之前在网页端玩过,App Store 上没有搜到特别好的,想自己做个类似的iOS APP 游戏 目测实现难度适中,可玩性较高
- js函数节流和函数防抖
概念解释 函数节流: 频繁触发,但只在特定的时间内才执行一次代码 函数防抖: 频繁触发,但只在特定的时间内没有触发执行条件才执行一次代码 函数节流 函数节流应用的实际场景,多数在监听页面元素滚动事件的 ...