codeforces 13E . Holes 分块

题目链接

 

nextt数组表示这个位置的下一个位置。

cnt数组表示这个位置 i 到nextt[i]可以弹几次。

end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里。

 

然后就看代码吧。

 

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5 + 5;
int a[maxn], cnt[maxn], nextt[maxn], pos[maxn], block[maxn], n;
void update(int x, int y) {
    if(y >= n) {
        nextt[x] = n+1;
        cnt[x] = 1;
        pos[x] = x;
    } else {
        if(block[x] == block[y]) {
            nextt[x] = nextt[y];
            cnt[x] = cnt[y] + 1;
        } else {
            nextt[x] = y;
            cnt[x] = 1;
        }
        pos[x] = pos[y];
    }
}
void query(int x) {
    int i, ans = 0;
    for(i = x; ; i = nextt[i]) {
        ans += cnt[i];
        if(nextt[i] >= n) {
            i = pos[i];
            break;
        }
    }
    printf("%d %d\n", i+1, ans);
}
int main()
{
    int m, sign, x, y;
    cin>>n>>m;
    int BLOCK = sqrt(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
        block[i] = i / BLOCK;
    }
    for (int i = n-1; i >= 0; i--) {
        update(i, i + a[i]);
    }
    while (m--) {
        scanf("%d", &sign);
        if(sign) {
            scanf("%d", &x);
            query(x-1);
        } else {
            scanf("%d%d", &x, &y);
            x--;
            a[x] = y;
            int l = block[x] * BLOCK;
            int r = l + BLOCK;
            r = min(n, r);
            for(int i = r-1; i >= l; i--) {
                update(i, i + a[i]);
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yohaha/p/5283316.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值