結果

問題 No.833 かっこいい電車
ユーザー Y17Y17
提出日時 2019-05-24 23:55:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 478 ms / 2,000 ms
コード長 3,328 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 162,784 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2023-09-14 20:59:01
合計ジャッジ時間 7,559 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 478 ms
5,500 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 241 ms
5,448 KB
testcase_11 AC 322 ms
7,848 KB
testcase_12 AC 87 ms
5,404 KB
testcase_13 AC 67 ms
4,380 KB
testcase_14 AC 238 ms
7,808 KB
testcase_15 AC 110 ms
5,496 KB
testcase_16 AC 56 ms
5,752 KB
testcase_17 AC 97 ms
4,380 KB
testcase_18 AC 300 ms
5,396 KB
testcase_19 AC 62 ms
5,448 KB
testcase_20 AC 12 ms
4,380 KB
testcase_21 AC 268 ms
4,404 KB
testcase_22 AC 118 ms
7,768 KB
testcase_23 AC 77 ms
5,668 KB
testcase_24 AC 122 ms
7,720 KB
testcase_25 AC 298 ms
5,476 KB
testcase_26 AC 79 ms
7,540 KB
testcase_27 AC 189 ms
5,404 KB
testcase_28 AC 149 ms
4,376 KB
testcase_29 AC 169 ms
5,400 KB
testcase_30 AC 65 ms
7,764 KB
testcase_31 AC 206 ms
5,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long

template <class type> class SegmentTree{
    public:
        int size;
        vector<type> tree;
        type def;
        type (*marge)(type, type);

        SegmentTree(int n, type d, type (*m)(type, type)){
            size = 1;
            def = d;
            marge = m;
            n++;

            while(size < n){
                size *= 2;
            }

            tree.resize(2*size);

            for(int i = 0;i < size;i++){
                tree[size-1+i] = def;
            }

            for(int i = size-2;i >= 0;i--){
                tree[i] = marge(tree[i*2+1], tree[i*2+2]);
            }

            return;
        }

        SegmentTree(int n, type array[], type d, type (*m)(type, type)){
            size = 1;
            def = d;
            marge = m;
            n++;

            while(size < n){
                size *= 2;
            }

            tree.resize(2*size);

            for(int i = 0;i < size;i++){
                if(i < n) tree[size-1+i] = array[i];
                else tree[size-1+i] = def;
            }

            for(int i = size-2;i >= 0;i--){
                tree[i] = marge(tree[i*2+1], tree[i*2+2]);
            }

            return;
        }

        void update(int i, type val){
            i = size-1+i;
            tree[i] = val;

            while(i > 0){
                i = (i-1)/2;
                tree[i] = marge(tree[i*2+1], tree[i*2+2]);
            }

            return;
        }

        void add(int i, type val){
            i = size-1+i;
            tree[i] = tree[i] + val;

            while(i > 0){
                i = (i-1)/2;
                tree[i] = marge(tree[i*2+1], tree[i*2+2]);
            }

            return;
        }

        type get(int a, int b, int k, int l, int r){
            if(r <= a || b <= l) return def;
            if(a <= l && r <= b) return tree[k];

            type vl = get(a, b, 2*k+1, l, (l+r)/2);
            type vr = get(a, b, 2*k+2, (l+r)/2, r);

            return marge(vl, vr);
        }

        type get(int a, int b){
            return get(a, b, 0, 0, size);
        }
};

int marge(int a, int b){
    return a + b;
}

signed main(){
    int n, q;
    cin >> n >> q;

    int a[100010];

    for(int i = 0;i < n;i++) cin >> a[i];

    SegmentTree<int> seg1(n, 0, marge);
    SegmentTree<int> seg2(n, a, 0, marge);

    int t, x;
    for(int i = 0;i < q;i++){
        cin >> t >> x;
        x--;
        if(t == 1){
            seg1.update(x, 1);
        }else if(t == 2){
            seg1.update(x, 0);
        }else if(t == 3){
            seg2.add(x, 1);
        }else{
            int l = x, r = n;
            while(r - l > 1){
                int mid = (r+l)/2;
                if(seg1.get(x, mid) == (mid-x)){
                    l = mid;
                }else{
                    r = mid;
                }
            }

            int ans = seg2.get(x, l+1);

            l = -1, r = x;
            while(r - l > 1){
                int mid = (r+l)/2;
                if(seg1.get(mid, x) == (x-mid)){
                    r = mid;
                }else{
                    l = mid;
                }

            }
            ans += seg2.get(r, x);

            cout << ans << endl;

        }
    }

    return 0;
}
0