#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; template struct SqrtDecomposition { int b, b_n; vector left, right; vector need_to_be_eval; SqrtDecomposition(int n) : b(sqrt(n)) { b_n = (n + b - 1) / b; left.resize(b_n); right.resize(b_n); need_to_be_eval.assign(b_n, false); REP(i, b_n) { left[i] = b * i; right[i] = (i + 1 == b_n ? n : b * (i + 1)); } } void partial_update(int idx, T val); void total_update(int idx, T val); void update(int l, int r, T val) { if (r <= l) return; int l_b = l / b, r_b = (r - 1) / b; if (l_b == r_b) { FOR(i, l, r) partial_update(i, val); } else { FOR(i, l, right[l_b]) partial_update(i, val); FOR(i, l_b + 1, r_b) total_update(i, val); FOR(i, left[r_b], r) partial_update(i, val); } } void partial_query(int idx, T &val); void total_query(int idx, T &val); T query(int l, int r, T UNITY) { int l_b = l / b, r_b = (r - 1) / b; T res = UNITY; if (l_b == r_b) { FOR(i, l, r) partial_query(i, res); } else if (l < r) { FOR(i, l, right[l_b]) partial_query(i, res); FOR(i, l_b + 1, r_b) total_query(i, res); FOR(i, left[r_b], r) partial_query(i, res); } return res; } }; vector a, squ, sum, add; template void SqrtDecomposition::partial_update(int idx, T val) { int block = idx / b; if (need_to_be_eval[block]) { FOR(i, left[block], right[block]) { squ[block] -= a[i] * a[i]; a[i] += add[block]; sum[block] += add[block]; squ[block] += a[i] * a[i]; } add[block] = 0; need_to_be_eval[block] = false; } squ[block] -= a[idx] * a[idx]; a[idx] += val; sum[block] += val; squ[block] += a[idx] * a[idx]; } template void SqrtDecomposition::total_update(int idx, T val) { add[idx / b] += val; need_to_be_eval[idx / b] = true; } template void SqrtDecomposition::partial_query(int idx, T &val) { int block = idx / b; if (need_to_be_eval[block]) { FOR(i, left[block], right[block]) { squ[block] -= a[i] * a[i]; a[i] += add[block]; sum[block] += add[block]; squ[block] += a[i] * a[i]; } add[block] = 0; need_to_be_eval[block] = false; } val += a[idx] * a[idx]; } template void SqrtDecomposition::total_query(int idx, T &val) { val += squ[idx] + sum[idx] * add[idx] * 2 + add[idx] * add[idx] * (right[idx] - left[idx]); } int main() { int n; cin >> n; SqrtDecomposition sd(n); a.resize(n); squ.assign(sd.b_n, 0); sum.assign(sd.b_n, 0); REP(i, n) { cin >> a[i]; squ[i / sd.b_n] += a[i] * a[i]; sum[i / sd.b_n] += a[i]; } add.assign(sd.b_n, 0); int q; cin >> q; while (q--) { int query, l, r; cin >> query >> l >> r; --l; --r; if (query == 1) { int x; cin >> x; sd.update(l, r + 1, x); } else if (query == 2) { cout << sd.query(l, r + 1, 0) << '\n'; } } return 0; }