#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, a, b) for(int i = a; i < b; i++) #define rrep(i, a, b) for(int i = b - 1; i >= a; i--) #define ALL(a) a.begin(), a.end() using pii = pair; using piii = pair; using pll = pair; using plll = pair; // #pragma GCC optimize("Ofast") #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define isSquare(x) (sqrt(x)*sqrt(x) == x) templateinline bool chmax(T &a, const T &b) {if(ainline bool chmin(T &a, const T &b) {if(a>b){a = b; return 1;} return 0; }; inline void in(void){return;} template void in(First& first, Rest&... rest){cin >> first;in(rest...);return;} inline void out(void){cout << "\n";return;} template void out(First first, Rest... rest){cout << first << " ";out(rest...);return;} const double EPS = 1e-9; const int mod = 1e9 + 7; // const int mod = 998244353; const int INF = 1e9; const long long INFLL = 1e18; void iosetup() { cin.tie(nullptr);ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template vector make_vec(size_t a) {return vector(a); } template auto make_vec(size_t a, Ts... ts){ return vector(ts...))>(a, make_vec(ts...)); } template pair operator+(const pair &s, const pair& t){return pair(s.first+t.first, s.second+t.second);} template pair operator-(const pair &s, const pair& t){return pair(s.first-t.first, s.second-t.second);} template pair operator*(const pair &s, const S& t){return pair(s.first*t, s.second*t);} template void Exit(T first){cout << first << endl;exit(0); }; template< int mod > struct ModInt { unsigned x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator*=(const ModInt &p) {x = (int) (1LL * x * p.x % mod);return *this;} ModInt &operator/=(const ModInt &p) {*this *= p.inverse();return *this;} ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const {int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); }return ModInt(u);} ModInt pow(int64_t n) const {ModInt ret(1), mul(x); while(n > 0) {if(n & 1) ret *= mul;mul *= mul;n >>= 1;}return ret;} friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x;} friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt< mod >; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const pii dxy[4] = {pii(1,0), pii(0, 1), pii(-1, 0), pii(0, -1)}; bool range(int a, int b, int x){if(a <= x and x < b)return true;else return false;} bool range(int a, int b, int c, int d, pii p){if(a <= p.first and p.first < b and c <= p.second and p.second < d) return true;else return false;} /** * @brief Lazy-Segment-Tree(遅延伝搬セグメント木) * @docs docs/lazy-segment-tree.md */ template< typename Monoid, typename OperatorMonoid = Monoid > struct LazySegmentTree { using F = function< Monoid(Monoid, Monoid) >; using G = function< Monoid(Monoid, OperatorMonoid) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; int sz, height; vector< Monoid > data; vector< OperatorMonoid > lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while(sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(int k) { if(lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = apply(k); lazy[k] = OM0; } } inline Monoid apply(int k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(int k) { while(k >>= 1) data[k] = f(apply(2 * k + 0), apply(2 * k + 1)); } inline void thrust(int k) { for(int i = height; i > 0; i--) propagate(k >> i); } void update(int a, int b, const OperatorMonoid &x) { if(a >= b) return; thrust(a += sz); thrust(b += sz - 1); for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) lazy[l] = h(lazy[l], x), ++l; if(r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(int a, int b) { if(a >= b) return M1; thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) L = f(L, apply(l++)); if(r & 1) R = f(apply(--r), R); } return f(L, R); } Monoid operator[](const int &k) { return query(k, k + 1); } template< typename C > int find_subtree(int a, const C &check, Monoid &M, bool type) { while(a < sz) { propagate(a); Monoid nxt = type ? f(apply(2 * a + type), M) : f(M, apply(2 * a + type)); if(check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C &check) { Monoid L = M1; if(a <= 0) { if(check(f(L, apply(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) { Monoid nxt = f(L, apply(a)); if(check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C &check) { Monoid R = M1; if(b >= sz) { if(check(f(apply(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for(b += sz; a < b; a >>= 1, b >>= 1) { if(b & 1) { Monoid nxt = f(apply(--b), R); if(check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; // using pi = pair< mint, int >; // using qi = pair< mint, mint >; // auto f = [](const pi &a, const pi &b) -> pi { // return {a.first + b.first, a.second + b.second}; // }; // auto g = [](const pi &a, const qi &b) -> pi { // return {a.first * b.first + mint(a.second) * b.second, a.second}; // }; // auto h = [](const qi &a, const qi &b) -> qi { // return {a.first * b.first, a.second * b.first + b.second}; // }; // LazySegmentTree< pi, qi > seg(N, f, g, h, pi(0, 0), pi(1, 0)); using P = tuple; int main(){ iosetup(); int n; cin >> n; vector a(n); cin >> a; auto f = [](P x, P y){ ll a, b, c, d, e, f; tie(a, b, c) = x; tie(d, e, f) = y; return P(a + d, b + e, c + f); }; auto g = [](P x, ll y) -> P{ ll a, b, c; tie(a, b, c) = x; return P(a + 2 * b * y + c * y * y, b + y * c, c); }; auto h = [](ll a, ll b){ return a + b; }; LazySegmentTree< P, ll > seg(n, f, g, h, P(0, 0, 0), 0); rep(i, 0, n) seg.set(i, P(a[i] * a[i], a[i], 1)); seg.build(); int Q; cin >> Q; while(Q--){ ll t, l, r, x; cin >> t >> l >> r; l--; if(t == 1){ cin >> x; seg.update(l, r, x); }else{ ll a, b, c; tie(a, b, c) = seg.query(l, r); cout << a << endl; } } return 0; }