結果

問題 No.1099 Range Square Sum
ユーザー Ricky_ponRicky_pon
提出日時 2020-06-26 23:35:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,215 bytes
コンパイル時間 2,486 ms
コンパイル使用メモリ 216,724 KB
実行使用メモリ 21,796 KB
最終ジャッジ日時 2023-09-18 08:27:17
合計ジャッジ時間 6,086 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
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>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

template<class T, class E> struct LazySegTree{
    using F = function<T(T, T)>;
    using G = function<T(T, E)>;
    using H = function<E(E, E)>;
    using P = function<E(E, int)>;
    int sz = 1, seq_sz; F f; G g; H h; T et; E ee; P p;
    int cur = 0;
    vector<T> node;
    vector<E> lazy;
    vector<int> idx, len;

    LazySegTree(int sz_, T et_, E ee_, F f_, G g_, H h_, P p_=[](E a, int b){return a;}):
    seq_sz(sz_), et(et_), ee(ee_), f(f_), g(g_), h(h_), p(p_){
        while(sz < sz_) sz <<= 1;
        node.resize(sz<<1, et);
        lazy.resize(sz<<1, ee);
        idx.resize(sz, -1);
        len.resize(sz<<1, 0);
        For(i, sz, sz<<1) len[i] = 1;
        rFor(i, sz, 1) len[i] = len[i<<1] + len[(i<<1)+1];
    }

    void build(vector<T> &a){
        rep(i, a.size()) node[i+sz] = a[i];
        rFor(i, sz, 1) node[i] = f(node[i<<1], node[(i<<1)+1]);
    }

    void build(T x){
        rep(i, seq_sz) node[i+sz] = x;
        rFor(i, sz, 1) node[i] = f(node[i<<1], node[(i<<1)+1]);
    }

    inline void push(int i){
        if(lazy[i] == ee) return;
        if(i < sz) {
            lazy[i << 1] = h(lazy[i << 1], lazy[i]);
            node[i << 1] = g(node[i << 1], p(lazy[i], len[i << 1]));
            lazy[(i << 1) + 1] = h(lazy[(i << 1) + 1], lazy[i]);
            node[(i << 1) + 1] = g(node[(i << 1) + 1], p(lazy[i], len[(i << 1) + 1]));
        }
        lazy[i] = ee;
    }

    inline void prop(int l, int r){
        cur = 0;
        if(l == sz && r == sz<<1) return;
        l = (l / (l & -l)) >> 1;
        r = (r / (r & -r)) >> 1;
        while(l > 1 || r > 1){
            if(l >= r){
                idx[cur++] = l;
                l >>= 1;
            }
            else{
                idx[cur++] = r;
                r >>= 1;
            }
        }
        idx[cur++] = 1;

        rrep(k, cur) push(idx[k]);
    }

    void update(int l, int r, E x){
        l += sz; r += sz;
        prop(l, r);
        for(; l<r; l>>=1, r>>=1){
            if(l & 1){
                lazy[l] = h(lazy[l], x);
                node[l] = g(node[l], p(x, len[l]));
                ++l;
            }
            if(r & 1){
                --r;
                lazy[r] = h(lazy[r], x);
                node[r] = g(node[r], p(x, len[r]));
            }
        }
        rep(k, cur){
            int i = idx[k];
            if(i < sz) node[i] = f(node[i<<1], node[(i<<1)+1]);
        }
    }

    T query(int l, int r){
        l += sz; r += sz;
        prop(l, r);
        T vl = et, vr = et;
        for(; l<r; l>>=1, r>>=1){
            if(l & 1) vl = f(vl, node[l++]);
            if(r & 1) vr = f(node[--r], vr);
        }
        return f(vl, vr);
    }
};

int main(){
    int n;
    scanf("%d", &n);
    vector<pll> a(n);
    rep(i, n) scanf("%lld", &a[i].se), a[i].fi = a[i].se * a[i].se;
    auto f = [](pll p, pll q){return make_pair(p.fi+q.fi, p.se+q.se);};
    auto g = [](pll p, lint x){return make_pair(p.fi+2*p.se+x, p.se+x);};
    LazySegTree<pll, lint> lst(n, {0, 0}, 0, f, g, plus<>(), multiplies<>());
    lst.build(a);

    int q;
    scanf("%d", &q);
    rep(_, q){
        int t, l, r, x;
        scanf("%d%d%d", &t, &l, &r);
        if(t == 1){
            scanf("%d", &x);
            lst.update(l-1, r, x);
        }
        else printf("%lld\n", lst.query(l-1, r).fi);
    }
}
0