結果

問題 No.1099 Range Square Sum
ユーザー ぷらぷら
提出日時 2021-05-08 20:38:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,101 bytes
コンパイル時間 2,300 ms
コンパイル使用メモリ 209,528 KB
実行使用メモリ 16,332 KB
最終ジャッジ日時 2023-10-17 06:13:15
合計ジャッジ時間 8,091 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct LazySegmentTree{
    int n;
    vector<T> dat;
    vector<T> dat2;
    vector<T> lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2-1,0);
        dat2.resize(n*2-1,0);
        lazy.resize(n*2-1,0);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == 0) return;
        if(k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+2] += lazy[k];
        }
        dat[k] += lazy[k];
        dat2[k] = dat[k]*dat[k];
        lazy[k] = 0;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2+1, l, (l+r)/2);
            update(a, b, x, k*2+2, (l+r)/2, r);
            dat2[k] = dat2[k*2+1]+dat2[k*2+2];
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return 0;
        }
        else if (a <= l && r <= b) {
            return dat2[k];
        }
        else {
            T vl = query_sub(a, b, k*2+1, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+2, (l+r)/2, r);
            return vl+vr;
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 0, 0, n);
    }
};


int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    LazySegmentTree<long long> tree(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        tree.update(i,i+1,A[i]);
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int f;
        cin >> f;
        if(f == 1) {
            int l,r,x;
            cin >> l >> r >> x;
            l--;
            tree.update(l,r,x);
        }
        else {
            int l,r;
            cin >> l >> r;
            l--;
            cout << tree.query(l,r) << endl;
        }
    }
}
0