結果

問題 No.833 かっこいい電車
ユーザー lowrlowr
提出日時 2019-09-07 18:54:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,712 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 206,088 KB
実行使用メモリ 8,532 KB
最終ジャッジ日時 2023-09-09 08:00:38
合計ジャッジ時間 7,583 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
5,620 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 120 ms
5,912 KB
testcase_11 AC 160 ms
7,536 KB
testcase_12 AC 49 ms
4,888 KB
testcase_13 AC 33 ms
4,380 KB
testcase_14 AC 130 ms
7,428 KB
testcase_15 AC 64 ms
5,632 KB
testcase_16 AC 53 ms
6,472 KB
testcase_17 AC 43 ms
4,376 KB
testcase_18 AC 144 ms
6,176 KB
testcase_19 AC 52 ms
6,288 KB
testcase_20 AC 15 ms
4,376 KB
testcase_21 AC 115 ms
4,560 KB
testcase_22 AC 90 ms
8,120 KB
testcase_23 AC 59 ms
6,220 KB
testcase_24 AC 90 ms
7,736 KB
testcase_25 AC 136 ms
5,828 KB
testcase_26 AC 63 ms
6,700 KB
testcase_27 AC 100 ms
6,004 KB
testcase_28 AC 70 ms
4,532 KB
testcase_29 AC 85 ms
5,628 KB
testcase_30 AC 95 ms
8,532 KB
testcase_31 AC 134 ms
5,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using i64 = long long;

template <class T = long long int>
class Bit {
    private:
    int len;
    T *arr;
    T init;

    public:
    Bit(int length, T initialValue = 0) : len(length), init(initialValue) {
        arr = new T[length + 1];
        for (int i = 0; i <= length; i++) {
            arr[i] = initialValue;
        }
    }
    ~Bit() {
        delete[] arr;
    }
    void update(int a, T newval) {
        for (int x = a; x <= len; x += x & -x) arr[x] += newval;
    }
    T query(int a) const {
        T ret = init;
        for (int x = a; x > 0; x -= x & -x) ret += arr[x];
        return ret;
    }
};


int main() {
    int n, q;
    std::cin >> n >> q;
    std::set<int> s;
    Bit<> bit(n + 10);
    for (int i = 1; i <= n; i++) {
        i64 in;
        std::cin >> in;
        bit.update(i, in);
        s.insert(i);
    }

    while (q--) {
        int t, x;
        std::cin >> t >> x;
        if (t == 1) {
            auto it = s.find(x + 1);
            if (it != s.end()) s.erase(it);
        } else if (t == 2) {
            auto it = s.find(x + 1);
            if (it == s.end()) s.insert(x + 1);
        } else if (t == 3) {
            bit.update(x, 1);
        } else {
            auto it = s.lower_bound(x), jt = it;
            int lb = it == s.end() || *it > x ? *--it : *it, ub;
            if (jt == s.end()) {
                ub = n + 1;
            } else if (*jt == x) {
                jt++;
                if (jt == s.end()) ub = n + 1;
                else ub = *jt;
            } else {
                ub = *jt;
            }
            std::cout << bit.query(ub - 1) - bit.query(lb - 1) << std::endl;
        }
    }

    return 0;
}
0