BZOj 2002 分块


LCT不会写,只能写写简单一点的分块了。


每sqrt(n)分一块,然后对每一块内的点的信息维护的是跳出这块的下一个位置,和次数。每次操作复杂度为sqrt(n),不是很快。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)
#define PI acos(-1.0)
#define ITER set<int>::iterator
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 100;
int a[maxn];
int nexts[maxn],times[maxn];

int main()
{
    int n,m;
    while( ~ scanf("%d",&n))
    {
        for(int i = 0; i < n; i ++)scanf("%d",&a[i]);
        int block = static_cast<int>(sqrt(n) + 1e-7);
        for(int i = n - 1; ~ i; i --)
        {
            int temp = i + a[i];
            if(temp >= n)
            {
                nexts[i] = -1;times[i] = 1;
            }
            else if(temp >= (i / block + 1) * block)
            {
                nexts[i] = temp;times[i] = 1;
            }
            else nexts[i] = nexts[temp],times[i] = times[temp] + 1;
        }
        scanf("%d",&m);
        while(m --)
        {
            int x,y,z;scanf("%d%d",&x,&y);
            if(x == 1)
            {
                int ans = 0;
                for(int i = y; ~ i; i = nexts[i])
                {
                    ans += times[i];
                }
                printf("%d\n",ans);
            }
            else if(x == 2)
            {
                scanf("%d",&z);a[y] = z;
                for(int i = y; i >= y/block * block; i --)
                {
                    int temp = i + a[i];
                    if(temp >= n)
                    {
                        nexts[i] = -1;times[i] = 1;
                    }
                    else if(temp >= (i / block + 1) * block)
                    {
                        nexts[i] = temp;times[i] = 1;
                    }
                    else nexts[i] = nexts[temp],times[i] = times[temp] + 1;
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值