結果

問題 No.833 かっこいい電車
ユーザー Shuz*Shuz*
提出日時 2019-04-20 23:05:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 2,319 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 169,060 KB
実行使用メモリ 4,592 KB
最終ジャッジ日時 2023-09-14 16:06:15
合計ジャッジ時間 4,820 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 91 ms
4,380 KB
testcase_11 AC 115 ms
4,376 KB
testcase_12 AC 38 ms
4,376 KB
testcase_13 AC 30 ms
4,380 KB
testcase_14 AC 93 ms
4,376 KB
testcase_15 AC 47 ms
4,380 KB
testcase_16 AC 35 ms
4,380 KB
testcase_17 AC 40 ms
4,380 KB
testcase_18 AC 107 ms
4,376 KB
testcase_19 AC 35 ms
4,376 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 99 ms
4,376 KB
testcase_22 AC 59 ms
4,592 KB
testcase_23 AC 41 ms
4,384 KB
testcase_24 AC 59 ms
4,376 KB
testcase_25 AC 107 ms
4,376 KB
testcase_26 AC 42 ms
4,376 KB
testcase_27 AC 75 ms
4,380 KB
testcase_28 AC 58 ms
4,376 KB
testcase_29 AC 64 ms
4,376 KB
testcase_30 AC 60 ms
4,592 KB
testcase_31 AC 121 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0, _##i = (n); i < _##i; ++i)

struct BIT {
    vector<ll> data;

    BIT() {}
    BIT(int N) : data(N + 1) {
        fill(data.begin(), data.end(), 0);
        data[0] = N;
    }

    void add(int pos, int val) {
        while (pos <= data[0]) {
            data[pos] += val;
            pos += pos & -pos;
        }
    }
    ll sum(int pos) {
        if (pos <= 0) return 0;
        ll res = 0;
        while (pos > 0) {
            res += data[pos];
            pos -= pos & -pos;
        }
        return res;
    }

    int lower_bound(int val) {
        if (val <= 0) return 0;
        int index = 0;
        for (int d = 1 << int(log2(data[0])); d > 0; d /= 2) {
            if (index + d <= data[0] && data[index + d] < val) {
                val -= data[index + d];
                index += d;
            }
        }
        return index;
    }
};

struct GCT {
    int size;
    BIT bit;

    GCT(int N) : bit(N), size(N) {
        for (int i = 1; i <= bit.data[0]; i++) {
            bit.data[i] = i & -i;
        }
    }

    int group_index(int index) { return bit.sum(index + 1) - 1; }
    int dist(int x, int y) { return abs(group_index(x) - group_index(y)); }

    int begin(int group_index) { return bit.lower_bound(group_index + 1); }
    int end(int group_index) { return bit.lower_bound(group_index + 2); }
    int group_size(int group_index) {
        return end(group_index) - begin(group_index);
    }

    void group(int index) {
        int s = -(bit.sum(index + 2) - bit.sum(index + 1));
        bit.add(index + 2, s);
        size += s;
    }
    void cut(int index) {
        int s = !(bit.sum(index + 2) - bit.sum(index + 1));
        bit.add(index + 2, s);
        size += s;
    }
};

int main() {
    int N, Q;
    cin >> N >> Q;
    int A, B;
    BIT bit(N);
    GCT gct(N);
    rep(i, N) cin >> A, bit.add(i + 1, A);
    rep(i, Q) {
        cin >> A >> B;
        if (A == 1) {
            gct.group(B - 1);
        } else if (A == 2) {
            gct.cut(B - 1);
        } else if (A == 3) {
            bit.add(B, 1);
        } else {
            int index = gct.group_index(B - 1);
            cout << bit.sum(gct.end(index)) - bit.sum(gct.begin(index)) << endl;
        }
    }
}
0