結果

問題 No.833 かっこいい電車
ユーザー rpy3cpp
提出日時 2019-05-24 23:05:10
言語 C++14
(gcc 9.2.0)
結果
AC  
実行時間 52 ms
コード長 2,367 Byte
コンパイル時間 1,356 ms
使用メモリ 4,156 KB
最終ジャッジ日時 2020-01-19 06:35:56

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_challenge1.txt AC 40 ms
3,444 KB
99_system_test1.txt AC 4 ms
3,360 KB
99_system_test2.txt AC 0 ms
3,248 KB
99_system_test3.txt AC 0 ms
3,360 KB
99_system_test4.txt AC 0 ms
3,284 KB
99_system_test5.txt AC 0 ms
3,308 KB
99_system_test6.txt AC 0 ms
3,352 KB
99_system_test7.txt AC 4 ms
3,248 KB
sample_01 AC 4 ms
3,340 KB
sample_02 AC 4 ms
3,348 KB
testcase_1.txt AC 40 ms
3,604 KB
testcase_2.txt AC 52 ms
3,824 KB
testcase_3.txt AC 16 ms
3,444 KB
testcase_4.txt AC 12 ms
3,400 KB
testcase_5.txt AC 40 ms
3,776 KB
testcase_6.txt AC 20 ms
3,436 KB
testcase_7.txt AC 16 ms
3,700 KB
testcase_8.txt AC 16 ms
3,384 KB
testcase_9.txt AC 48 ms
3,560 KB
testcase_10.txt AC 12 ms
3,620 KB
testcase_11.txt AC 4 ms
3,372 KB
testcase_12.txt AC 44 ms
3,400 KB
testcase_13.txt AC 24 ms
3,900 KB
testcase_14.txt AC 16 ms
3,560 KB
testcase_15.txt AC 24 ms
3,776 KB
testcase_16.txt AC 48 ms
3,496 KB
testcase_17.txt AC 16 ms
3,712 KB
testcase_18.txt AC 32 ms
3,708 KB
testcase_19.txt AC 28 ms
3,360 KB
testcase_20.txt AC 28 ms
3,484 KB
writer_systemtest_1 AC 24 ms
4,156 KB
writer_systemtest_2 AC 32 ms
3,460 KB
テストケース一括ダウンロード

ソースコード

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

template<class T>
struct BIT{
    vector<T> bit;
    BIT(int n) : bit(n, 0){}
    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 < bit.size(); k |= k + 1) bit[k] += a;}
};

template<class T>
struct BITrange{
    vector<T> bit;
    BITrange(int 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 < bit.size(); k |= k + 1) bit[k] += a;}
    int queryR(int k){
        if (this->range_sum(k, bit.size()) == bit.size() - k) return bit.size();
        int OK = k;
        int NG = bit.size();
        while (OK + 1 < NG){
            int mid = (OK + NG) / 2;
            if (this->range_sum(k, mid) == mid - k){
                OK = mid;
            }else{
                NG = mid;
            }
        }
        return OK;
    }
    int queryL(int k){
        if (this->range_sum(0, k) == k) return 0;
        int OK = k;
        int NG = 0;
        while (NG + 1 < OK){
            int mid = (OK + NG) / 2;
            if (this->range_sum(mid, k) == k - mid){
                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){
        long long a;
        cin >> a;
        bit.add(i, a);
    }
    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