#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif template struct lazy_segtree { const int n; vector t; vector d; lazy_segtree(int _n = 0) : n(_n), t(2 * n), d(n) {} T& operator[](int i) { return t[n + i]; } void build() { for (int i = n; i--; ) t[i] = t[2 * i] * t[2 * i + 1]; } void apply(int i, F f) { t[i] = f(t[i]); if (i < n) d[i] = d[i] * f; } void push(int i) { apply(2 * i, d[i]), apply(2 * i + 1, d[i]); d[i] = F(); } void push_down(int i) { for (int h = __lg(i), tz = __builtin_ctz(i); h > tz; --h) push(i >> h); } void pull_up(int i) { while (i >>= 1) t[i] = t[2 * i] * t[2 * i + 1]; } T fold(int l, int r) { push_down(l += n), push_down(r += n); T a, b; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) a = a * t[l++]; if (r & 1) b = t[--r] * b; } return a * b; } T get(int i) { return fold(i, i + 1); } void act(int l, int r, F f) { push_down(l += n), push_down(r += n); for (int i = l, j = r; i < j; i >>= 1, j >>= 1) { if (i & 1) apply(i++, f); if (j & 1) apply(--j, f); } pull_up(l >> __builtin_ctz(l)), pull_up(r >> __builtin_ctz(r)); } void set(int i, T a) { push_down(i += n), push_down(i + 1); t[i] = a; pull_up(i); } }; using ll = long long; struct node { ll s0 = 0, s1 = 0, s2 = 0; node operator*(node b) const { return {s0 + b.s0, s1 + b.s1, s2 + b.s2}; } }; struct add { ll v = 0; add operator*(add g) const { return {v + g.v}; } node operator()(node x) const { x.s2 += 2 * v * x.s1 + x.s0 * v * v; x.s1 += x.s0 * v; return x; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; lazy_segtree seg(n); for (int i = 0; i < n; ++i) { ll a; cin >> a; seg[i] = {1, a, a * a}; } seg.build(); int q; cin >> q; while (q--) { int tp, l, r; cin >> tp >> l >> r; --l; if (tp == 1) { ll x; cin >> x; seg.act(l, r, {x}); } else { cout << seg.fold(l, r).s2 << '\n'; } } }