BZOJ 3786: 星系探索 ETT
Description
物理学家小C的研究正遇到某个瓶颈。
他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球。主星球没有依赖星球。
我们定义依赖关系如下:若星球a的依赖星球是b,则有星球a依赖星球b.此外,依赖关系具有传递性,即若星球a依赖星球b,星球b依赖星球c,则有星球a依赖星球c.
对于这个神秘的星系中,小C初步探究了它的性质,发现星球之间的依赖关系是无环的。并且从星球a出发只能直接到达它的依赖星球b.
每个星球i都有一个能量系数wi.小C想进行若干次实验,第i次实验,他将从飞船上向星球di发射一个初始能量为0的能量收集器,能量收集器会从星球di开始前往主星球,并收集沿途每个星球的部分能量,收集能量的多少等于这个星球的能量系数。
但是星系的构成并不是一成不变的,某些时刻,星系可能由于某些复杂的原因发生变化。
有些时刻,某个星球能量激发,将使得所有依赖于它的星球以及他自己的能量系数均增加一个定值。还有可能在某些时刻,某个星球的依赖星球会发生变化,但变化后依然满足依赖关系是无环的。
现在小C已经测定了时刻0时每个星球的能量系数,以及每个星球(除了主星球之外)的依赖星球。接下来的m个时刻,每个时刻都会发生一些事件。其中小C可能会进行若干次实验,对于他的每一次实验,请你告诉他这一次实验能量收集器的最终能量是多少。
Input
第一行一个整数n,表示星系的星球数。
接下来n-1行每行一个整数,分别表示星球2-n的依赖星球编号。
接下来一行n个整数,表示每个星球在时刻0时的初始能量系数wi.
接下来一行一个整数m,表示事件的总数。
事件分为以下三种类型。
(1)"Q di"表示小C要开始一次实验,收集器的初始位置在星球di.
(2)"C xi yi"表示星球xi的依赖星球变为了星球yi.
(3)"F pi qi"表示星球pi能量激发,常数为qi.
Output
对于每一个事件类型为Q的事件,输出一行一个整数,表示此次实验的收集器最终能量。
这个应该是伪 $ETT$,不知道真正的 $ETT$ 该咋写.
开始卡了半天常数,本地 $25s$,交上去却一顿 T,后来改用结构体存变量就跑的飞快.
以后写数据结构就都用结构体存了
#include <cstdio>
#include <algorithm> E
#define N 200005
#define ll long long
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define setIO(s) freopen(s".in", "r", stdin)
using namespace std;
int edges, tim, root = 0, top;
int sta[N], hd[N], to[N], nex[N], L[N], R[N], euler[N], val[N];
inline void addedge(int u, int v)
{
nex[++edges] = hd[u], hd[u] = edges, to[edges] = v;
}
struct Stack
{
int x, i;
Stack(int x = 0, int i = 0) : x(x), i(i) {}
}A[N];
struct Node
{
int siz, d, ch[2], f;
ll sum, val, lazy;
}t[N];
inline int get(int x)
{
return t[t[x].f].ch[1] == x;
}
inline void pushup(int x)
{
t[x].siz = t[lson].siz + t[rson].siz + t[x].d;
t[x].sum = t[lson].sum + t[rson].sum + t[x].val;
}
inline void mark(int x, ll v)
{
t[x].val += 1ll * t[x].d * v;
t[x].sum += 1ll * t[x].siz * v;
t[x].lazy += v;
}
inline void pushdown(int x)
{
if(t[x].lazy)
{
if(lson) mark(lson, t[x].lazy);
if(rson) mark(rson, t[x].lazy);
t[x].lazy = 0;
}
}
inline void rotate(int x)
{
int old = t[x].f, fold = t[old].f, which = get(x);
t[old].ch[which] = t[x].ch[which ^ 1], t[t[old].ch[which]].f = old;
t[x].ch[which ^ 1] = old, t[old].f = x, t[x].f = fold;
if(fold) t[fold].ch[t[fold].ch[1] == old] = x;
pushup(old), pushup(x);
}
inline void splay(int x, int &tar)
{
int u = t[tar].f, v = 0;
for(int g = x ; g ; sta[++v] = g, g = t[g].f);
for(int i = v ; i >= 1; --i) pushdown(sta[i]);
for(int fa; (fa = t[x].f) ^ u; rotate(x))
if(t[fa].f ^ u)
rotate(get(fa) == get(x) ? fa: x);
tar = x;
}
void solve()
{
top = 0;
A[++ top] = Stack(1, hd[1]), L[1] = ++tim, t[tim].d = 1, t[tim].val = (ll) val[1];
for( ; top ; )
{
Stack u = A[top];
if(u.i)
{
A[top].i = nex[A[top].i];
A[++ top] = Stack(to[u.i], hd[to[u.i]]);
L[to[u.i]] = ++tim;
t[tim].d = 1, t[tim].val = (ll) val[to[u.i]];
}
else
{
R[u.x] = ++tim;
t[tim].d = -1, t[tim].val = (ll) -val[u.x];
-- top;
}
}
}
int build(int l, int r, int ff)
{
int mid = (l + r) >> 1;
t[mid].f = ff;
if(mid > l) t[mid].ch[0] = build(l, mid - 1, mid);
if(r > mid) t[mid].ch[1] = build(mid + 1, r, mid);
if(mid == 1) t[mid].ch[0] = tim + 1, t[tim + 1].f = mid;
if(mid == tim) t[mid].ch[1] = tim + 2, t[tim + 2].f = mid;
pushup(mid);
return mid;
}
int pre(int x)
{
int g = root;
splay(x, root);
for(x = lson; rson ; pushdown(x), x = rson);
splay(g, root);
return x;
}
int nxt(int x)
{
int g = root;
splay(x, root);
for(x = rson; lson ; pushdown(x), x = lson);
splay(g, root);
return x;
}
int main()
{
// setIO("input");
int n, m, i, j, x, y;
scanf("%d", &n);
for(i = 2; i <= n ; ++i) scanf("%d", &x), addedge(x, i);
for(i = 1; i <= n ; ++i) scanf("%d", &val[i]);
solve(), root = build(1, tim , 0);
scanf("%d", &m);
for(int cas = 1; cas <= m; ++cas)
{
char str[4];
scanf("%s", str);
if(str[0] == 'Q')
{
scanf("%d", &x), splay(L[x], root), printf("%lld\n", t[root].sum - t[t[root].ch[1]].sum);
}
if(str[0] == 'C')
{
scanf("%d%d", &x, &y);
int l = pre(L[x]), r = nxt(R[x]), key, k;
splay(l, root), splay(r, t[root].ch[1]), key = t[t[root].ch[1]].ch[0];
t[key].f = t[t[root].ch[1]].ch[0] = 0, pushup(t[root].ch[1]), pushup(root);
splay(L[y], root), splay(nxt(L[y]), t[root].ch[1]);
t[t[root].ch[1]].ch[0] = key, t[key].f = t[root].ch[1], pushup(t[root].ch[1]), pushup(root);
}
if(str[0] == 'F')
{
scanf("%d%d", &x, &y);
splay(pre(L[x]), root), splay(nxt(R[x]), t[root].ch[1]);
int key = t[t[root].ch[1]].ch[0];
mark(key, 1ll * y);
}
}
return 0;
}
递归:
#include <cstdio>
#include <algorithm>
#define N 200005
#define ll long long
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define setIO(s) freopen(s".in", "r", stdin)
using namespace std;
int edges, tim, root = 0;
int sta[N], hd[N], to[N], nex[N], L[N], R[N], euler[N], val[N];
inline void addedge(int u, int v)
{
nex[++edges] = hd[u], hd[u] = edges, to[edges] = v;
}
struct Node
{
int siz, d, ch[2], f;
ll sum, val, lazy;
}t[N];
inline int get(int x)
{
return t[t[x].f].ch[1] == x;
}
inline void pushup(int x)
{
t[x].siz = t[lson].siz + t[rson].siz + t[x].d;
t[x].sum = t[lson].sum + t[rson].sum + t[x].val;
}
inline void mark(int x, ll v)
{
t[x].val += 1ll * t[x].d * v;
t[x].sum += 1ll * t[x].siz * v;
t[x].lazy += v;
}
inline void pushdown(int x)
{
if(t[x].lazy)
{
if(lson) mark(lson, t[x].lazy);
if(rson) mark(rson, t[x].lazy);
t[x].lazy = 0;
}
}
inline void rotate(int x)
{
int old = t[x].f, fold = t[old].f, which = get(x);
t[old].ch[which] = t[x].ch[which ^ 1], t[t[old].ch[which]].f = old;
t[x].ch[which ^ 1] = old, t[old].f = x, t[x].f = fold;
if(fold) t[fold].ch[t[fold].ch[1] == old] = x;
pushup(old), pushup(x);
}
inline void splay(int x, int &tar)
{
int u = t[tar].f, v = 0;
for(int g = x ; g ; sta[++v] = g, g = t[g].f);
for(int i = v ; i >= 1; --i) pushdown(sta[i]);
for(int fa; (fa = t[x].f) ^ u; rotate(x))
if(t[fa].f ^ u)
rotate(get(fa) == get(x) ? fa: x);
tar = x;
}
void dfs(int u)
{
L[u] = ++tim;
t[tim].d = 1, t[tim].val = (ll)val[u];
for(int i = hd[u] ; i ; i = nex[i]) dfs(to[i]);
R[u] = ++tim;
t[tim].d = -1, t[tim].val = (ll)-val[u];
}
int build(int l, int r, int ff)
{
int mid = (l + r) >> 1;
t[mid].f = ff;
if(mid > l) t[mid].ch[0] = build(l, mid - 1, mid);
if(r > mid) t[mid].ch[1] = build(mid + 1, r, mid);
if(mid == 1) t[mid].ch[0] = tim + 1, t[tim + 1].f = mid;
if(mid == tim) t[mid].ch[1] = tim + 2, t[tim + 2].f = mid;
pushup(mid);
// printf("%d %d %d\n",mid, t[mid].ch[0], t[mid].ch[1]);
return mid;
}
int pre(int x)
{
int g = root;
splay(x, root);
for(x = lson; rson ; pushdown(x), x = rson);
splay(g, root);
return x;
}
int nxt(int x)
{
int g = root;
splay(x, root);
for(x = rson; lson ; pushdown(x), x = lson);
splay(g, root);
return x;
}
int main()
{
// setIO("input");
int n, m, i, j, x, y;
scanf("%d", &n);
for(i = 2; i <= n ; ++i) scanf("%d", &x), addedge(x, i);
for(i = 1; i <= n ; ++i) scanf("%d", &val[i]);
dfs(1), root = build(1, tim , 0);
scanf("%d", &m);
for(int cas = 1; cas <= m; ++cas)
{
char str[4];
scanf("%s", str);
if(str[0] == 'Q')
{
scanf("%d", &x), splay(L[x], root), printf("%lld\n", t[root].sum - t[t[root].ch[1]].sum);
}
if(str[0] == 'C')
{
scanf("%d%d", &x, &y);
int l = pre(L[x]), r = nxt(R[x]), key, k;
splay(l, root), splay(r, t[root].ch[1]), key = t[t[root].ch[1]].ch[0];
t[key].f = t[t[root].ch[1]].ch[0] = 0, pushup(t[root].ch[1]), pushup(root);
splay(L[y], root), splay(nxt(L[y]), t[root].ch[1]);
t[t[root].ch[1]].ch[0] = key, t[key].f = t[root].ch[1], pushup(t[root].ch[1]), pushup(root);
}
if(str[0] == 'F')
{
scanf("%d%d", &x, &y);
splay(pre(L[x]), root), splay(nxt(R[x]), t[root].ch[1]);
int key = t[t[root].ch[1]].ch[0];
mark(key, 1ll * y);
}
}
return 0;
}
BZOJ 3786: 星系探索 ETT的更多相关文章
- BZOJ 3786: 星系探索 解题报告
3786: 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅 ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- BZOJ 3786: 星系探索 [伪ETT]
传送门 数据,标程 题意: 一颗有根树,支持询问点到根路径权值和,子树加,换父亲 欧拉序列怎么求路径权值和? 一个点的权值只会给自己的子树中的点贡献,入栈权值正出栈权值负,求前缀和就行了! 和上题一样 ...
- 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序作为序 ...
- [BZOJ3786]星系探索(伪ETT)
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1638 Solved: 506[Submit][Status][Discuss ...
随机推荐
- HTML协义代码
这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息. 400- ...
- MSF魔鬼训练营-3.1.1信息收集-通过DNS和IP地址挖掘目标网络信息
情报搜集环境站渗透测试全过程的80%~90% 一.外围信息搜集(公开渠道信息搜集OSINT open source intelligence) 3.1.1信息收集-通过DNS和IP地址挖掘目标网 ...
- 2019JAVA第五次实验报告
Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间2019/10/11 评分等级 实验四 类的继承 实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. ...
- CentOS安装Netdata进行系统监控
偶然间在知乎看到了Netdata这个东西 看到它华丽的界面 顿时心动 gkd gkd #准备 yum install autoconf automake curl gcc git libmnl-dev ...
- ORA-01406:提取的列值被截断 ; SQL Server :将截断字符串或二进制数据
oracle 数据库可以正常连接,表数据也可以正常读取, 但在程序中相同的位置,有时会报错,有时不会报错,有的电脑会报错,有的不会 报错内容为 ORA-01406:提取的列值被截断 查了网上提供的一些 ...
- 细说vue axios登录请求拦截器
当我们在做接口请求时,比如判断登录超时时候,通常是接口返回一个特定的错误码,那如果我们每个接口都去判断一个耗时耗力,这个时候我们可以用拦截器去进行统一的http请求拦截. 1.安装配置axios cn ...
- Vue进行路由跳转的几种方式
1.<router-link to="需要跳转到页面的路径"> 2.this.$router.push()跳转到指定的url,并在history中添加记录,点击回退返回 ...
- C++ 程序设计语言
好记性不如烂笔头. 第六章 标准库给出了静态断言,形式类似如下: stastic_assert(A,S);//当A不为true时,把S作为一条编译器错误信息输出 其最重要的用途是为泛型编程中作为形参的 ...
- nginx的rewrite
nginx服务的rewrite nginx后端服务的指令 1)upstream指令 :设置后端服务器组的主要指令 Upstream name {} 2)server指令:用于设定组内的服务器 3)Ip ...
- Librepilot-Windows编译环境的搭建
1.安装Msys2 下载Msys2,下载地址 https://msys2.github.io,注意根据笔记本的型号选择32bit或64bit. 2.添加LibrePilot MinGW库 在 /e ...