結果

問題 No.1099 Range Square Sum
ユーザー SlephySlephy
提出日時 2023-03-02 06:46:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 10,449 bytes
コンパイル時間 2,093 ms
コンパイル使用メモリ 212,056 KB
実行使用メモリ 23,808 KB
最終ジャッジ日時 2024-09-17 03:55:37
合計ジャッジ時間 5,981 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 137 ms
23,808 KB
testcase_22 AC 136 ms
23,808 KB
testcase_23 AC 135 ms
23,796 KB
testcase_24 AC 133 ms
23,808 KB
testcase_25 AC 134 ms
23,680 KB
testcase_26 AC 119 ms
23,680 KB
testcase_27 AC 120 ms
23,792 KB
testcase_28 AC 118 ms
23,784 KB
testcase_29 AC 119 ms
23,788 KB
testcase_30 AC 119 ms
23,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = (int)1e9 + 1001010;
constexpr ll llINF = (ll)4e18 + 11000010;
#define endn "\n"
template <class T> inline vector<vector<T>> vector2(size_t i, size_t j, const T &init = T()) {return vector<vector<T>>(i, vector<T>(j, init));}
const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b) {assert(b != 0); return((a%b != 0 && ((a>0) != (b>0))) ? a/b-1 : a/b);}
ll ceil (ll a, ll b) {assert(b != 0); return((a%b != 0 && ((a>0) == (b>0))) ? a/b+1 : a/b);}
// ================================== ここまでテンプレ ==================================

// ACLの実装を一部改変したもの
// == 変更点 ==
// internal::ceil_pow2 を埋め込んだ
// add を追加
// データの初期化用途で init を渡せるようにした(従来の実装では e で初期化される)

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)(),
          S (*init)() = e
          >
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    explicit lazy_segtree(int n) : lazy_segtree(vector<S>(n, init())) {}
    explicit lazy_segtree(const vector<S>& v) : _n(int(v.size())) {
        log = 0;
        while((1U << log) < (unsigned int)(_n)) log++;
        size = 1 << log;
        d = vector<S>(2 * size, e());
        lz = vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void add(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] += x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    vector<S> d;
    vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};


template<class S, class F> struct RangeAdd_RangeMin {
    static S op(S a, S b){ return min(a, b); }
    static S e(){ return numeric_limits<S>::max(); }
    static S mapping(F f, S x){ return f+x; }
    static F composition(F f, F g){ return f+g; }
    static F id(){ return 0; }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id>;
};

template<class S, class F> struct RangeAdd_RangeMax {
    static S op(S a, S b){ return max(a, b); }
    static S e(){ return numeric_limits<S>::min(); }
    static S mapping(F f, S x){ return f+x; }
    static F composition(F f, F g){ return f+g; }
    static F id(){ return 0; }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id>;
};

template<class ValType, class F> struct RangeAdd_RangeSum {
    struct S{
        ValType value;
        int size;
    };
    static S op(S a, S b){ return {a.value+b.value, a.size+b.size}; }
    static S e(){ return {0, 0}; }
    static S mapping(F f, S x){ return {x.value + f*x.size, x.size}; }
    static F composition(F f, F g){ return f+g; }
    static F id(){ return numeric_limits<F>::max(); }
    static S init(){ return {0, 1}; }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id, init>;
};

template<class S, class F> struct RangeSet_RangeMin {
    static S op(S a, S b){ return min(a, b); }
    static S e(){ return numeric_limits<S>::max(); }
    static S mapping(F f, S x){ return (f == id() ? x : f); }
    static F composition(F f, F g){ return (f == id() ? g : f); }
    static F id(){ return numeric_limits<F>::max(); }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id>;
};

template<class S, class F> struct RangeSet_RangeMax {
    static S op(S a, S b){ return max(a, b); }
    static S e(){ return numeric_limits<S>::min(); }
    static S mapping(F f, S x){ return (f == id() ? x : f); }
    static F composition(F f, F g){ return (f == id() ? g : f); }
    static F id(){ return numeric_limits<F>::max(); }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id>;
};

template<class ValType, class F> struct RangeSet_RangeSum {
    struct S{
        ValType value;
        int size;
    };
    static S op(S a, S b){ return {a.value+b.value, a.size+b.size}; }
    static S e(){ return {0, 0}; }
    static S mapping(F f, S x){
        if(f != id()) x.value = x.size * f;
        return x;
    }
    static F composition(F f, F g){ return (f == id() ? g : f); }
    static F id(){ return numeric_limits<F>::max(); }
    static S init(){ return {0, 1}; }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id, init>;
};

struct RangeAdd_RangeSquare {
    using S = array<ll, 3>;
    using F = ll;
    static S op(S a, S b){ return S{a[0]+b[0], a[1]+b[1], a[2]+b[2]}; }
    static S e(){ return  S{0, 0, 0}; }
    static S mapping(F f, S x){ return S{x[0], x[1] + x[0]*f, x[2] + 2*f*x[1] + f*f*x[0]}; }
    static F composition(F f, F g){ return f+g; }
    static F id(){ return 0; }
    using Type = struct lazy_segtree<S, op, e, F, mapping, composition, id>;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    vector<ll> a(n); cin >> a;
    RangeAdd_RangeSquare::Type seg(n);
    for(int i = 0; i < n; i++){
        seg.set(i, {1, a[i], a[i]*a[i]});
    }

    int q; cin >> q;
    while(q--){
        int op; cin >> op;
        if(op == 1){
            ll l, r, x; cin >> l >> r >> x;
            l--; 
            // [l, r) 0-indexed
            seg.apply(l, r, x);
        }
        if(op == 2){
            ll l, r; cin >> l >> r;
            l--;
            // [l, r) 0-indexed
            auto res = seg.prod(l, r);
            cout << res[2] << endn;
        }
    }
    return 0;
}
0