BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

分块

分为根号n块,对每一个装置,记录它跳出所在块需要的步数和跳出所在块之后到达的点。复杂度就是O(根号n)了

#include
#include
#define N 200005
#define SN 1000
using namespace std;
int k[N], step[N], next[N], n, block, cnt, l[SN], r[SN], belong[N];
void init()
{
    block=sqrt(n);
    if(block*block==n)cnt=block;
    else cnt=block+1;
    l[1]=0; r[1]=block-1;
    for(int i = 2; i <= block; i++)
    {
        l[i]=l[i-1]+block;
        r[i]=r[i-1]+block;
    }
    if(cnt>block)
        l[cnt]=l[cnt-1]+block, r[cnt]=n-1;
    for(int i = 1; i <= cnt; i++)
    {
        for(int j = r[i]; j >= l[i]; j--)
        {
            belong[j]=i;
            if(j+k[j]>r[i])step[j]=1, next[j]=j+k[j];
            else step[j]=step[j+k[j]]+1, next[j]=next[j+k[j]];
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
        scanf("%d",&k[i]);
    init();
    int m; scanf("%d",&m);
    while(m--)
    {
        int cmd, x, y;
        scanf("%d",&cmd);
        if(cmd==1)
        {
            scanf("%d",&x);
            int ans=0;
            while(xx];
                x=next[x];
            }
            printf("%d\n",ans);
        }
        else
        {
            scanf("%d%d",&x,&y);
            k[x]=y;
            for(int i = x, ii = l[belong[x]]; i >= ii; i--)
            {
                if(i+k[i]>r[belong[x]])step[i]=1,next[i]=i+k[i];
                else step[i]=step[i+k[i]]+1, next[i]=next[i+k[i]];
            }
        }
    }
}


你可能感兴趣的:(其它-分块)