#include using namespace std; int INF = 1000000000; int MAX_N = 200000; int MAX_A = 1000000; int MIN_A = -1000000; int MAX_Q = 100000; int MAX_X = 1000000; int MIN_X = -1000000; struct range_add_range_min{ int N; vector ST; vector lazy; range_add_range_min(vector &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST = vector(N * 2 - 1, INF); lazy = vector(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST[i] += lazy[i]; lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i); } else { int m = (l + r) / 2; range_add(L, R, x, i * 2 + 1, l, m); range_add(L, R, x, i * 2 + 2, m, r); ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]); } } void range_add(int L, int R, int x){ range_add(L, R, x, 0, 0, N); } int range_min(int L, int R, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return INF; } else if (L <= l && r <= R){ return ST[i]; } else { int m = (l + r) / 2; return min(range_min(L, R, i * 2 + 1, l, m), range_min(L, R, i * 2 + 2, m, r)); } } int range_min(int L, int R){ return range_min(L, R, 0, 0, N); } }; struct range_add_range_max{ int N; vector ST; vector lazy; range_add_range_max(vector &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST = vector(N * 2 - 1, -INF); lazy = vector(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST[i] += lazy[i]; lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i); } else { int m = (l + r) / 2; range_add(L, R, x, i * 2 + 1, l, m); range_add(L, R, x, i * 2 + 2, m, r); ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]); } } void range_add(int L, int R, int x){ range_add(L, R, x, 0, 0, N); } int range_max(int L, int R, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return -INF; } else if (L <= l && r <= R){ return ST[i]; } else { int m = (l + r) / 2; return max(range_max(L, R, i * 2 + 1, l, m), range_max(L, R, i * 2 + 2, m, r)); } } int range_max(int L, int R){ return range_max(L, R, 0, 0, N); } }; struct range_add_range_sqsum{ int N; vector ST1; //sum vector ST2; //sqsum vector lazy; range_add_range_sqsum(vector &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST1 = vector(N * 2 - 1, 0); ST2 = vector(N * 2 - 1, 0); lazy = vector(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST1[N - 1 + i] = A[i]; ST2[N - 1 + i] = A[i] * A[i]; } for (int i = N - 2; i >= 0; i--){ ST1[i] = ST1[i * 2 + 1] + ST1[i * 2 + 2]; ST2[i] = ST2[i * 2 + 1] + ST2[i * 2 + 2]; } } void eval(int i, int l, int r){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST2[i] = ST2[i] + lazy[i] * ST1[i] * 2 + lazy[i] * lazy[i] * (r - l); ST1[i] = ST1[i] + lazy[i] * (r - l); lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i, l, r); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i, l, r); } else { int m = (l + r) / 2; range_add(L, R, x, i * 2 + 1, l, m); range_add(L, R, x, i * 2 + 2, m, r); ST1[i] = ST1[i * 2 + 1] + ST1[i * 2 + 2]; ST2[i] = ST2[i * 2 + 1] + ST2[i * 2 + 2]; } } void range_add(int L, int R, int x){ range_add(L, R, x, 0, 0, N); } long long range_sqsum(int L, int R, int i, int l, int r){ eval(i, l, r); if (R <= l || r <= L){ return 0; } else if (L <= l && r <= R){ return ST2[i]; } else { int m = (l + r) / 2; return range_sqsum(L, R, i * 2 + 1, l, m) + range_sqsum(L, R, i * 2 + 2, m, r); } } long long range_sqsum(int L, int R){ return range_sqsum(L, R, 0, 0, N); } }; int main(){ int N; cin >> N; assert(1 <= N); assert(N <= MAX_N); vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; assert(MIN_A <= A[i]); assert(A[i] <= MAX_A); } range_add_range_sqsum a(A); range_add_range_min b(A); range_add_range_max c(A); int Q; cin >> Q; assert(1 <= Q); assert(Q <= MAX_Q); int cnt = 0; for (int i = 0; i < Q; i++){ int t; cin >> t; assert(t == 1 || t == 2); if (t == 1){ int l, r, x; cin >> l >> r >> x; assert(1 <= l && l <= r && r <= N); assert(MIN_X <= x); assert(x <= MAX_X); a.range_add(l - 1, r, x); b.range_add(l - 1, r, x); c.range_add(l - 1, r, x); assert(b.range_min(0, N) >= MIN_A); assert(c.range_max(0, N) <= MAX_A); } if (t == 2){ cnt++; int l, r; cin >> l >> r; assert(1 <= l && l <= r && r <= N); cout << a.range_sqsum(l - 1, r) << endl; } } assert(cnt >= 1); }