結果

問題 No.1099 Range Square Sum
ユーザー siro53siro53
提出日時 2020-06-27 01:27:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 4,420 bytes
コンパイル時間 2,771 ms
コンパイル使用メモリ 215,016 KB
実行使用メモリ 35,236 KB
最終ジャッジ日時 2023-09-18 13:58:34
合計ジャッジ時間 7,484 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 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 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,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 327 ms
35,236 KB
testcase_22 AC 324 ms
35,164 KB
testcase_23 AC 324 ms
35,060 KB
testcase_24 AC 325 ms
35,064 KB
testcase_25 AC 332 ms
35,060 KB
testcase_26 AC 210 ms
35,180 KB
testcase_27 AC 211 ms
35,168 KB
testcase_28 AC 209 ms
35,180 KB
testcase_29 AC 209 ms
35,120 KB
testcase_30 AC 207 ms
35,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

template <typename T, typename E = T> struct LazySegmentTree {
    using F = function<T(T, T)>;
    using G = function<T(T, E)>;
    using H = function<E(E, E)>;
    using P = function<E(E, int)>;
    F f;
    G g;
    H h;
    P p;
    T d1;
    E d0;
    int n;
    vector<T> node;
    vector<E> lazy;

    LazySegmentTree(
        int sz, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(),
        P p = [](E a, int b) { return a; })
        : f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
        init(sz);
        if(sz == (int)v.size()) {
            build(sz, v);
        }
    }

    void init(int sz) {
        n = 1;
        while(n < sz) {
            n *= 2;
        }
        node.clear();
        node.resize(2 * n - 1, d1);
        lazy.clear();
        lazy.resize(2 * n - 1, d0);
    }

    void build(int sz, vector<T> v) {
        for(int i = 0; i < sz; i++) {
            node[i + n - 1] = v[i];
        }
        for(int i = n - 2; i >= 0; i--) {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    inline void eval(int len, int now) {
        if(lazy[now] == d0) {
            return;
        }
        if(2 * now + 1 < 2 * n - 1) {
            lazy[2 * now + 1] = h(lazy[2 * now + 1], lazy[now]);
            lazy[2 * now + 2] = h(lazy[2 * now + 2], lazy[now]);
        }
        node[now] = g(node[now], p(lazy[now], len));
        lazy[now] = d0;
    }

    T update(int a, int b, E x, int now = 0, int l = 0, int r = -1) {
        if(r < 0) {
            r = n;
        }
        eval(r - l, now);
        if(r <= a || b <= l) {
            return node[now];
        }
        if(a <= l && r <= b) {
            lazy[now] = h(lazy[now], x);
            return g(node[now], p(lazy[now], r - l));
        }
        T vl = update(a, b, x, 2 * now + 1, l, (l + r) / 2);
        T vr = update(a, b, x, 2 * now + 2, (l + r) / 2, r);
        return node[now] = f(vl, vr);
    }

    T query(int a, int b, int now = 0, int l = 0, int r = -1) {
        if(r < 0) {
            r = n;
        }
        eval(r - l, now);
        if(r <= a || b <= l) {
            return d1;
        }
        if(a <= l && r <= b) {
            return node[now];
        }
        T vl = query(a, b, 2 * now + 1, l, (l + r) / 2);
        T vr = query(a, b, 2 * now + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
};

struct Data {
    ll x, x2, cnt;
    Data() {}
    Data(ll _x, ll _x2, ll _cnt) : x(_x), x2(_x2), cnt(_cnt) {}
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> a(n);
    vector<Data> start(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        start[i] = Data(a[i], a[i] * a[i], 1);
    }
    auto f = [](const Data &A, const Data &B) {
        return Data(A.x + B.x, A.x2 + B.x2, A.cnt + B.cnt);
    };
    auto g = [](const Data &A, const ll &X) {
        return Data(A.x + X * A.cnt, A.x2 + X * X * A.cnt + 2 * X * A.x, A.cnt);
    };
    auto h = [](const ll &x, const ll &y) { return x + y; };
    LazySegmentTree<Data, ll> seg(n, f, g, h, Data(0, 0, 0), 0, start);
    int q;
    cin >> q;
    while(q--) {
        int t;
        cin >> t;
        if(t == 1) {
            int l, r;
            ll x;
            cin >> l >> r >> x;
            seg.update(l - 1, r, x);
        } else {
            int l, r;
            cin >> l >> r;
            cout << seg.query(l - 1, r).x2 << "\n";
        }
    }
}
0