結果

問題 No.1099 Range Square Sum
ユーザー ooaiu
提出日時 2024-11-30 15:59:03
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 1,294 bytes
コンパイル時間 3,462 ms
コンパイル使用メモリ 254,304 KB
実行使用メモリ 22,324 KB
最終ジャッジ日時 2024-11-30 15:59:12
合計ジャッジ時間 7,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#include <bits/stdc++.h>

using namespace std;

#define debug(...) (void(0))
#else
#include "algo/debug.h"
#endif

#include <atcoder/lazysegtree>

struct S {
    int64_t sum, sum2, sz;
};
S op(S a, S b) {
    return S{
        a.sum + b.sum,
        a.sum2 + b.sum2,
        a.sz + b.sz,
    };
};
S e() {
    return S{0, 0, 0};
}
using F = int64_t;
F id() { return 0; }
S mapping(F f, S x) {
    return S{
        x.sum + f * x.sz,
        x.sum2 + 2 * f * x.sum + f * f * x.sz,
        x.sz,
    };
}
F composition(F f, F g) {
    return f + g;
}
void solve() {
    int N;
    cin >> N;
    vector<S> A(N);
    for (int i = 0; i < N; i++) {
        int64_t a;
        cin >> a;
        A[i] = S{a, a * a, 1};
    }
    atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> seg(A);
    int Q;
    cin >> Q;
    while (Q--) {
        int o;
        cin >> o;
        int l, r;
        cin >> l >> r;
        if (o == 1) {
            int64_t x;
            cin >> x;
            seg.apply(--l, r, x);
        } else {
            S ans = seg.prod(--l, r);
            cout << ans.sum2 << endl;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int tt = 1;
    // std::cin >> tt;
    while (tt--) {
        solve();
    }
}
0