結果

問題 No.833 かっこいい電車
ユーザー Manuel1024Manuel1024
提出日時 2021-12-20 04:07:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,827 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 72,820 KB
実行使用メモリ 4,756 KB
最終ジャッジ日時 2023-10-13 19:25:43
合計ジャッジ時間 6,236 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
4,368 KB
testcase_01 AC 2 ms
4,476 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 87 ms
4,352 KB
testcase_11 AC 108 ms
4,368 KB
testcase_12 AC 36 ms
4,348 KB
testcase_13 AC 28 ms
4,348 KB
testcase_14 AC 89 ms
4,392 KB
testcase_15 AC 47 ms
4,348 KB
testcase_16 AC 35 ms
4,352 KB
testcase_17 AC 38 ms
4,348 KB
testcase_18 AC 106 ms
4,352 KB
testcase_19 AC 34 ms
4,348 KB
testcase_20 AC 10 ms
4,348 KB
testcase_21 AC 95 ms
4,348 KB
testcase_22 AC 58 ms
4,352 KB
testcase_23 AC 39 ms
4,368 KB
testcase_24 AC 58 ms
4,352 KB
testcase_25 AC 102 ms
4,352 KB
testcase_26 AC 41 ms
4,356 KB
testcase_27 AC 70 ms
4,352 KB
testcase_28 AC 56 ms
4,352 KB
testcase_29 AC 63 ms
4,356 KB
testcase_30 AC 60 ms
4,756 KB
testcase_31 AC 117 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;

template <typename T>
struct FenwickTree{
    const int n;
    vector<T> arr;
    FenwickTree() = default;
    FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}

    void add(int ind, T x){
        for(int i = ind+1; i <= n; i += i & (-i)){
            arr[i] += x;
        }
    }

    // [0, end)
    T sum_sub(int end){
        T res = 0;
        for(int i = end; i > 0; i -= i & (-i)){
            res += arr[i];
        }
        return res;
    }

    // [start, end)
    T sum(int start, int end){
        return sum_sub(end) - sum_sub(start);
    }

    int lower_bound(T w){
        if(w <= 0) return 0;
        int x = 0;
        int k = 1 << 30;
        while(k > n) k /= 2;
        for(; k > 0; k /= 2){
            if(x+k <= n && arr[x+k] < w){
                w -= arr[x+k];
                x += k;
            }
        }
        return x;
    }
};

int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for(auto &it: a) cin >> it;
    FenwickTree<ll> sum(n);
    for(int i = 0; i < n; i++) sum.add(i, a[i]);
    FenwickTree<int> range(n);
    for(int i = 1; i < n; i++) range.add(i, 1);
    while(q--){
        int op, x;
        cin >> op >> x;
        --x;
        if(op == 1){
            if(range.sum(x+1, x+2) == 1) range.add(x+1, -1);
        }else if(op == 2){
            if(range.sum(x+1, x+2) == 0) range.add(x+1, 1);
        }else if(op == 3){
            sum.add(x, 1);
        }else{
            // cout << range.sum(0, 1) << " " << range.sum(1, 2) << " ";
            int r = range.lower_bound(range.sum(0, x+1)+1);
            int l = range.lower_bound(range.sum(0, x+1));
            // cout << l << " " << r << " ";
            cout << sum.sum(l, r) << '\n';
        }
    }
    return 0;
}
0