【HNOI2010】弹飞绵羊 - LCT
题目描述
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
思路
LCT模板题,把这n个装置看作n+1个节点,
n+1为虚拟节点,到达这个点即被弹飞,初
始时对每个节点进行link(i,i+k[i]),修改时先
cut(i,i+k[i]),再link(i,i+K),并让k[i] = K
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
struct Link_Cut_Tree {
struct Splay { int ch[2],f,mark,sum,w; }t[maxn];
inline bool getfa(int x) { return t[t[x].f].ch[1] == x; }
inline bool isroot(int x) { return t[t[x].f].ch[0] != x && t[t[x].f].ch[1] != x; }
inline void pushup(int x) { t[x].sum = t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].w; }
inline void rev(int x) { t[x].mark ^= 1; swap(t[x].ch[0],t[x].ch[1]); }
inline void pushdown(int x) {
if (t[x].mark) {
if (t[x].ch[0]) rev(t[x].ch[0]);
if (t[x].ch[1]) rev(t[x].ch[1]);
t[x].mark = 0;
}
}
inline void rotate(int x) {
int f = t[x].f,g = t[f].f,c = getfa(x);
if (!isroot(f)) t[g].ch[getfa(f)]=x; t[x].f = g;
t[f].ch[c] = t[x].ch[c^1]; t[t[f].ch[c]].f = f;
t[x].ch[c^1] = f; t[f].f = x;
pushup(f); pushup(x);
}
inline void check(int x) { if (!isroot(x)) check(t[x].f); pushdown(x); }
inline void splay(int x) {
check(x);
for (;!isroot(x);rotate(x))
if (!isroot(t[x].f)) rotate(getfa(t[x].f) == getfa(x) ? t[x].f : x);
}
inline void access(int x) { for (int y = 0;x;y = x,x = t[x].f) splay(x),t[x].ch[1] = y,pushup(x); }
inline void makeroot(int x) { access(x); splay(x); rev(x); }
inline int findroot(int x) {
access(x); splay(x);
while (t[x].ch[0]) pushdown(x),x = t[x].ch[0];
return x;
}
inline void link(int x,int y) { makeroot(x),t[x].f = y; }
inline void cut(int x,int y) { makeroot(x); access(y); splay(y); if (t[y].ch[0] == x && t[x].ch[1] == 0) t[x].f = t[y].ch[0] = 0; }
inline void split(int x,int y) { makeroot(x); access(y); splay(y); }
}lct;
int n,q,k[maxn];
int main() {
scanf("%d",&n);
for (int i = 1;i <= n+1;i++) lct.t[i].w = 1;
for (int i = 1;i <= n;i++) {
scanf("%d",&k[i]);
lct.link(i,k[i]+i <= n ? k[i]+i : n+1);
}
scanf("%d",&q);
while (q--) {
int op,x,y;
scanf("%d%d",&op,&x);
x++;
if (op == 1) lct.split(x,n+1),printf("%d\n",lct.t[n+1].sum-1);
else {
scanf("%d",&y);
lct.cut(x,k[x]+x <= n ? k[x]+x: n+1);
lct.link(x,y+x <= n ? y+x : n+1);
k[x] = y;
}
}
return 0;
}
【HNOI2010】弹飞绵羊 - LCT的更多相关文章
- [Luogu P3203] [HNOI2010]弹飞绵羊 (LCT维护链的长度)
题面 传送门:洛谷 Solution 这题其实是有类似模型的. 我们先考虑不修改怎么写.考虑这样做:每个点向它跳到的点连一条边,最后肯定会连成一颗以n+1为根的树(我们拿n+1代表被弹出去了).题目所 ...
- 洛谷P3203 [HNOI2010] 弹飞绵羊 [LCT]
题目传送门 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- P3203 [HNOI2010]弹飞绵羊(LCT)
弹飞绵羊 题目传送门 解题思路 LCT. 将每个节点的权值设为\(1\),连接\(i\)和\(i+ki\),被弹飞就连上\(n\),维护权值和\(sum[]\).从\(j\)弹飞需要的次数就是\(sp ...
- BZOJ2002[Hnoi2010]弹飞绵羊——LCT
题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...
- P3203 [HNOI2010]弹飞绵羊(LCT)
P3203 [HNOI2010]弹飞绵羊 LCT板子 用一个$p[i]$数组维护每个点指向的下个点. 每次修改时cut*1+link*1就解决了 被弹出界时新设一个点,权为0,作为终点表示出界点.其他 ...
- [HNOI2010] 弹飞绵羊 (分块)
[HNOI2010] 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上 ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- 「洛谷P3202」[HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
随机推荐
- deepin换源
python的pip源 pip install pip -U pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/sim ...
- vector 赋初始值的问题
这个,输出为1 这个,啥都输不出来. 据说是因为没有初始化. 其实我搜了一下 vector<vector<int> > A;//正确的定义方式 vector<vector ...
- PHP getimagesizefromstring - 获取图片信息函数
getimagesizefromstring — 从字符串中获取图像尺寸信息.高佣联盟 www.cgewang.com 语法 array getimagesizefromstring ( string ...
- PHP constant() 函数
实例 返回一个常量的值: <?php//define a constantdefine("GREETING","Hello you! How are you tod ...
- IC行业常见用语
https://www.cnblogs.com/yeungchie/ Active Devices 有源器件 MOSFET Metal-Oxide-Semicoductor Field-Effect ...
- 2020牛客暑假多校训练营 第二场 E Exclusive OR FWT
LINK:Exclusive OR 没做出 原因前面几篇说过了. 根据线性基的知识容易推出 不超过\(w=log Mx\)个数字即可拼出最大值 其中Mx为值域. 那么考虑w+2个数字显然也为最大值.. ...
- C/C++编程笔记:C语言制作情侣必备《爱情电子相册》,源码解析!
今天是521,就分享一个程序员必会的——情侣回忆杀<爱情电子相册>吧!话不多说,先上思路,后接源码! 具备能力: 1.基本可视化编程 1.1 initgraph(800,600); 1.2 ...
- mysql优化:explain 和 profile
此文转自:https://blog.csdn.net/hanjungua8144/article/details/84317829 一.SQL查询语句优化基本思路和原则 优化更需要优化的Query.定 ...
- Hadoop学习之NCDC天气数据获取
期望目的 下载<Hadoop权威教程>里用到的NCDC天气数据,供后续在此数据基础上跑mapred程序. 操作过程 步骤一.编写简单的shell脚本,下载数据文件到本地文件系统 已知NCD ...
- CentOS部署RabbitMQ
CentOS版本:CentOS-7-x86_64-DVD-1804 RabbitMQ版本:3.7.24 1. 下载安装包 因为RabbitMQ是erlang语言开发的,所以需要提前安装erlang环境 ...