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 分析: 绵羊在弹飞的路径中相当于一棵 ...
随机推荐
- 再次阅读《精通CSS-高级Web标准解决方案(第二版)》
昨天(2015年11月21日) 在我们学校举行了大型招聘会.我面试了三家企业.有一家企业是先做笔试题的,做完后发现自己还是很多细节处理得不够.无论还有没有二面,我还是要重新把<精通CSS> ...
- Java - 正则表达式常用操作
验证 简单验证 String regex = "\\d{4}-\\d{2}-\\d{2}"; String input = "2016-01-01"; asse ...
- SQL_从星期一到星期六自动打卡SQL代码
create proc sp_MarkAutoKQ as begin ) ---创建两个变量,接收当前时间和当天是星期几 set @dateA=getdate() ---获取当前时间 set @dat ...
- %r与%s的区别
%r用rper()方法处理对象 %s用str()方法处理对象 有些情况下,两者处理的结果是一样的,比如说处理int型对象. 例一: print "I am %d years old.&quo ...
- 【转】MySQL的安装与配置
一.MySQL的安装 1.在线安装: 命令:sudo apt-get install mysql-server 在安装的过程中将提示为“root”用户设置密码,输入自己的密码即可,安装按成后已自动配置 ...
- C#对象XML序列化
1.Xml序列化操作类 .Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象. 以下 ...
- OpenGL8-直接分配显存-极速绘制(2)
视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440/*** OpenGL8-直接分配显存-极速绘制(Opengl1 ...
- KindEditor配置步骤
KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等主流浏览器. KindEditor ...
- css3学习笔记之多列
CSS3 创建多列 column-count 属性指定了需要分割的列数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!D ...
- SQL Server 收缩事务日志的方法
由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005) Backup Log DataBaseName with no_ ...