[HNOI2010]弹飞绵羊 (平衡树,LCT动态树)
题面

题解
因为每个点都只能向后跳到一个唯一的点,但可能不止一个点能跳到后面的某个相同的点,
所以我们把它抽象成一个森林。(思考:为什么是森林而不是树?)
子节点可以跳到父节点,根节点再跳就跳飞了。
由于我们发现有一些父子关系要变,所以不能用树链剖分等静态的数据结构,可以用LCT(Link-Cut-Tree 联-切-树,即动态树,支持动态修改父子关系)。
但是当我们询问答案的时候,我们发现wa了,那是因为我们询问的是x点到根的路径上的点数,但是各种LCT操作已经把原先的根不知换到那里去了,所以整个过程中,我们要维护树的根不变。
这个其实很简单,每次进行涉及换根操作时,我们都把原先的根存一下(Findroot()/Find()),操作完后,再把根换回去(Makeroot()),全过程中,当发现x + kx大于n时,就Makeroot(x)。
(由于是道数据结构题,题解写得真的不是很长,请尽量理解吧)
CODE
LCT建议压行,不仅好改而且好看。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#include<map>
#include<stack>
#define LL long long
#define MAXN 2000050
#define DB double
#define lowbit(x) ((-x & x))
#define rg register
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0';s = getchar();}
return x * f;
}
int n;
int m,i,j,s,o,k;
//--------------------------key numbers-------------------------------
struct tr{
int s[2],fa;
int siz;
int nm,as;
int lzn,lzr;// lzn其实不需要,只是懒得删
tr(){s[0] = s[1] = 0;nm = 0;siz = 0;lzn = lzr = 0;as = 0;}
}tre[MAXN];int cnt_splay = 0,st[MAXN],sttop = 0;
inline int newnode(int nm) {
int ct = ++cnt_splay;
tre[ct] = tr();
tre[ct].nm = nm;
tre[ct].as = nm;
tre[ct].siz = 1;
return ct;
}
inline bool isroot(int x) {
if(tre[tre[x].fa].s[1] == x || tre[tre[x].fa].s[0] == x) return 0;
return 1;
}
inline void update(int x) {
int l=tre[x].s[0],r = tre[x].s[1];tre[0] = tr();
tre[x].as = tre[x].nm + tre[l].as + tre[r].as;
tre[x].siz = tre[l].siz + tre[r].siz + 1;
tre[l].fa = x;tre[r].fa = x;tre[0] = tr();
}
inline void changer(int x) {swap(tre[x].s[0],tre[x].s[1]); tre[x].lzr ^= 1;}
inline void pushdown(int x) {
if(!x) return ;
if(tre[x].lzr) {
changer(tre[x].s[0]);
changer(tre[x].s[1]);
tre[x].lzr = 0;
}return ;
}
inline void pushup(int x) {
sttop = 0;st[++sttop] = x;
for(int f = x;!isroot(f); f = tre[f].fa) st[++sttop] = tre[f].fa;
while(sttop) pushdown(st[sttop --]);
}
inline void rotate(int x) {
int y = tre[x].fa,z = tre[y].fa;
int d = (tre[y].s[1] == x);
if(!isroot(y)) tre[z].s[tre[z].s[1] == y] = x;
tre[x].fa = z;
tre[y].s[d] = tre[x].s[d^1];
if(tre[y].s[d]) tre[tre[y].s[d]].fa = y;
tre[x].s[d^1] = y;
tre[y].fa = x;
update(y);update(x);
}
inline void splay(int x) {
pushup(x);
while(!isroot(x)) {
int y = tre[x].fa,z = tre[y].fa;
if(!isroot(y)) {
if((tre[y].s[1] == x) ^ (tre[z].s[1] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
update(x); return ;
}
inline void wash() {splay(rand() % cnt_splay + 1);}
//--------------------------splay-------------------------------------
inline void Access(int x) {
for(int pre = 0; x; pre = x,x = tre[x].fa) {
splay(x);
tre[x].s[1] = pre;
update(x);
}return ;
}
inline void Maketop(int x) {Access(x);splay(x);}
inline void Makeroot(int x) {Access(x); splay(x); changer(x);}
inline int Findroot(int x) {for(Maketop(x); tre[x].s[0]; x = tre[x].s[0]); splay(x); return x;}
inline void Change(int x,int nm) {Maketop(x); tre[x].nm = nm; update(x);}
//##### no Makeroot() inside
inline void Link(int x,int y) {Makeroot(x); tre[x].fa = y;}
inline void Cut(int x,int y) {Makeroot(x); Maketop(y); tre[y].s[0] = tre[x].fa = 0; update(y); pushup(x);}
inline int Getline(int x,int y) {Makeroot(x); Maketop(y); update(y); return y;}
inline bool Haveedge(int x,int y) {Makeroot(x); Maketop(y); return tre[x].fa == y;}
//-------------------------Link Cut Tree------------------------------
char ss[20];
int ki[MAXN];
int main() {
n = read();
cnt_splay = 0;
for(int i = 1;i <= n+3;i ++) {
newnode(1);
}
for(int i = 1;i <= n;i ++) {
ki[i] = read();
if(i + ki[i] <= n) Link(i,i + ki[i]);
else Makeroot(i);
}
m = read();
while(m --) {
k = read();
if(k == 1) {
s = read() + 1;
Access(s);
splay(s);
printf("%d\n",tre[s].as);
}
else if(k == 2) {
s = read() + 1;o = read();
if(s + ki[s] <= n) {
int v = s + ki[s];
int rt = Findroot(v);
Cut(s,v);
Makeroot(rt);
}
ki[s] = o;
if(s + ki[s] > n) Makeroot(s);
else {
int rt = Findroot(s + ki[s]);
Link(s,s + ki[s]);
Makeroot(rt);
}
}
}
return 0;
}
[HNOI2010]弹飞绵羊 (平衡树,LCT动态树)的更多相关文章
- P3203 [HNOI2010]弹飞绵羊(LCT)
P3203 [HNOI2010]弹飞绵羊 LCT板子 用一个$p[i]$数组维护每个点指向的下个点. 每次修改时cut*1+link*1就解决了 被弹出界时新设一个点,权为0,作为终点表示出界点.其他 ...
- BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 13768 Solved: 6989[Subm ...
- 洛谷P3203 [HNOI2010]弹飞绵羊(LCT,Splay)
洛谷题目传送门 关于LCT的问题详见我的LCT总结 思路分析 首先分析一下题意.对于每个弹力装置,有且仅有一个位置可以弹到.把这样的一种关系可以视作边. 然后,每个装置一定会往后弹,这不就代表不存在环 ...
- 2002. [HNOI2010]弹飞绵羊【LCT】
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- 【洛谷 P3203】 [HNOI2010]弹飞绵羊(LCT)
题目链接 把每个点和能跳到的点连边,于是就构成了一个森林. 查询操作就是该点到根的路径长度,修改操作就相当于删边再重新连边. 显然是\(LCT\)的强项. 查询时\(access(x),splay(x ...
- [Luogu P3203] [HNOI2010]弹飞绵羊 (LCT维护链的长度)
题面 传送门:洛谷 Solution 这题其实是有类似模型的. 我们先考虑不修改怎么写.考虑这样做:每个点向它跳到的点连一条边,最后肯定会连成一颗以n+1为根的树(我们拿n+1代表被弹出去了).题目所 ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- [HNOI2010] 弹飞绵羊 (分块)
[HNOI2010] 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- 「洛谷P3202」[HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
随机推荐
- CSS 技术
浏览本篇文章前可以先看之前的前端网页介绍和html常用标签以便更容易理解 本文目录: 目录 CSS 技术介绍 CSS 语法规则 CSS 和 HTML 的结合方式 第一种: 第二种 第三种 CSS 选择 ...
- python各版本下载
python2源码压缩包 Python-2.7.9.tgz Python-2.7.10.tgz Python-2.7.11.tgz Python-2.7.12.tgz Python-2.7.13. ...
- Java集合框架(一)-ArrayList
大佬理解->Java集合之ArrayList 1.ArrayList的特点 存放的元素有序 元素不唯一(可以重复) 随机访问快 插入删除元素慢 非线程安全 2.底层实现 底层初始化,使用一个Ob ...
- Mybatisi和Spring整合源码分析
一.MybatisSpring的使用 1.创建 Maven 工程. 2.添加依赖,代码如下 <dependency> <groupId>org.mybatis</grou ...
- PaddleOCR系列(二)--hubserving & pdserving & hub install
一.各种部署方式特点及注意事项 简称 hubserving=PaddleHub Serving pdserving=PaddleHub Serving hub install =指通过paddlehu ...
- 传统 API 管理与测试过程正面临严峻的挑战
随着测试左移思想的引入, API (应用程序编程接口)经济的飞速增长导致对 API 管理平台的需求相应增加.越来越多的企业注重并关注接口测试.单纯的做接口测试或者做好接口测试的本质工作其实并不复杂: ...
- Centos7较为彻底的删除mysql
Centos7下较为彻底的删除mysql(root 身份操作) 删除mysql安装包 1. yum检查 yum list installed | grep mysql 安装则直接删除 示例:yum r ...
- P2183 [国家集训队]【一本通提高组合数学】礼物
[国家集训队]礼物 题目背景 一年一度的圣诞节快要来到了.每年的圣诞节小 E 都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小 E 心目中的重要性不同,在小 E 心中分量越重的人,收到的礼物会 ...
- 常用的函数式接口Consumer接口练习字符串拼接输出
题目 下面的字符串数组当中有多条信息,请按照格式"姓名: XX 性别: XX"的格式将信息打印出来,要求将 打印姓名的动作为第一个Consumer接口的Lambda实例,将打印性别 ...
- 【ASP.NET Core】选项类的依赖注入
咱们继续上一个话题.先简单复习一下,根据老周前面文章的介绍,选项类体系的基本套路是通过 IOptionsFactory 来创建选项类实例的.而我们在服务容器(IServiceCollection)上是 ...