zoukankan      html  css  js  c++  java
  • 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
    ", 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;
    }
  • 相关阅读:
    bzoj2763: [JLOI2011]飞行路线(分层图spfa)
    8.20noip模拟题
    8.19noip模拟题
    1046: [HAOI2007]上升序列(dp)
    bzoj1079: [SCOI2008]着色方案(dp)
    逆序对
    P1966 火柴排队(逆序对)
    NOIP 2015 DAY2
    8.15学校模拟
    差分
  • 原文地址:https://www.cnblogs.com/yohaha/p/5283316.html
Copyright © 2011-2022 走看看