結果

問題 No.833 かっこいい電車
コンテスト
ユーザー 梧桐
提出日時 2026-02-23 20:03:22
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,551 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,013 ms
コンパイル使用メモリ 85,304 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2026-02-23 20:03:26
合計ジャッジ時間 4,308 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <set>
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 100010;

int n, q;
set<int> s;

struct BIT {
    LL tr[N];
    int LowBit(int x) { return x & -x; }
    void Add(int x, int v) {
        for (int i = x; i <= n; i += LowBit(i)) tr[i] += v;
    }
    LL Sum(int x) {
        LL ret = 0LL;
        for (int i = x; i; i -= LowBit(i)) ret += tr[i];
        return ret;
    }
};
BIT bit;

int main() {
    // freopen("bus.in", "r", stdin);
    // freopen("bus.out", "w", stdout);

    scanf("%d%d", &n, &q);
    for (int i = 1, x; i <= n; ++i) {
        scanf("%d", &x);
        bit.Add(i, x);
        if (i <= n - 1) s.insert(i);
    }

    while (q--) {
        int op, x;
        scanf("%d%d", &op, &x);
        if (op == 1) {
            if (s.count(x)) s.erase(x);
        } else if (op == 2) {
            s.insert(x);
        } else if (op == 3) {
            bit.Add(x, 1);
        } else {
            if (s.empty()) {
                printf("%lld\n", bit.Sum(n));
            } else {
                if (*s.rbegin() < x) {
                    printf("%lld\n", bit.Sum(n) - bit.Sum(*s.rbegin()));
                } else {
                    int bg = *s.lower_bound(x);
                    if (bg == *s.begin()) printf("%lld\n", bit.Sum(bg));
                    else {
                        int ed = *prev(s.lower_bound(x));
                        printf("%lld\n", bit.Sum(bg) - bit.Sum(ed));
                    }
                }
            }
        }
    }

    return 0;
}
0