BZOJ 3786: 星系探索 [伪ETT]
题意:
一颗有根树,支持询问点到根路径权值和,子树加,换父亲
欧拉序列怎么求路径权值和?
一个点的权值只会给自己的子树中的点贡献,入栈权值正出栈权值负,求前缀和就行了!
和上题一样,伪ETT大法好
注意本题的子树需要根,所以需要找到子树区间左右的前驱和后继节点把他们splay出来才能得到子树区间,不能直接$l-1, r+1$,一开始写错了
然后注意下放标记,splay需要记录splay子树里有几个入栈几个出栈
加强版:询问任意一条路径$(u,v)$
当然需要减去lca了,于是用一个LCT维护树的形态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
#define pii pair<int, int>
#define MP make_pair
#define fir first
#define sec second
typedef long long ll;
const int N=2e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n, a[N], x, y, Q, fa[N]; char s[];
struct edge{int v, ne;} e[N];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
}
pii dfn[N]; int dfc, eul[N];
void dfs(int u) {
dfn[u].fir = ++dfc; eul[dfc] = u;
for(int i=h[u]; i; i=e[i].ne) dfs(e[i].v);
dfn[u].sec = ++dfc; eul[dfc] = -u;
} struct meow{int ch[], fa, v, sl, sr, type; ll sum, tag;} t[N];
int root, sz;
inline int wh(int x) {return t[pa].ch[] == x;}
inline void update(int x) {
t[x].sum = t[lc].sum + t[rc].sum + t[x].v;
t[x].sl = t[lc].sl + t[rc].sl + (t[x].type==);
t[x].sr = t[lc].sr + t[rc].sr + (t[x].type==-);
} inline void paint(int x, int d) {
t[x].sum += (ll)d*(t[x].sl - t[x].sr);
t[x].v += d*t[x].type;
t[x].tag += d;
}
inline void pushDown(int x) { //printf("pushDown %d\n",x);
if(t[x].tag) { //puts("yes");
if(lc) paint(lc, t[x].tag);
if(rc) paint(rc, t[x].tag);
t[x].tag = ;
}
}
void pd(int x) { if(pa) pd(pa); pushDown(x); } inline void rotate(int x) {
int f=t[x].fa, g=t[f].fa, c=wh(x);
if(g) t[g].ch[wh(f)] = x; t[x].fa=g;
t[f].ch[c] = t[x].ch[c^]; t[t[f].ch[c]].fa=f;
t[x].ch[c^] = f; t[f].fa=x;
update(f); update(x);
}
inline void splay(int x, int tar) {
pd(x);
for(; pa!=tar; rotate(x))
if(t[pa].fa != tar) rotate(wh(x)==wh(pa) ? pa : x);
if(tar==) root=x;
} void build(int &x, int l, int r, int f) {
int mid = (l+r)>>; x=mid;
t[x].fa=f;
if(eul[x]>) t[x].type = , t[x].v = a[eul[x]];
else t[x].type = -, t[x].v = -a[-eul[x]];
if(l<mid) build(lc, l, mid-, x);
if(mid<r) build(rc, mid+, r, x);
update(x);
} inline int pre(int x) {
x = lc; while(rc) x = rc; return x;
}
inline int nex(int x) {
x = rc; while(lc) x = lc; return x;
}
inline void Split(int &p, int &x) {
splay(p, ); p = pre(p);
splay(x, ); x = nex(x);
splay(p, ); splay(x, p);
} ll Que(int u) {
int p = dfn[].fir, x = dfn[u].fir;
Split(p, x);
return t[lc].sum;
} void Cha(int u, int far) {
int p = dfn[u].fir, x = dfn[u].sec;
Split(p, x);
int q = lc;
lc = t[q].fa = ;
update(x); update(p); p = dfn[far].fir; splay(p, );
x = nex(p); splay(x, p);
lc = q; t[q].fa = x;
update(x); update(p);
} void AddVal(int u, int d) {
int p = dfn[u].fir, x = dfn[u].sec;
Split(p, x);
paint(lc, d);
} int main() {
//freopen("in","r",stdin);
freopen("galaxy.in", "r", stdin);
freopen("galaxy.out", "w", stdout);
n=read();
for(int i=; i<=n; i++) ins(read(), i);
for(int i=; i<=n; i++) a[i]=read();
dfc=; dfs(); dfc++;
build(root, , dfc, );
Q=read();
for(int i=; i<=Q; i++) {
scanf("%s", s); x=read();
if(s[]=='Q') printf("%lld\n", Que(x));
else if(s[]=='C') Cha(x, read());
else AddVal(x, read());
}
}
BZOJ 3786: 星系探索 [伪ETT]的更多相关文章
- [BZOJ3786]星系探索(伪ETT)
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1638 Solved: 506[Submit][Status][Discuss ...
- BZOJ 3786: 星系探索 解题报告
3786: 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅 ...
- BZOJ 3786: 星系探索 ETT
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- BZOJ 3786 星系探索
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- BZOJ 3786 星系探索 ——Splay
子树可以移动,唔. 还是用Splay维护DFS序即可. 子树的话直接截取出来就好了. 然后求前驱后继可能麻烦一些. 添加两个虚拟节点会比较好写. #include <map> #inclu ...
- BZOJ 3786 星系探索 (splay+dfs序)
题目大意:给你一棵树,支持一下三种操作 1.获取某节点到根节点的路径上所有节点的权值和 2.更换某棵子树的父亲 3.某子树内所有节点的权值都增加一个值w 当时想到了splay维护dfs序,查完题解发现 ...
- BZOJ 3786: 星系探索 欧拉游览树
一个叫 Euler-Tour-Tree 的数据结构,说白了就是用 Splay_Tree 维护欧拉序 #include <cstring> #include <algorithm> ...
- 【BZOJ】3786: 星系探索
[题意]给定一棵带点权树,三种操作: 1.询问点x到根的路径和 2.子树x内的点权加定值y 3.将点x的父亲更换为y,保证仍是树. [算法]平衡树(fhq-treap) [题解] 将树的dfs序作为序 ...
随机推荐
- C#的改进特性
1.初始器 当你新建一个对象实例的时候,是否遇到下面这种情况: class a = new class(); a.item1 = "; a.item2 = "; 或者写一个构造函数 ...
- CentOS6下安装git
Ubuntu12.04中默认没有安装Git.需要自行安装. 1. 安装Git 1.1 Ubuntu12.04下 可以使用apt-get方式安装,也可以下载源代码安装[1],我们这里使用apt-git安 ...
- Effective Java 第三版——25. 将源文件限制为单个顶级类
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 番外篇--Moddule Zero介绍
1.1 ABPZero - 概述 介绍 微软ASP.NET身份框架 权限 会话 角色管理 默认角色 用户管理 多租户 设置管理 审计日志 1.1.1 介绍 Modulde Zero实现了ASP.NET ...
- Python3 的函数
1.编写power(x,y)函数返回x的y次幂值 def power(x,y): return x**y 2.求最大公约数 def gcd(x,y): r=x%y x=y y=r if r==0: p ...
- 邓_ ThinkPhp框架
登陆功能是PHP程序设计中常见的功能.本文ThinkPHP实例主要完成注册成功后进入首页,并告诉你是登录用户的功能.具体实现步骤如下: 第一步:在config.php文件中加上: 1 'USER_AU ...
- AVFrame转换到Mat,yuv420p转换到RGB源代码
FFmpeg中AVFrame到OpenCV中Mat的两种转换方法 方法一:查表法 void AVFrame2Img(AVFrame *pFrame, cv::Mat& img) { int f ...
- maven pom.xml 详细
一.什么是POM Project Object Model,项目对象模型.通过xml格式保存的pom.xml文件.作用类似ant的build.xml文件,功能更强大.该文件用于管理:源代码.配置文件. ...
- 高质量JAVA代码编写规范
1. Java 命名约定 除了以下几个特例之外,命名时应始终采用完整的英文描述符.此外,一般应采用小写字母,但类名.接口名以及任何非初始单词的第一个字母要大写. 1.1 一般概念 * 尽量使用完整的英 ...
- MyCat 启蒙:分布式系统的数据库架构演变
文章首发于[博客园-陈树义],点击跳转到原文<MyCat 启蒙:分布式系统的数据库架构演变> 单数据库架构 一个项目在初期的时候,为了尽可能快地验证市场,其对业务系统的最大要求是快速实现. ...