B2002 [Hnoi2010]Bounce 弹飞绵羊 分块

原来做过,看大家都做这道题都热情高涨,沈爷爷debug这道题4天,作为告诉他这个题的人,我还有点不好意思。。。我自己也就做了一个小时。

其实这个题思路还好,就是维护每个点的出块次数和跳出块的位置,然后访问的时候直接调用块。

这个题还有一个比较坑的地方,就是每个绵羊只有到队尾的时候才会被弹飞,所以不用担心在中间停止的情况。

题干:

Description

某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。
Input

第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000
Output
对于每个i=1的情况,你都要输出一个需要的步数,占一行。
Sample Input
4                              
1 2 1 1                           
3
1 1
2 1 1
1 1
Sample Output
2
3
HINT
Source

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
struct node
{
    int l,r;
}a[1100];
int outt[200010], times[200010];
int n,x,blo,bl[200010],top = 0;
int f[200010],num = 0;
void init(int x,int y)
{
    lv(i,y,x)
    {
        // cout<<i<<" "<<f[i]<<endl;
        if(i + f[i] > a[bl[i]].r)
        {
            times[i] = 1;
            outt[i] = i + f[i];
        }
        else
        {
            times[i] = 1 + times[i + f[i]];
            outt[i] = outt[i + f[i]];
        }
    }
}
int main()
{
    read(n);
    blo = sqrt(n);
    duke(i,1,n)
    {
        read(f[i]);
    }
    for(int i = 1;i <= n;i += blo)
    {
        a[++num].l = i;
        a[num].r = i + blo - 1;
    }
    top = 0;
    duke(i,1,n)
    {
        if(a[top].r < i)
        {
            top++;
        }
        bl[i] = top;
    }
    init(1,n);
    /*duke(i,1,n)
    {
        printf("%d ",outt[i]);
    }
    cout<<endl;
    duke(i,1,n)
    {
        printf("%d ",times[i]);
    }
    cout<<endl;*/
    int m;
    /*duke(i,1,n)
    {
        printf("%d ",bl[i]);
    }
    cout<<endl;*/
    read(m);
    while(m--)
    {
        int x,y,z;
        read(x);
        read(y);
        y += 1;
        if(x == 1)
        {
            int ans = times[y],z = outt[y];
            //cout<<ans<<" "<<bl[y]<<" "<<z<<"="<<endl;
            for(int i = bl[y];i <= num && z <= n;i++)
            {
                // cout<<ans<<" "<<z<<endl;
                // cout<<z<<" "<<outt[z]<<endl;
                ans += times[z];
                z = outt[z];
            }
            printf("%d
",ans);
        }
        else
        {
            read(z);
            f[y] = z;
            init(a[bl[y]].l,a[bl[y]].r);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DukeLv/p/9638140.html