#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 lazy_segment_tree_minmax{ int N; vector ST_min; vector ST_max; vector lazy; lazy_segment_tree_minmax(vector &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST_min = vector(N * 2 - 1, INF); ST_max = vector(N * 2 - 1, -INF); lazy = vector(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST_min[N - 1 + i] = A[i]; ST_max[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST_min[i] = min(ST_min[i * 2 + 1], ST_min[i * 2 + 2]); ST_max[i] = max(ST_max[i * 2 + 1], ST_max[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST_min[i] += lazy[i]; ST_max[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_min[i] = min(ST_min[i * 2 + 1], ST_min[i * 2 + 2]); ST_max[i] = max(ST_max[i * 2 + 1], ST_max[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_min[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); } 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_max[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 lazy_segment_tree{ int N; vector count; vector sum; vector sqsum; vector lazy; lazy_segment_tree(vector &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } count = vector(N * 2 - 1, 1); sum = vector(N * 2 - 1, 0); sqsum = vector(N * 2 - 1, 0); lazy = vector(N * 2 - 1, 0); for (int i = 0; i < n; i++){ sum[N - 1 + i] = A[i]; sqsum[N - 1 + i] = A[i] * A[i]; } for (int i = N - 2; i >= 0; i--){ count[i] = count[i * 2 + 1] + count[i * 2 + 2]; sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2]; sqsum[i] = sqsum[i * 2 + 1] + sqsum[i * 2 + 2]; } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } sqsum[i] = sqsum[i] + 2 * sum[i] * lazy[i] + count[i] * lazy[i] * lazy[i]; sum[i] = sum[i] + count[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); sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2]; sqsum[i] = sqsum[i * 2 + 1] + sqsum[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); if (R <= l || r <= L){ return 0; } else if (L <= l && r <= R){ return sqsum[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); } lazy_segment_tree_minmax a(A); lazy_segment_tree b(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); assert(a.range_min(0, N) >= MIN_A); assert(a.range_max(0, N) <= MAX_A); } if (t == 2){ cnt++; int l, r; cin >> l >> r; assert(1 <= l && l <= r && r <= N); cout << b.range_sqsum(l - 1, r) << endl; } } assert(cnt >= 1); }