B2002 [Hnoi2010]Bounce 弹飞绵羊 分块
原来做过,看大家都做这道题都热情高涨,沈爷爷debug这道题4天,作为告诉他这个题的人,我还有点不好意思。。。我自己也就做了一个小时。
其实这个题思路还好,就是维护每个点的出块次数和跳出块的位置,然后访问的时候直接调用块。
这个题还有一个比较坑的地方,就是每个绵羊只有到队尾的时候才会被弹飞,所以不用担心在中间停止的情况。
题干:
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input 第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=,对于100%的数据n<=,m<=
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
Sample Input Sample Output HINT
Source
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
struct node
{
int l,r;
}a[];
int outt[], times[];
int n,x,blo,bl[],top = ;
int f[],num = ;
void init(int x,int y)
{
lv(i,y,x)
{
// cout<<i<<" "<<f[i]<<endl;
if(i + f[i] > a[bl[i]].r)
{
times[i] = ;
outt[i] = i + f[i];
}
else
{
times[i] = + times[i + f[i]];
outt[i] = outt[i + f[i]];
}
}
}
int main()
{
read(n);
blo = sqrt(n);
duke(i,,n)
{
read(f[i]);
}
for(int i = ;i <= n;i += blo)
{
a[++num].l = i;
a[num].r = i + blo - ;
}
top = ;
duke(i,,n)
{
if(a[top].r < i)
{
top++;
}
bl[i] = top;
}
init(,n);
/*duke(i,1,n)
{
printf("%d ",outt[i]);
}
cout<<endl;
duke(i,1,n)
{
printf("%d ",times[i]);
}
cout<<endl;*/
int m;
/*duke(i,1,n)
{
printf("%d ",bl[i]);
}
cout<<endl;*/
read(m);
while(m--)
{
int x,y,z;
read(x);
read(y);
y += ;
if(x == )
{
int ans = times[y],z = outt[y];
//cout<<ans<<" "<<bl[y]<<" "<<z<<"="<<endl;
for(int i = bl[y];i <= num && z <= n;i++)
{
// cout<<ans<<" "<<z<<endl;
// cout<<z<<" "<<outt[z]<<endl;
ans += times[z];
z = outt[z];
}
printf("%d\n",ans);
}
else
{
read(z);
f[y] = z;
init(a[bl[y]].l,a[bl[y]].r);
}
}
return ;
}
B2002 [Hnoi2010]Bounce 弹飞绵羊 分块的更多相关文章
- 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块
[bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊 分块/LCT
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
- bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块
Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...
- COGS.1689.[HNOI2010]Bounce 弹飞绵羊(分块)
题目链接 /* 分块,维护从某位置跳到下一个块需要多少步 */ #include<cmath> #include<cstdio> #include<cctype> ...
- bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 分块
这个题体现了分块不只是最大值最小值众数次数,而是一种清真的思想. 我们把整个序列分块,在每个块里处理每个位置跳出这个块的次数和跳出的位置,那么每次修改n0.5,每次查询也是,那么O(m* n0.5)的 ...
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
随机推荐
- Mongo优化笔记
最近MongoDb服务器负载比较高,容易出问题,这里把优化的方式整理一下. 1.由于各个项目组共用一个mongo实例,所以一个项目组的问题会影响到别的项目组,所以需要把各个项目的数据从一个实例中剥离出 ...
- Python 之selenium+phantomJS斗鱼抓取案例
from selenium import webdriver from bs4 import BeautifulSoup import time if __name__ == '__main__': ...
- PHP 之base16加密解密封装
/** * @Description: base16加密 * @Author: Yang * @param $data * @return string */ function base16_enco ...
- centOS7卸载google-chrome
参考: https://www.jianshu.com/p/39d0b8f578d9
- ARX自定义实体
本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h"," ...
- Vue和JQuery相比,除了节省了开发成本,还有什么优点?
1.模块化,变量都是私有作用域,JQuery只能用全局变量.闭包,影响性能 2.组件化 3.因为1,所以方便维护 vuex 要注意刷新清空的问题 vue-router是局部刷新,window.loca ...
- js 阻止冒泡事件和默认事件
阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...
- codevs2833 奇怪的梦境
2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...
- linux修改mysql表结构
增加字段: alter table [tablename] add [字段名] [字段类型] first(首位); alter table [tablename] add [字段名] [字段类型] a ...
- 【codeforces 767B】The Queue
[题目链接]:http://codeforces.com/contest/767/problem/B [题意] 排队去办护照; 给你n个人何时来的信息; 然后问你应该何时去才能在队伍中等待的时间最短; ...