# bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 分块

## 2002: [Hnoi2010]Bounce 弹飞绵羊

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 11719  Solved: 5923
[Submit][Status][Discuss]

4
1 2 1 1
3
1 1
2 1 1
1 1

2
3

## 思路：

1.F[i]，表示第i个点多少歩后会跳出这个块。
2.S[i]，表示第i个点跳出块以后会跳到哪里。

## 代码：

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 2e5+10;

int n,num,block,belong[maxn],a[maxn],f[maxn],s[maxn],le[maxn],ri[maxn];

void build(){
num = sqrt(n*1.0);
block = n/num;
if(n%num) block++;
for(int i=1; i<=n; i++){
belong[i] = (i-1)/num+1;
}
for(int i=1; i<=block; i++){
le[i] = (i-1)*num+1;
ri[i] = i*num;
}
ri[block] = n;
for(int i=block; i>=1; i--){
for(int j=ri[i]; j>=le[i]; j--){
int t = j+a[j];
f[j] = (t>ri[i] ? 1 : f[t]+1);
s[j] = (t>ri[i] ? t : s[t]);
}
}
}

int que(int x){
int res = 0;
while(x <= n){
res += f[x];
// cout << x << endl;
x = s[x];
// cout << x << " ====\n";
}
return res;
}

void upd(int x,int y){
int id = belong[x];
int i = x, cnt = 0;
a[x] = y;
while(i <= ri[id]){
i = i+a[i];
cnt++;
}
f[x] = cnt;
s[x] = i;
for(int j=x-1; j>=le[id]; j--){
int t = j+a[j];
f[j] = (t>ri[id] ? 1: f[t]+1);
s[j] = (t>ri[id] ? t : s[t]);
}
// cout << f[x] << " " << s[x] << " " << ri[id] << endl;
}

int main(){
for(int i=1; i<=n; i++)
build();
// cout << block << endl;
// for(int i=1; i<=n; i++)
//     cout << f[i] << " " << s[i] << endl;
while(q--){
if(op == 1){
printf("%d\n",que(x));
}else{
int x,y; scanf("%d%d",&x,&y);
x++;
upd(x,y);
}
}

return 0;
}

posted @ 2017-09-03 19:05  _yxg123  阅读(92)  评论(0编辑  收藏  举报