题目描述

某天,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的更多相关文章

  1. [Luogu P3203] [HNOI2010]弹飞绵羊 (LCT维护链的长度)

    题面 传送门:洛谷 Solution 这题其实是有类似模型的. 我们先考虑不修改怎么写.考虑这样做:每个点向它跳到的点连一条边,最后肯定会连成一颗以n+1为根的树(我们拿n+1代表被弹出去了).题目所 ...

  2. 洛谷P3203 [HNOI2010] 弹飞绵羊 [LCT]

    题目传送门 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...

  3. P3203 [HNOI2010]弹飞绵羊(LCT)

    弹飞绵羊 题目传送门 解题思路 LCT. 将每个节点的权值设为\(1\),连接\(i\)和\(i+ki\),被弹飞就连上\(n\),维护权值和\(sum[]\).从\(j\)弹飞需要的次数就是\(sp ...

  4. BZOJ2002[Hnoi2010]弹飞绵羊——LCT

    题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...

  5. P3203 [HNOI2010]弹飞绵羊(LCT)

    P3203 [HNOI2010]弹飞绵羊 LCT板子 用一个$p[i]$数组维护每个点指向的下个点. 每次修改时cut*1+link*1就解决了 被弹出界时新设一个点,权为0,作为终点表示出界点.其他 ...

  6. [HNOI2010] 弹飞绵羊 (分块)

    [HNOI2010] 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上 ...

  7. 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告

    P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...

  8. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  9. 「洛谷P3202」[HNOI2010]弹飞绵羊 解题报告

    P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...

  10. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

随机推荐

  1. JAVA基础系列:JDK目录结构

    0. 名词解释 SDK: Softeare Development Kit,用于开发JavaEE,包括JDK. JDK: Java Development Kit,java开发工具包,包括Java编译 ...

  2. C++语法小记---开篇

    几句闲话 由于C++的语法非常的复杂,但是实际使用的过程中,经常使用的语法也就那么一些,还有比较多的语法很少被使用,时间一长就容易忘记,因此“C++语法小记”主要是将C++中不经常使用和容易忘记的语法 ...

  3. 题解 洛谷 P4632 【[APIO2018] New Home 新家】

    首先考虑可以用二分答案来解决询问,可以二分一个长度\(len\),若在区间\([x-len,x+len]\)内包含了所有\(k\)种的商店,那么这个\(len\)就是合法的,可以通过二分来求其最小值. ...

  4. Mybatis核心模块简介

    Configuration 主要字段 Environment:配置DataSource和TransactionFactory ObjectFactory:bean工厂 MapperRegistry:M ...

  5. 前端学习(十):CSS选择器

    进击のpython ***** 前端学习--CSS选择器 每一条CSS样式声明由两部分组成: 选择器{ 样式: } 在CSS中{}之前的部分就是"选择器","选择器&qu ...

  6. 深入理解Spring AOP 1.0

    本文相关代码(来自官方源码spring-test模块)请参见spring-demysify org.springframework.mylearntest包下. 统称能够实现AOP的语言为AOL,即( ...

  7. electron开发 - mac关闭和隐藏窗口

    针对mac平台的app let willQuitApp = false; // 控制退出方式 mainWindow.on('close', (e) => { if (willQuitApp) { ...

  8. 第二次作业:卷积神经网络 part 1

    第二次作业:卷积神经网络 part 1 视频学习 数学基础 受结构限制严重,生成式模型效果往往不如判别式模型. RBM:数学上很漂亮,且有统计物理学支撑,但主流深度学习平台不支持RBM和预训练. 自编 ...

  9. Ef Core增加Sql方法

    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)] public class DbFunAttribute : Attri ...

  10. 【六省联考2017】组合数问题 题解(矩阵快速幂优化DP)

    题目链接 题目大意:求$(\sum\limits_{i=0}^n C_{nk}^{ik+r})\ mod \ p$的值. --------------------- 讲真,一开始看到这个题我都没往DP ...