#include #include #include #include #include #include #include #include using namespace std; #pragma warning (disable: 4996) const int BACKET = 500; long long N, A[1 << 18]; long long Q, X[1 << 18], Y[1 << 18], Z[1 << 18], W[1 << 18]; long long Answer[1 << 18]; // 平方分割 vector vec; long long V[1 << 18]; long long P1[1 << 18], P2[1 << 18], P3[1 << 18]; void solve(int cl, int cr) { // 初期化 vec.clear(); for (int i = 0; i <= BACKET * 2 + 100; i++) { V[i] = 0; P1[i] = 0; P2[i] = 0; P3[i] = 0; } // ソート vec.clear(); vec.push_back(1); vec.push_back(N + 1); for (int i = cl; i <= cr; i++) vec.push_back(Y[i]); for (int i = cl; i <= cr; i++) vec.push_back(Z[i] + 1); sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); // 前準備 for (int i = 0; i < vec.size() - 1; i++) { for (int j = vec[i]; j < vec[i + 1]; j++) { P1[i] += 1LL; P2[i] += 2LL * A[j]; P3[i] += A[j] * A[j]; } } // 平方分割 for (int i = cl; i <= cr; i++) { int pos1 = lower_bound(vec.begin(), vec.end(), (int)Y[i]) - vec.begin(); int pos2 = lower_bound(vec.begin(), vec.end(), (int)Z[i] + 1) - vec.begin(); if (X[i] == 1) { for (int j = pos1; j < pos2; j++) V[j] += W[i]; } if (X[i] == 2) { long long ans = 0; for (int j = pos1; j < pos2; j++) ans += V[j] * V[j] * P1[j] + V[j] * P2[j] + P3[j]; Answer[i] = ans; } } } int main() { // 入力 scanf("%lld", &N); for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); scanf("%lld", &Q); for (int i = 1; i <= Q; i++) { scanf("%lld", &X[i]); if (X[i] == 1) scanf("%lld%lld%lld", &Y[i], &Z[i], &W[i]); if (X[i] == 2) scanf("%lld%lld", &Y[i], &Z[i]); } // 平方分割 for (int i = 1; i <= Q; i += BACKET) { int cl = i, cr = i + BACKET - 1; cr = min(cr, (int)Q); solve(cl, cr); } // 出力 for (int i = 1; i <= Q; i++) { if (X[i] == 2) printf("%lld\n", Answer[i]); } return 0; }