結果

問題 No.833 かっこいい電車
ユーザー rpy3cpp
提出日時 2019-05-25 11:06:19
言語 C++14
(gcc 8.3.0)
結果
AC  
実行時間 50 ms
コード長 2,462 Byte
コンパイル時間 1,395 ms
使用メモリ 3,008 KB
最終ジャッジ日時 2019-09-10 03:27:15

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_challenge1.txt AC 41 ms
2,084 KB
99_system_test1.txt AC 3 ms
1,528 KB
99_system_test2.txt AC 3 ms
1,524 KB
99_system_test3.txt AC 3 ms
1,528 KB
99_system_test4.txt AC 3 ms
1,528 KB
99_system_test5.txt AC 3 ms
1,528 KB
99_system_test6.txt AC 3 ms
3,008 KB
99_system_test7.txt AC 3 ms
1,528 KB
sample_01 AC 4 ms
1,524 KB
sample_02 AC 3 ms
1,524 KB
testcase_1.txt AC 40 ms
2,080 KB
testcase_2.txt AC 50 ms
2,344 KB
testcase_3.txt AC 19 ms
1,816 KB
testcase_4.txt AC 14 ms
1,672 KB
testcase_5.txt AC 41 ms
2,344 KB
testcase_6.txt AC 22 ms
1,876 KB
testcase_7.txt AC 17 ms
2,080 KB
testcase_8.txt AC 18 ms
1,612 KB
testcase_9.txt AC 48 ms
2,080 KB
testcase_10.txt AC 17 ms
2,084 KB
testcase_11.txt AC 7 ms
1,740 KB
testcase_12.txt AC 44 ms
1,832 KB
testcase_13.txt AC 27 ms
2,348 KB
testcase_14.txt AC 19 ms
2,080 KB
testcase_15.txt AC 28 ms
2,348 KB
testcase_16.txt AC 45 ms
1,888 KB
testcase_17.txt AC 20 ms
2,084 KB
testcase_18.txt AC 33 ms
2,080 KB
testcase_19.txt AC 26 ms
1,800 KB
testcase_20.txt AC 29 ms
1,840 KB
writer_systemtest_1 AC 28 ms
2,608 KB
writer_systemtest_2 AC 35 ms
2,084 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){
        auto sum_to_k_minus_k = this->sum(k - 1) - k;
        if (this->sum(bit.size() - 1) == bit.size() + sum_to_k_minus_k) return bit.size();
        int OK = k;
        int NG = bit.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){
        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