CF 13E Holes 【块状链表】

89 篇文章 0 订阅
42 篇文章 0 订阅

题目描述:

一条直线上n个点,每个点有个“弹力”,可以把当前位置x上面的ball弹到x+a[x]上面。

两种操作

0. 修改a处的弹力值,编程b

1. 询问a点的ball经过多少次能跳出n个点外(就是出界了)。。。。求出弹跳的次数和最后落脚的点。



块状链表就是用来暴力模拟的。

用块状链表可以把时间复杂度从O(n)变成O(sqrt(n))。

这道题目的复杂度为O(m*sqrt(n))。

具体实现还是直接看代码容易理解……


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define N 100100
int block[N], c[N], next[N], a[N], end[N];
int n, m, block_size;

void make(int x) {
    int y = x + a[x];
    if (y <= n && block[x] == block[y]) {
        c[x] = c[y] + 1;
        next[x] = next[y];
        end[x] = end[y];
    } else {
        end[x] = x;
        c[x] = 1;
        next[x] = y;
    }
}
void change(int x, int y) {
    a[x] = y;
    for (y=x; y && block[y]==block[x]; y--) make(y);
}

void ask(int x) {
    int ret = 0, ans;
    for (; x<=n; x=next[x]) {
        ret += c[x];
        ans = end[x];
    }
    cout << ans << " " << ret << endl;
}
int main() {

    cin >> n >> m;
    block_size = sqrt(n);

    for (int i=1; i<=n; i++) {
        cin >> a[i];
        block[i] = i/block_size;
    }
    for (int i=n; i; i--) make(i);

    int op, x, y;
    while (m--) {
        cin >> op;
        if (op == 0) {
            cin >> x >> y;
            change(x, y);
        } else {
            cin >> x;
            ask(x);
        }
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值