#define MOD_TYPE 1 #pragma region Macros #include using namespace std; #if 0 #include #include using Int = boost::multiprecision::cpp_int; using lld = boost::multiprecision::cpp_dec_float_100; #endif #if 1 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif using ll = long long int; using ld = long double; using pii = pair; using pll = pair; using pld = pair; template using smaller_queue = priority_queue, greater>; constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353); constexpr int INF = (int)1e9 + 10; constexpr ll LINF = (ll)4e18; constexpr double PI = acos(-1.0); constexpr double EPS = 1e-7; constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0}; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define REPI(i, m, n) for (int i = m; i < (int)(n); ++i) #define repi(i, n) REPI(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << "\n" #define Yes(n) cout << ((n) ? "Yes" : "No") << "\n" #define possible(n) cout << ((n) ? "possible" : "impossible") << "\n" #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n" #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x) cerr << #x << ":" << x << "\n"; struct io_init { io_init() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(30) << setiosflags(ios::fixed); }; } io_init; template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template constexpr istream &operator>>(istream &is, pair &p) noexcept { is >> p.first >> p.second; return is; } template constexpr ostream &operator<<(ostream &os, pair &p) noexcept { os << p.first << " " << p.second; return os; } #pragma endregion template struct SegmentTree { using F = function; using G = function; using H = function; using P = function; int n; F f; G g; H h; P p; T ti; E ei; vector dat; vector laz; SegmentTree() {} SegmentTree( int n_, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(n_); } SegmentTree( vector &v, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(v.size()); build(v); } void init(int n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, ti); laz.clear(); laz.resize(2 * n - 1, ei); } void build(const vector v) { for (int i = 0; i < v.size(); i++) dat[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } void eval(int len, int k) { if (laz[k] == ei) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = ei; } T update(int a, int b, E x, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); } T query(int a, int b, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return ti; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(int a, int b) { return query(a, b, 0, 0, n); } }; void solve() { int n; cin >> n; using arr = array; vector v(n); rep(i, n) { ll a; cin >> a; v[i] = {a * a, a, 1}; } auto f = [](arr a, arr b) { return arr{a[0] + b[0], a[1] + b[1], a[2] + b[2]}; }; auto g = [](arr a, ll x) { return arr{a[0] + 2 * a[1] * x + a[2] * x * x, a[1] + a[2] * x, a[2]}; }; auto h = [](ll x, ll y) { return x + y; }; SegmentTree sg(v, f, g, h, arr{0, 0, 0}, 0); int q; cin >> q; rep(qi, q) { int type; cin >> type; if (type == 1) { int l, r; ll x; cin >> l >> r >> x; l--; sg.update(l, r, x); } else { int l, r; cin >> l >> r; l--; cout << sg.query(l, r)[0] << "\n"; } } } int main() { solve(); }