#include using namespace std; using ll = long long; constexpr int INF = (int)1e9 + 1001010; constexpr ll llINF = (ll)4e18 + 11000010; #define endn "\n" template inline vector> vector2(size_t i, size_t j, const T &init = T()) {return vector>(i, vector(j, init));} const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn; template istream& operator >>(istream &i, vector &A) {for(auto &I : A) {i >> I;} return i;} template ostream& operator <<(ostream &o, const vector> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;} template ostream& operator <<(ostream &o, const vector &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;} template vector& operator ++(vector &A, int n) {for(auto &I : A) {I++;} return A;} template vector& operator --(vector &A, int n) {for(auto &I : A) {I--;} return A;} template bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);} template 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 struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} explicit lazy_segtree(int n) : lazy_segtree(vector(n, init())) {} explicit lazy_segtree(const vector& v) : _n(int(v.size())) { log = 0; while((1U << log) < (unsigned int)(_n)) log++; size = 1 << log; d = vector(2 * size, e()); lz = vector(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 int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template 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 int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template 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 d; vector 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 struct RangeAdd_RangeMin { static S op(S a, S b){ return min(a, b); } static S e(){ return numeric_limits::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; }; template struct RangeAdd_RangeMax { static S op(S a, S b){ return max(a, b); } static S e(){ return numeric_limits::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; }; template 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::max(); } static S init(){ return {0, 1}; } using Type = struct lazy_segtree; }; template struct RangeSet_RangeMin { static S op(S a, S b){ return min(a, b); } static S e(){ return numeric_limits::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::max(); } using Type = struct lazy_segtree; }; template struct RangeSet_RangeMax { static S op(S a, S b){ return max(a, b); } static S e(){ return numeric_limits::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::max(); } using Type = struct lazy_segtree; }; template 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::max(); } static S init(){ return {0, 1}; } using Type = struct lazy_segtree; }; struct RangeAdd_RangeSquare { using S = array; 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; }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector 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; }