#include int ri() { int n; scanf("%d", &n); return n; } struct SegTree { int n; std::vector sum; std::vector ssum; std::vector size; std::vector added; SegTree (const std::vector &a) { for (n = 1; n < (int) a.size(); n <<= 1); sum.resize(n << 1); ssum.resize(n << 1); added.resize(n << 1); size.resize(n << 1); for (int i = 0; i < (int) a.size(); i++) { sum[i + n] = a[i]; ssum[i + n] = (int64_t) a[i] * a[i]; size[i + n] = 1; } for (int i = n; --i; ) fetch(i), size[i] = size[i << 1] << 1; } void flush(int i) { if (added[i]) { ssum[i] += (int64_t) size[i] * added[i] * added[i] + (int64_t) sum[i] * added[i] * 2; sum[i] += (int64_t) size[i] * added[i]; if (i < n) { added[i << 1] += added[i]; added[i << 1 | 1] += added[i]; } added[i] = 0; } } void fetch(int i) { sum[i] = sum[i << 1] + sum[i << 1 | 1]; ssum[i] = ssum[i << 1] + ssum[i << 1 | 1]; } template void dive(int l, int r, int node, int node_l, int node_r, const T &func) { flush(node); if (r <= node_l || l >= node_r) return; if (l <= node_l && r >= node_r) func(node); else { int mid = node_l + ((node_r - node_l) >> 1); dive(l, r, node << 1, node_l, mid, func); dive(l, r, node << 1 | 1, mid, node_r, func); fetch(node); } } void add(int l, int r, int val) { dive(l, r, 1, 0, n, [&] (int node) { added[node] += val; flush(node); }); } int64_t get_ssum(int l, int r) { int64_t res = 0; dive(l, r, 1, 0, n, [&] (int node) { res += ssum[node]; }); return res; } }; int main() { int n = ri(); std::vector a(n); for (auto &i : a) i = ri(); SegTree tree(a); int q = ri(); for (int i = 0; i < q; i++) { int t = ri(); int l = ri() - 1; int r = ri(); if (t == 1) tree.add(l, r, ri()); else printf("%" PRId64 "\n", tree.get_ssum(l, r)); } return 0; }