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个装置,每个装置设定初始弹力系 ...
随机推荐
- sql server和my sql 命令(语句)的差别,sql server与mysql的比較
sql与mysql的比較 1.连接字符串 sql :Initial Catalog(database)=x; --数据库名称 Data Source(source)=x; ...
- Cocos2d-x3.0TestCpp文件夹笔记(二)
3.Actions-Basic:此demo中体现ccp由Point取代 ①ActionManual:直接设置精灵的属性demo. const Color3B Color3B::RED (255, ...
- [TypeScript] Understanding Decorators
Decorators are a feature of TypeScript that are becoming more and more common in many major librarie ...
- JavaBean中DAO设计模式介绍(转)
一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/Ser ...
- Cocos2d-x中获取设备语言的方法
1.cocos2dx获取设备语言的方法:CCApplication::sharedApplication()->getCurrentLanguage() 2.cocos2dx 2.1.4支持识别 ...
- Linux学习笔记总结--ssh认证登录
原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密过的 ...
- [学习笔记]设计模式之Adapter
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Adapter(适配器)模式主要解决接口不匹配的问题.为此,让我们要回到最初Builder模式创建平行世界时,白雪公主和小霍比特人的谜 ...
- bash登录式shell(完全切换)与非登陆式shell(不完全切换)区别
1.以登录式shell切换用户 su - username 登录式shell读取配置文件及其顺序: /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~ ...
- 最新的C#SqlHelper 类苏飞修改版(转载)
/// <summary> /// 类说明:公共的数据库访问访问类 /// 编码日期:2010-4-22 /// 编 码 人:苏飞 /// 联系方式:361983679 Email:[ur ...
- POJ 2039 To and Fro(模拟)
To and Fro Description Mo and Larry have devised a way of encrypting messages. They first decide sec ...