http://www.lydsy.com/JudgeOnline/problem.php?id=2002
分块基础题,初次接触,很巧妙。
OJ好像挂了,没法提交。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; int a[],cnt[],next[],l[],r[],belong[],n,m; void build()
{
int block = sqrt(n);
int num = n/block;
if(n%block) num++;
for(int i = ;i <= num;i++)
{
l[i] = (i-)*block+;
r[i] = i*block;
}
r[num] = n;
for(int i = ;i <= n;i++) belong[i] = (i-)/block+;
for(int i = n;i >= ;i--)
{
int temp = i+a[i];
if(temp > n)
{
cnt[i] = ;
next[i] = -;
}
else if(belong[i] == belong[temp])
{
cnt[i] = cnt[temp]+;
next[i] = next[temp];
}
else
{
cnt[i] = ;
next[i] = temp;
}
}
} int getsum(int x)
{
int ans = cnt[x];
while(next[x] > )
{
x = next[x];
ans += cnt[x];
}
return ans;
} void update(int x,int y)
{
a[x] = y;
for(int i = x;i >= l[belong[x]];i--)
{
int temp = i+a[i];
if(temp > n)
{
cnt[i] = ;
next[i] = -;
}
else if(belong[i] == belong[temp])
{
cnt[i] = cnt[temp]+;
next[i] = next[temp];
}
else
{
cnt[i] = ;
next[i] = temp;
}
}
}
int main()
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
build();
scanf("%d",&m);
while(m--)
{
int i,j,k;
scanf("%d%d",&i,&j);
if(i == ) printf("%d\n",getsum(j+));
else
{
scanf("%d",&k);
update(j+,k);
}
}
return ;
}