传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002

这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块。先想,如果仅仅记录每个节点需要几步可以弹飞,就可以做到O(1)查询O(n)修改;如果仅仅记录每个节点弹力洗漱,就可以做到O(n)查询O(1)修改。这会不会给人一种随机访问数组与链表的感觉呢?如果把n个弹簧分成√n块,记录块里每个弹簧需要几步才能跳出这一个块,并且记录跳出这个块后落到了哪,这样子查询以及修改复杂度都是O(√n)了。

#include <cstdio>
#include <cmath> const int maxn = 200005; int n, m, a[maxn], t1, t2, t3, siz, zuihou, kaishi, to[maxn], stp[maxn]; inline int qry(int pos) {
int rt = 0;
while (~pos) {
rt += stp[pos];
pos = to[pos];
}
return rt;
}
inline void upd(int pos, int data) {
kaishi = pos / siz * siz;
zuihou = (pos / siz + 1) * siz - 1;
a[pos] = data;
for (int i = pos; i >= kaishi; --i) {
if (i + a[i] >= n) {
to[i] = -1;
stp[i] = 1;
}
else if (i + a[i] > zuihou) {
to[i] = i + a[i];
stp[i] = 1;
}
else {
to[i] = to[i + a[i]];
stp[i] = stp[i + a[i]] + 1;
}
}
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d", &n);
siz = (int)sqrt((float)n + 0.5f);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
for (int i = n - 1; ~i; --i) {
zuihou = (i / siz + 1) * siz - 1;
if (i + a[i] >= n) {
to[i] = -1;
stp[i] = 1;
}
else if (i + a[i] > zuihou) {
to[i] = i + a[i];
stp[i] = 1;
}
else {
to[i] = to[i + a[i]];
stp[i] = stp[i + a[i]] + 1;
}
}
scanf("%d", &m);
while (m--) {
scanf("%d%d", &t1, &t2);
if (t1 == 1) {
printf("%d\n", qry(t2));
}
else {
scanf("%d", &t3);
upd(t2, t3);
}
}
return 0;
}

  

bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】的更多相关文章

  1. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  2. [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块

    Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...

  3. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...

  4. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    这个题体现了分块不只是最大值最小值众数次数,而是一种清真的思想. 我们把整个序列分块,在每个块里处理每个位置跳出这个块的次数和跳出的位置,那么每次修改n0.5,每次查询也是,那么O(m* n0.5)的 ...

  5. BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】

    BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...

  6. 【bzoj2002】[Hnoi2010]Bounce 弹飞绵羊 分块

    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊 2014年7月30日8101 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀 ...

  7. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

  8. [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT

    Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...

  9. 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊 分块/LCT

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...

随机推荐

  1. Meteor教程

    Meteor 是一个构建在 Node.js 之上的平台,用来开发实时网页程序.Meteor 程序位于数据库和用户界面之间,保持二者之间的数据同步更新. 因为 Meteor 是基于 Node.js 开发 ...

  2. strstr-strcat实现

    strstr 调用格式 #include <string.h> char *strstr(const char *haystack, const char *needle); 功能说明 该 ...

  3. [Javascript] Link to Other Objects through the JavaScript Prototype Chain

    Objects have the ability to use data and methods that other objects contain, as long as it lives on ...

  4. Angular2.x

    Angular版本 Angular1和Angular4分别是Angular的两个版本,也就是Angular1.x和Angular2.x(除了Angular1以外,其余都属于Angular2.x). 1 ...

  5. antd 表单验证

    antd  form 自带方法 /** * 获取 form 自带方法 * getFieldDecorator * getFieldsError * getFieldError * isFieldTou ...

  6. coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command

    在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...

  7. 关于OutOfMemoryError的处理

    转自:http://www.blogjava.net/rosen/archive/2010/05/21/321575.html http://www.blogjava.net/rosen/archiv ...

  8. 第3周课后实践&#183;程序阅读(4)-利用引用訪问私有数据成员

    /* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名:test.cpp * 作 者:刘畅 * 完毕日期:2015年 3 月 2 ...

  9. 6.游戏特别离不开脚本(3)-JS脚本操作java(2)(直接解析JS公式,并非完整JS文件或者函数)

    在游戏中可以考虑数据由javabean保存,逻辑方法由JS提供. public class Bean4JS { private int id; private String name; private ...

  10. HDU1083 Courses —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-1083 Courses Time Limit: 20000/10000 MS (Java/Others)    Memory ...