結果

問題 No.833 かっこいい電車
ユーザー betrue12betrue12
提出日時 2019-05-24 22:17:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 5,083 bytes
コンパイル時間 2,108 ms
コンパイル使用メモリ 184,204 KB
実行使用メモリ 8,388 KB
最終ジャッジ日時 2023-09-14 20:52:41
合計ジャッジ時間 7,722 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
5,744 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 213 ms
5,700 KB
testcase_11 AC 284 ms
8,376 KB
testcase_12 AC 88 ms
5,488 KB
testcase_13 AC 59 ms
4,380 KB
testcase_14 AC 227 ms
8,140 KB
testcase_15 AC 116 ms
5,704 KB
testcase_16 AC 95 ms
5,916 KB
testcase_17 AC 76 ms
4,380 KB
testcase_18 AC 259 ms
5,680 KB
testcase_19 AC 92 ms
5,632 KB
testcase_20 AC 24 ms
4,432 KB
testcase_21 AC 209 ms
4,380 KB
testcase_22 AC 159 ms
8,368 KB
testcase_23 AC 106 ms
5,996 KB
testcase_24 AC 160 ms
8,268 KB
testcase_25 AC 246 ms
5,708 KB
testcase_26 AC 115 ms
8,080 KB
testcase_27 AC 175 ms
5,752 KB
testcase_28 AC 125 ms
4,376 KB
testcase_29 AC 150 ms
5,824 KB
testcase_30 AC 284 ms
8,388 KB
testcase_31 AC 269 ms
5,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct SetF{
    typedef function<void(T& a, T b)> func2;
    func2 f;
    T e;
    SetF(){}
    SetF(func2 f, T e):f(f), e(e){}
};
template<typename T>
SetF<T> setf_update([](T& a, T b){ a = b; }, 0);
template<typename T>
SetF<T> setf_add([](T& a, T b){ a += b; }, 0);

template<typename T>
struct GetF{
    typedef function<T(T a)> func1;
    typedef function<T(T a, int width)> func_int;
    typedef function<T(T a, T b)> func2;
    func2 f;
    func_int expand;
    T e;
    GetF(){}
    GetF(func2 f, func_int expand, T e):f(f), expand(expand), e(e){}
};
template<typename T>
GetF<T> getf_min([](T a, T b){ return min(a, b); }, [](T a, int w){ return a;}, (T)1e9);
template<typename T>
GetF<T> getf_max([](T a, T b){ return max(a, b); }, [](T a, int w){ return a;}, (T)-1e9);
template<typename T>
GetF<T> getf_sum([](T a, T b){ return a+b; }, [](T a, int w){ return w*a;}, 0);

template<typename T>
struct LazySegtree {
    int n;
    vector<T> dat, laz;
    vector<bool> lazflag;
    SetF<T> setf;
    GetF<T> getf;

    LazySegtree(){}
    LazySegtree(int n_input, SetF<T> setf_input, GetF<T> getf_input){ initialize(n_input, setf_input, getf_input); }
    void initialize(int n_input, SetF<T> setf_input, GetF<T> getf_input){
        n = 1;
        while(n < n_input) n <<= 1;
        setf = setf_input;
        getf = getf_input;
        dat.resize(2*n-1, getf.e);
        laz.resize(2*n-1, setf.e);
        lazflag.resize(2*n-1, false);
    }

    void build(vector<T>& A){
        for(int k=0; k<A.size(); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = getf.f(dat[2*k+1], dat[2*k+2]);
    }

    void eval(int k, int l, int r){
        if(!lazflag[k]) return;
        setf.f(dat[k], getf.expand(laz[k], r-l));
        if(r-l > 1){
            setf.f(laz[2*k+1], laz[k]);
            setf.f(laz[2*k+2], laz[k]);
            lazflag[2*k+1] = lazflag[2*k+2] = true;
        }
        laz[k] = setf.e;
        lazflag[k] = false;
    }

    void update_between(int a, int b, T x){
        update(a, b+1, x, 0, 0, n);
    }

    void update(int a, int b, T x, int k, int l, int r){
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            setf.f(laz[k], x);
            lazflag[k] = true;
            eval(k, l, r);
        }else{
            update(a, b, x, 2*k+1, l, (l+r)/2);
            update(a, b, x, 2*k+2, (l+r)/2, r);
            dat[k] = getf.f(dat[2*k+1], dat[2*k+2]);
        }
    }

    void force_set(int a, T x){
        get_between(a, a);
        int k = a+n-1;
        dat[k] = x;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = getf.f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get_between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        eval(k, l, r);
        if(r<=a || b<=l) return getf.e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return getf.f(vl, vr);
    }
};

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0){
        initialize(n);
    }

    void initialize(int nin){
        n = nin;
        dat.resize(n);
        for(int i = 0; i<n; i++) dat[i] = 0;
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

    T sum_between(int i, int j){
        return sum(j) - sum(i-1);
    }

    void plus(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};


int main(){
    int N, Q, A[100000];
    cin >> N >> Q;
    for(int i=0; i<N; i++) cin >> A[i];

    BIT<int64_t> bit(N);
    for(int i=0; i<N; i++) bit.plus(i, A[i]);
    LazySegtree<int> stl(N, setf_update<int>, getf_max<int>), str(N, setf_update<int>, getf_max<int>);
    for(int i=0; i<N; i++){
        stl.update_between(i, i, i);
        str.update_between(i, i, i);
    }

    while(Q--){
        int t, x;
        cin >> t >> x;
        t--; x--;
        if(t == 0){
            int l = stl.get_between(x, x), r = str.get_between(x+1, x+1);
            stl.update_between(l, r, l);
            str.update_between(l, r, r);
        }else if(t == 1){
            int l = stl.get_between(x, x), r = str.get_between(x+1, x+1);
            str.update_between(l, x, x);
            stl.update_between(x+1, r, x+1);
        }else if(t == 2){
            bit.plus(x, 1);
        }else{
            int l = stl.get_between(x, x), r = str.get_between(x, x);
            int64_t ans = bit.sum_between(l, r);
            cout << ans << endl;
        }
    }
    return 0;
}
0