結果

問題 No.1099 Range Square Sum
ユーザー betrue12betrue12
提出日時 2020-06-26 21:54:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 4,418 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 207,648 KB
実行使用メモリ 18,620 KB
最終ジャッジ日時 2023-09-18 04:19:27
合計ジャッジ時間 7,363 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 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 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 350 ms
18,560 KB
testcase_22 AC 349 ms
18,368 KB
testcase_23 AC 351 ms
18,356 KB
testcase_24 AC 349 ms
18,540 KB
testcase_25 AC 350 ms
18,320 KB
testcase_26 AC 283 ms
18,620 KB
testcase_27 AC 280 ms
18,564 KB
testcase_28 AC 289 ms
18,352 KB
testcase_29 AC 285 ms
18,608 KB
testcase_30 AC 285 ms
18,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename F, typename T, typename Func_mf, typename Func_op, typename Func_mv>
struct LazySegtree {
    int n, n_org;
    vector<T> dat;
    vector<F> laz;
    Func_mf merge_functions;
    Func_op operate;
    Func_mv merge_values;
    F fe;
    T te;

    LazySegtree(){}
    LazySegtree(int n_org,
                Func_mf merge_functions,
                Func_op operate,
                Func_mv merge_values,
                F fe, T te):
                n_org(n_org),
                merge_functions(merge_functions),
                operate(operate),
                merge_values(merge_values),
                fe(fe), te(te){
        n = 1;
        while(n < n_org) n <<= 1;
        dat.assign(2*n-1, te);
        laz.assign(2*n-1, fe);
    }

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

    void eval(int k, int w){
        if(laz[k] == fe) return;
        operate(dat[k], laz[k], w);
        if(k < n-1){
            merge_functions(laz[2*k+1], laz[k]);
            merge_functions(laz[2*k+2], laz[k]);
        }
        laz[k] = fe;
    }

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

    void update(int a, int b, F x, int k, int lb, int rb){
        eval(k, rb-lb);
        if(b <= lb || rb <= a) return;
        if(a <= lb && rb <= b){
            merge_functions(laz[k], x);
            eval(k, rb-lb);
        }else{
            int mb = (lb+rb)>>1;
            update(a, b, x, 2*k+1, lb, mb);
            update(a, b, x, 2*k+2, mb, rb);
            dat[k] = merge_values(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 lb, int rb){
        eval(k, rb-lb);
        if(rb<=a || b<=lb) return te;
        if(a<=lb && rb<=b) return dat[k];
        int mb = (lb+rb)>>1;
        T vl = query(a, b, 2*k+1, lb, mb);
        T vr = query(a, b, 2*k+2, mb, rb);
        return merge_values(vl, vr);
    }

    // [S, t] が条件checkを満たす最大のtを求める
	template<typename Func_ck>
    int lower_bound(int S, Func_ck check){
        T val = get_between(S, S);
        int k = S+n-1;
        int lb = S, rb = S+1;
        while(true){
            while(k%2){
                k = (k-1)/2;
                rb += rb-lb;
            }
			int w = rb-lb;
            eval(k, w);
            T val2 = merge_values(val, dat[k]);
            if(check(val2)){
                if(rb == n) return n_org-1;
                val = val2;
                k++;
				lb += w;
				rb += w;
            }else{
                break;
            }
        }
        while(k<n-1){
            int wh = (rb-lb)>>1;
            eval(2*k+1, wh);
            eval(2*k+2, wh);
            T val2 = merge_values(val, dat[2*k+1]);
            if(check(val2)){
                val = val2;
                k = 2*k+2;
                lb += wh;
            }else{
                k = 2*k+1;
                rb -= wh;
            }
        }
        return min(lb, n_org)-1;
    }
};

auto make_segtree = [](int N){
    using F = int64_t;
    // 2乗和、1乗和
    using T = pair<int64_t, int64_t>;
    auto merge_functions = [](F& f, F& g){
        f += g;
    };
    auto operate = [](T& v, F& f, int w){
        v.first += 2*v.second*f + w*f*f;
        v.second += w*f;
    };
    auto merge_values = [](T& a, T& b){
        return make_pair(a.first+b.first, a.second+b.second);
    };
    F fe = 0;
    T te = {0, 0};
    return LazySegtree<F, T, decltype(merge_functions), decltype(operate), decltype(merge_values)>
        (N, merge_functions, operate, merge_values, fe, te);
};

int main(){
    int N;
    cin >> N;
    vector<pair<int64_t, int64_t>> A(N);
    for(int i=0; i<N; i++){
        int64_t a;
        cin >> a;
        A[i] = {a*a, a};
    }
    auto st = make_segtree(N);
    st.build(A);

    int Q;
    cin >> Q;
    while(Q--){
        int t;
        cin >> t;
        if(t == 1){
            int l, r, x;
            cin >> l >> r >> x;
            st.update_between(l-1, r-1, x);
        }else{
            int l, r;
            cin >> l >> r;
            int64_t ans = st.get_between(l-1, r-1).first;
            cout << ans << endl;
        }
    }
    return 0;
}
0