結果

問題 No.833 かっこいい電車
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-25 14:45:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,553 bytes
コンパイル時間 1,568 ms
コンパイル使用メモリ 169,532 KB
実行使用メモリ 5,276 KB
最終ジャッジ日時 2023-09-14 21:02:55
合計ジャッジ時間 5,982 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 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 12 ms
4,376 KB
testcase_14 RE -
testcase_15 AC 19 ms
4,380 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 14 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 16 ms
4,436 KB
testcase_27 RE -
testcase_28 AC 23 ms
4,380 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 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