#pragma GCC optimize("Ofast") #include #include #include using namespace std; using namespace __gnu_pbds; using ll = long long; using u64 = uint_fast64_t; using pii = pair; using pll = pair; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007; //constexpr long long MOD = 998244353; template inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; } template inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; } inline int popcount(int x) {return __builtin_popcount(x);} inline int popcount(long long x) {return __builtin_popcountll(x);} void print() { cout << "\n"; } template void print(const T &x, const Args &... args) { cout << x << " "; print(args...); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// const int MAX = 100000; const ll B = 320; ll A[MAX]; ll S[(MAX+B)/B]; ll nijo[(MAX+B)/B]; ll flag[(MAX+B)/B]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; rep(i,N) { cin >> A[i]; S[i/B] += A[i]; nijo[i/B] += A[i]*A[i]; } int Q; cin >> Q; while (Q--) { int t; cin >> t; if (t==2) { ll l,r; cin >> l >> r; --l; --r; ll ans = 0; if (l/B==r/B) { if (flag[l/B] != 0) { for (int i = l/B*B; i < min(l/B*B+B,N); i++) { A[i] += flag[i/B]; } flag[l/B] = 0; } for (int i = l; i <= r; i++) { ans += A[i]*A[i]; } } else { if (flag[l/B] != 0) { for (int i = l/B*B; i < l/B*B+B; i++) { A[i] += flag[i/B]; } flag[l/B] = 0; } if (flag[r/B] != 0) { for (int i = r/B*B; i < min(r/B*B+B,N); i++) { A[i] += flag[i/B]; } flag[r/B] = 0; } for (int i = l; i < l/B*B+B; i++) ans += A[i]*A[i]; for (int i = l/B+1; i < r/B; i++) ans += nijo[i]; for (int i = r/B*B; i <= r; i++) ans += A[i]*A[i]; } cout << ans << ln; } else { ll l,r,x; cin >> l >> r >> x; --l; --r; if (l/B==r/B) { if (flag[l/B] != 0) { for (int i = l/B*B; i < min(l/B*B+B,N); i++) { A[i] += flag[i/B]; } flag[l/B] = 0; } for (int i = l; i <= r; i++) { S[i/B] -= A[i]; nijo[i/B] -= A[i]*A[i]; A[i] += x; S[i/B] += A[i]; nijo[i/B] += A[i]*A[i]; } } else { if (flag[l/B] != 0) { for (int i = l/B*B; i < l/B*B+B; i++) { A[i] += flag[i/B]; } flag[l/B] = 0; } if (flag[r/B] != 0) { for (int i = r/B*B; i < min(r/B*B+B,N); i++) { A[i] += flag[i/B]; } flag[r/B] = 0; } for (int i = l; i < l/B*B+B; i++) { S[i/B] -= A[i]; nijo[i/B] -= A[i]*A[i]; A[i] += x; S[i/B] += A[i]; nijo[i/B] += A[i]*A[i]; } for (int i = l/B+1; i < r/B; i++) { nijo[i] += x*2*S[i]; S[i] += B*x; nijo[i] += B*x*x; flag[i] = x; } for (int i = r/B*B; i <= r; i++) { S[i/B] -= A[i]; nijo[i/B] -= A[i]*A[i]; A[i] += x; S[i/B] += A[i]; nijo[i/B] += A[i]*A[i]; } } } } }