結果

問題 No.833 かっこいい電車
ユーザー rpy3cpp
提出日時 2019-05-25 14:45:40
言語 C++14
(gcc 8.3.0)
結果
RE   .
実行時間 -
コード長 2,553 Byte
コンパイル時間 1,419 ms
使用メモリ 8,924 KB
最終ジャッジ日時 2019-10-20 11:44:15

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_challenge1.txt RE -
99_system_test1.txt AC 3 ms
8,920 KB
99_system_test2.txt AC 3 ms
6,872 KB
99_system_test3.txt AC 3 ms
6,876 KB
99_system_test4.txt AC 3 ms
6,872 KB
99_system_test5.txt AC 4 ms
6,872 KB
99_system_test6.txt AC 4 ms
6,872 KB
99_system_test7.txt AC 3 ms
6,876 KB
sample_01 AC 3 ms
6,872 KB
sample_02 AC 4 ms
8,924 KB
testcase_1.txt RE -
testcase_2.txt RE -
testcase_3.txt RE -
testcase_4.txt AC 14 ms
8,920 KB
testcase_5.txt RE -
testcase_6.txt RE -
testcase_7.txt RE -
testcase_8.txt RE -
testcase_9.txt RE -
testcase_10.txt RE -
testcase_11.txt RE -
testcase_12.txt RE -
testcase_13.txt RE -
testcase_14.txt RE -
testcase_15.txt RE -
testcase_16.txt RE -
testcase_17.txt RE -
testcase_18.txt RE -
testcase_19.txt RE -
testcase_20.txt RE -
writer_systemtest_1 RE -
writer_systemtest_2 RE -
テストケース一括ダウンロード

ソースコード

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

template<class T>
struct BIT{
    vector<T> bit;
    BIT(int n) : bit(n, 0){}
    BIT(const vector<T> &src) : bit(src) {
        for (int k = 0; k < src.size() - 1; ++k) bit[k | (k + 1)] += bit[k];
    }
    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;
    vector<long long> As(N, 0);
    for (auto &a : As) cin >> a;
    BIT<long long> bit(As);
    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