2002: [Hnoi2010]Bounce 弹飞绵羊 - BZOJ
Description
某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input
第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
Sample Input
4
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
2
3
入门级link-cut-tree
跟树链剖分很像,不过树链剖分形态不能变,直接把链分好了,然后用线段树维护这些链
这个link-cut-tree的主要操作是access(x),表示访问x节点
规则是每次访问x后从x到根节点的路径变成一条链,把这条链上长出来的枝叶减掉,链在不断地变化,所以用splay维护这个链
对于这个改变父节点,就是先断开他与父节点的链,然后接上他现在的父节点
这个讲的比较详细,可以看一下,http://hi.baidu.com/niyuzheno1/item/30994f43ba02c8c31e19bc28
const
maxn=;
type
node=record
son:array[..]of longint;
size,fa:longint;
root:boolean;
end;
var
tree:array[..maxn]of node;
n,m:longint; procedure rotate(x,w:longint);
var
y:longint;
begin
y:=tree[x].fa;
tree[y].son[w]:=tree[x].son[w xor ];
if tree[x].son[w xor ]<> then tree[tree[x].son[w xor ]].fa:=y;
tree[x].son[w xor ]:=y;
if tree[y].root then
begin
tree[y].root:=false;
tree[x].root:=true;
end
else
if tree[tree[y].fa].son[]=y then tree[tree[y].fa].son[]:=x
else tree[tree[y].fa].son[]:=x;
tree[x].fa:=tree[y].fa;
tree[y].fa:=x;
tree[y].size:=tree[tree[y].son[]].size+tree[tree[y].son[]].size+;
end; procedure splay(x:longint);
var
y:longint;
begin
while tree[x].root=false do
begin
y:=tree[x].fa;
if tree[y].root then
if tree[y].son[]=x then rotate(x,)
else rotate(x,)
else
if tree[tree[y].fa].son[]=y then
if tree[y].son[]=x then
begin
rotate(y,);
rotate(x,);
end
else
begin
rotate(x,);
rotate(x,);
end
else
if tree[y].son[]=x then
begin
rotate(x,);
rotate(x,);
end
else
begin
rotate(y,);
rotate(x,);
end;
end;
tree[x].size:=tree[tree[x].son[]].size+tree[tree[x].son[]].size+;
end; procedure access(x:longint);
var
z:longint;
begin
splay(x);
while tree[x].fa<> do
begin
z:=tree[x].fa;
splay(z);
tree[tree[z].son[]].root:=true;
tree[x].root:=false;
tree[z].son[]:=x;
tree[z].size:=tree[tree[z].son[]].size+tree[tree[z].son[]].size+;
splay(x);
end;
end; procedure init;
var
i:longint;
begin
read(n);
for i:= to n do
begin
tree[i].root:=true;
tree[i].size:=;
read(tree[i].fa);
inc(tree[i].fa,i);
if tree[i].fa>n then tree[i].fa:=n+;
end;
tree[n+].size:=;
tree[n+].root:=true;
end; procedure work;
var
i,x,y:longint;
begin
read(m);
for i:= to m do
begin
read(x,y);
inc(y);
if x= then
begin
access(y);
writeln(tree[tree[y].son[]].size);
end
else
begin
read(x);
splay(y);
tree[tree[y].son[]].fa:=tree[y].fa;
tree[tree[y].son[]].root:=true;
tree[y].son[]:=;
tree[y].size:=tree[tree[y].son[]].size+;
tree[y].fa:=y+x;
if tree[y].fa>n then tree[y].fa:=n+;
end;
end;
end; begin
init;
work;
end.
2002: [Hnoi2010]Bounce 弹飞绵羊 - BZOJ的更多相关文章
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 4055 Solved: 2172[Submi ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2843 Solved: 1519[Submi ...
- 【BZOJ】2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 14802 Solved: 7507[Subm ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 https://www.lydsy.com/JudgeOnline/problem.php?id=2002 分析: 绵羊在弹飞的路径中相当于一棵 ...
随机推荐
- toggle
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Commons JXPath - Extension Functions
Standard Extension Functions 创建新的对象 JXPathContext context = JXPathContext.newContext(null); Book boo ...
- C标准库函数实现之strstr(转)
看下Linux下的实现: char *strstr(const char *s1, const char *s2) { size_t l1, l2; l2 = strlen(s2); if (!l2) ...
- mvc中的webapi
MVC中 webapi的使用 和 在其他网站中如何来调用(MVC) 1.webapi的路由规则注册在App_Start\WebApiConfig.cs文件中 2.webapi控制器继承父类 apiCo ...
- Javascript中while和do-while循环用法详解
while循环 while 语句与 if 语句相似,都有条件来控制语句(或语句块)的执行,其语言结构基本相同:while(conditions){ statements;} while 语句与 ...
- [Bootstrap]概述
——1.html,css,javascript框架 ——2.一般开发响应式布局或者移动优先的项目可以优先考虑 优点 1.css开发版本(可以直接上手)和源码版本(可根据l ...
- spring定时器 @Scheduled
需要注意的几点: 1.spring的@Scheduled注解 需要写在实现上. 2. 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误.需要设定一个proxyta ...
- 【Qt】关于Qt【转】
什么是Qt Qt是一个针对桌面.嵌入式.移动设备的一个跨平台的应用程序开发框架,支持的平台包括Linux.OS X.Windows.VxWorks.QNX.Android.iOS.BlackBerry ...
- PHPStorm 2016.2 - 2016.3许可证服务器
最快,最安全的选择,以激活您的PHPStorm 2016.2 - 2016.3,这是足够的激活服务器,软件将自动激活.该过程将不断更新,如果不工作评价写入,如果有,以激活没有列出的服务器也可以说. 通 ...
- 前端工程模块化——以一个php项目为例
实现一个页面功能总是需要 JavaScript.CSS 和 Template 三种语言相互组织,所以我们真正需要的是一种可以将 JavaScript.CSS 和 Template 同时都考虑进去的模块 ...