bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹
2002: [Hnoi2010]Bounce 弹飞绵羊
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 4055 Solved: 2172
[Submit][Status]
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
1 2 1 1
3
1 1
2 1 1
1 1
Sample Output
3
HINT
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 310000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
//AC
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=(char)getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=(char)getchar(),ch<='' && ch>='');
return x*(flag?-:);
} int n,m;
int ch[MAXN][],pnt[MAXN];
int siz[MAXN];
bool rt[MAXN];
bool is_root(int cur)
{
return pnt[cur]== || (cur!=ch[pnt[cur]][] && cur!=ch[pnt[cur]][]);
} void push_up(int cur)
{
siz[cur]=siz[ch[cur][]]+siz[ch[cur][]]+;
}
void rotate(int cur)
{
int p=pnt[cur],anc=pnt[p];
int dir=ch[p][]==cur;
if (!is_root(p))
ch[anc][ch[anc][]==p]=cur; pnt[cur]=anc;
pnt[ch[cur][dir]]=p;
ch[p][-dir]=ch[cur][dir];
ch[cur][dir]=p;
pnt[p]=cur;
push_up(p);
push_up(cur);
} //int stack[MAXN];
void splay(int cur)
{
int now;
/*
int tops=-1;
now=cur;
while (pnt[now])
stack[++tops]=now;
int i;
for (i=tops;i>=0;i--)
push_down(i);
*///本題不用打標記
while (!is_root(cur))
{
int p=pnt[cur],anc=pnt[p];
if (is_root(pnt[cur]))
rotate(cur);
else if ( (ch[p][]==cur) == (ch[anc][]==p) )
rotate(p),rotate(cur);//Attention!
else
rotate(cur),rotate(cur);
}
push_up(cur);
}
void Access(int cur)
{
int son=;
for (;cur;cur=pnt[son=cur])
{
splay(cur);
ch[cur][]=son;
push_up(cur);
}
}
int a[MAXN];
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
scanf("%d",&n);
for (i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for (i=;i<=n;i++)
siz[i]=;
siz[n+]=;
for (i=;i<=n;i++)
{
int t=i+a[i];
if (t>n+)t=n+;
pnt[i]=t;//初始化時不存在重鏈
}
scanf("%d",&m);
int opt;
while (m--)
{
scanf("%d",&opt);
if (opt==)
{
scanf("%d",&x);
x++;//編號從0開始
Access(x);
splay(x);//保證siz[x]包含整棵子樹
printf("%d\n",siz[x]-);
//printf("%d\n",siz[ch[x][0]]); 二者等價
}else
{
scanf("%d%d",&x,&y);
x++;
Access(x);
splay(x);//意味着ch[x][1]==null
//此時pnt[x]不在這顆splay上
pnt[ch[x][]]=pnt[x];
pnt[x]=;
ch[x][]=;
push_up(x);//對於x即其子節點有所改動時使用
int t=x+y;;
if (t>n+)t=n+;
pnt[x]=t;
}
}
return ;
}
bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹的更多相关文章
- 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 弹飞绵羊 (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 弹飞绵羊 【分块】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...
- 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- 洛谷 P3203 BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...
随机推荐
- 在 VS14 上安装 Visual F# Power Tools
在 VS14 上安装 Visual F# Power Tools Visual F# Power Tools 眼下版本号 1.3,下载地址:http://visualstudiogallery.msd ...
- [Webpack 2] Intro to the Production Webpack Course
There are several lessons that will build on top of this project. It is a fairly standard, small web ...
- windows修改mysql默认字符集不成功
今天下午弄了半天,终于把mysql的默认字符集弄成了, 按照网上的说法,什么修改mysql下面的my.ini文件,把参数设置成utf-8,我反复操作,结果都是不成功,后来我把mysql5.0卸载了,网 ...
- JSP/Servlet 中的事件处理
写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应. 其实我们在编写JSP/Serv ...
- CSS排版页面
创建CSS文件如下: @charset "utf-8"; /* CSS Document */ *{ margin:0px; padding:0px; border:0px; } ...
- Android 设计随便说说之简单实践(合理组合)
上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...
- 手把手教你Windows下Go语言的环境搭建
1.想写GO语言首先得下载go语言的开发包 官方下载地址:https://code.google.com/p/go/downloads/list 我用的是Win7 64位的操作系统,截图如下: 2.把 ...
- xp和win 2003远程桌面强制进入命令
xp和win 2003远程桌面强制进入命令 注意:端口号也可以不写 如果是在win 2003里面进行踢人的话可以用命令: mstsc /console /v:172.25.100.27:3389 如果 ...
- Mybatis的学习总结:mybatis的入门介绍
一.myBatis简述 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- CI 笔记,使用 json的参考文档(废弃)
Json的处理转换, Json转换步骤, a) 先设置json为空字符串, b) While循环,遍历, While(!!$row = mysql_fetch_arra ...