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 分析: 绵羊在弹飞的路径中相当于一棵 ...
随机推荐
- Colossal Fibonacci Numbers(巨大的斐波那契数)UVA 11582
评测地址:http://acm.hust.edu.cn/vjudge/problem/41990 The i'th Fibonacci number f (i) is recursively de n ...
- Git CMD - show: Show various types of objects
命令格式 git show [options] <object>… 实例 a) 查看某次提交的信息 $ git show <commit> b) 查看远程仓库的信息. git ...
- 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
网站更换域名,把旧域名用301指到新域名来. 从iis中设置url永久转向就可以,看上去很容易,用了一会儿才发现,参数都没有带上. 从微软网站上找到如下说明,果然好使: 重定向参考 (IIS 6. ...
- SQL 行转列
---1.最简单的行转列/* 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94 想变成(得到如下结 ...
- SQL Server的三种物理连接之Loop Join(一)
Sql Server有三种物理连接Loop Join,Merge Join,Hash Join, 当表之间连接的时候会选择其中之一,不同的连接产生的性能不同,理解这三种物理连接对性能调优有很大帮助. ...
- error LNK2005: DDX_Control 已经在 uafxcwd.lib(wincore2.obj) 中定义
编译错误提示: 1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(classCData ...
- String.Trim
String.Trim有2个重载方法,分别是:Trim()和Trim(char[]),根据官方的解释 Trim():从当前String对象移除所有前导空白字符和尾部空白字符. Trim(char[]) ...
- JS实现登录页面记住密码和enter键登录
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>按 ...
- js 书写规范
1.字符串用单引号 2.运算符号和变量之间用空格建立间距 3.书写插件时使用 'use strict'; 开头 4.方法如果是获取或者设置数据集合则使用动词如 getData,setData,eac ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...