結果

問題 No.833 かっこいい電車
ユーザー merom686merom686
提出日時 2019-05-25 11:17:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,697 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 79,072 KB
実行使用メモリ 4,556 KB
最終ジャッジ日時 2023-09-14 21:02:40
合計ジャッジ時間 2,847 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 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,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 28 ms
4,376 KB
testcase_11 AC 36 ms
4,376 KB
testcase_12 AC 13 ms
4,380 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 31 ms
4,376 KB
testcase_15 AC 16 ms
4,376 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 12 ms
4,380 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 28 ms
4,376 KB
testcase_22 AC 20 ms
4,380 KB
testcase_23 AC 15 ms
4,376 KB
testcase_24 AC 20 ms
4,380 KB
testcase_25 AC 32 ms
4,380 KB
testcase_26 AC 15 ms
4,376 KB
testcase_27 AC 24 ms
4,376 KB
testcase_28 AC 18 ms
4,376 KB
testcase_29 AC 20 ms
4,376 KB
testcase_30 AC 25 ms
4,556 KB
testcase_31 AC 25 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

template <class T>
struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, T v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    T sum(int k) {
        T s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    //合計がs以上になる最小のk n個の合計より大きければn+1が返る
    int lower_bound(T s) {
        if (s <= 0) return 0;
        int x = 0, w = 1;
        while (w << 1 <= n) w <<= 1;
        do {
            int k = x + w;
            if (k <= n && b[k] < s) {
                s -= b[k];
                x += w;
            }
        } while (w >>= 1);
        return x + 1;
    }
    vector<T> b;
    int n;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    BIT<ll> ba(n);
    BIT<int> bb(n);
    vector<char> c(n - 1, 0);

    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;

        ba.add(i, a);
        bb.add(i, 1);
    }

    for (int h = 0; h < q; h++) {
        int t, x;
        cin >> t >> x;
        x--;

        if (t == 1) {
            if (c[x] == 0) bb.add(x, -1), c[x] = 1;

        } else if (t == 2) {
            if (c[x] != 0) bb.add(x, +1), c[x] = 0;

        } else if (t == 3) {
            ba.add(x, 1);

        } else if (t == 4) {
            int s = bb.sum(x);
            int k0 = bb.lower_bound(s);
            int k1 = bb.lower_bound(s + 1);
            cout << ba.sum(k1) - ba.sum(k0) << '\n';
        }
    }

    return 0;
}
0