結果

問題 No.833 かっこいい電車
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-25 19:52:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,568 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 169,956 KB
実行使用メモリ 814,076 KB
最終ジャッジ日時 2023-09-14 21:03:13
合計ジャッジ時間 5,799 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 11 ms
4,380 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 13 ms
4,380 KB
testcase_17 RE -
testcase_18 MLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

template<class T>
struct BIT{
    int size;
    vector<T> bit;
    BIT(int n) : size(n), bit(n, 0){}
    void init() {for (int i = 0; i < size - 1; ++i) {
        if (i | (i + 1) < size) bit[i | (i + 1)] += bit[i];
        }
    }
    T range_sum(int i, int j) {return sum(j) - sum(i - 1);}
    // return sum of v[i] to v[j] including both ends.
    T sum(int j){
        T s = 0;
        for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j];
        return s;
    }
    void add(int k, T a) {for (; k < size; k |= k + 1) bit[k] += a;}
};

template<class T>
struct BITrange{
    int size;
    vector<T> bit;
    BITrange(int n) : size(n), bit(n, 0){}
    T range_sum(int i, int j) {return sum(j - 1) - sum(i - 1);}
    // return sum of v[i] to v[j - 1].
    T sum(int j){
        T s = 0;
        for (; j >= 0; j = (j & (j + 1)) - 1) s += bit[j];
        return s;
    }
    void add(int k, T a) {for (; k < size; k |= k + 1) bit[k] += a;}
    int queryR(int k){
        auto sum_to_k_minus_k = this->sum(k - 1) - k;
        if (this->sum(size - 1) == size + sum_to_k_minus_k) return size;
        int OK = k;
        int NG = size;
        while (OK + 1 < NG){
            int mid = (OK + NG) / 2;
            if (this->sum(mid - 1) == mid + sum_to_k_minus_k){
                OK = mid;
            }else{
                NG = mid;
            }
        }
        return OK;
    }
    int queryL(int k){
        auto tmp = k - this->sum(k - 1); 
        if (tmp == 0) return 0;
        int OK = k;
        int NG = 0;
        while (NG + 1 < OK){
            int mid = (OK + NG) / 2;
            if (mid - this->sum(mid - 1) == tmp){
                OK = mid;
            }else{
                NG = mid;
            }
        }
        return OK;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, Q;
    cin >> N >> Q;
    BIT<long long> bit(N);
    for (int i = 0; i < N; ++i) cin >> bit.bit[i];
    bit.init();
    BITrange<int> bitr(N);
    while (Q--){
        int q, x;
        cin >> q >> x;
        if (q == 1){
            if (bitr.range_sum(x - 1, x) == 0){
                bitr.add(x - 1, 1);
            }
        }else if (q == 2){
            if (bitr.range_sum(x - 1, x) == 1){
                bitr.add(x - 1, -1);
            }
        }else if (q == 3){
            bit.add(x - 1, 1LL);
        }else if (q == 4){
            int R = bitr.queryR(x - 1);
            int L = bitr.queryL(x - 1);
            cout << bit.range_sum(L, R) << '\n';
        }
    }
    return 0;
}
0