题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002

第一次用分块,感觉超方便啊;

如果记录每个点的弹力系数,那么是O(1)修改O(n)查询;

如果记录每个点几次被弹飞,那么是O(n)修改O(1)查询;

那么如果分成根号n块,则相当于每块都路径压缩,修改和查询都是根号n的复杂度!

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int const maxn=;
int n,m,k[maxn],nxt[maxn],t[maxn],bh[maxn],K;
void change(int x)
{
// int s=0,nw=x;
// while(bh[nw]==bh[x]&&nw<=n)s++,nw+=k[nw];
// t[x]=s;nxt[x]=nw;
int y=x+k[x];
if(bh[y]!=bh[x])t[x]=,nxt[x]=y;
else t[x]=t[y]+,nxt[x]=nxt[y];
}
int query(int x)
{
// if(x>n)return 0;
// int ret=t[x];
// ret+=query(nxt[x]);
// return ret;
int ret=;
while(x<=n)ret+=t[x],x=nxt[x];
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&k[i]);
K=sqrt(n);
for(int i=n;i;i--)
{
// if(i>tot*K)tot++;bh[i]=tot;
bh[i]=(i-)/K+;
// int j=i,s=0;
// while(bh[j]==bh[i]&&j<=n)j+=k[j],s++;
// t[i]=s;nxt[i]=j;
int j=i+k[i];
if(bh[j]!=bh[i])t[i]=,nxt[i]=j;
else t[i]=t[j]+,nxt[i]=nxt[j];
}
scanf("%d",&m);
int x,y,z;
while(m--)
{
scanf("%d%d",&x,&y);y++;
if(x==)printf("%d\n",query(y));
else
{
scanf("%d",&z);
k[y]=z;
// for(int i=(bh[y]-1)*K+1;i<=y;i++)change(i);
for(int i=y;i>(bh[y]-)*K;i--)change(i);
}
}
return ;
}

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 弹飞绵羊 分块

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

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

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

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

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

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

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

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

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

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

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

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

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...

随机推荐

  1. HDU 4499

    题目大意: N*M的棋盘上摆了一些棋子,在剩余位置上尽可能多的摆上炮,使所有炮不能互吃 dfs+回溯 #include <iostream> #include <cstdio> ...

  2. 字典树模板题 POJ 2503

    #include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void in ...

  3. ACDream:1210:Chinese Girls' Amusement【水题】

    Chinese Girls' Amusement Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Oth ...

  4. 新版VS-code如何自动换行?

    文件  -> 首选项 -> 设置 -> 编辑器 找到 // 控制折行方式.可以选择: - "off" (禁用折行), - "on" (视区折行 ...

  5. 转载:用vector保存对象时保存指针的优点, 以及reserve的使用

    #include <vector> #include <stdio.h> class A { public: A() { printf("A()/n"); ...

  6. java 字节码 指令集

    有时候为了能理解JVM对程序所做的优化等,需要查看程序的字节码,因此知道了解一些常见的指令集很重要! 指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推 ...

  7. 免费SSL申请

    https://letsencrypt.org/ https://letsencrypt.org/docs/client-options/ ACMESharp (.NET, PowerShell) w ...

  8. Go -- 多个go文件包名都是main

    用go run *.go 或 go run one.go two.go main.go

  9. zz年度热门编程语言排行榜

    原文在这里:Link 蛮有意思的,可以看看.

  10. php monolog 的写日志到unix domain socket 测试终于成功

    在另外一个客户端执行 php s.php后, 通过nc -lU /tmp/tg.sck 建立的unix domain socket 有接收到消息. <?php require 'vendor/a ...