#include #include #include using namespace std; class SegTree { using T = pair; static const T op(const T& lhs, const T& rhs) { // binary operator return {lhs.first + rhs.first, lhs.second + rhs.second}; } constexpr static T unit = { 0, 0 }; // unit element const int N; T *data; static int calc_size(int sz) { int n = 1; while (sz > n) n *= 2; return n; } public: explicit SegTree(int sz) : N(calc_size(sz)) { data = new T[2*N-1]; } ~SegTree() { delete[] data; } T query(int a, int b, int i = -1, int l = -1, int r = -1) const { if (i == -1) { i = 0; l = 0; r = N; } // if (r <= a || b <= l) return unit; // need this if a query can be empty if (a <= l && r <= b) return data[i]; int m = (l+r)/2; if (b <= m) return query(a, b, 2*i+1, l, m); if (m <= a) return query(a, b, 2*i+2, m, r); T v1 = query(a, b, 2*i+1, l, m), v2 = query(a, b, 2*i+2, m, r); return op(v1, v2); } void update(int i, T v) { int x = i + N - 1; data[x] = v; while (x > 0) { x = (x-1)/2; data[x] = op(data[2*x+1], data[2*x+2]); } } }; struct query { int id, l, r, x; }; int main() { vector qs; int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { int a; cin >> a; qs.push_back({-1, i, 0, a}); } for (int i = 0; i < q; i++) { int j, l, r, x; cin >> j >> l >> r >> x; qs.push_back({i, l, r, x}); } sort(qs.begin(), qs.end(), [](query &lhs, query &rhs) { return lhs.x > rhs.x; }); SegTree st(n); vector ans(q); for (query &q : qs) { if (q.id == -1) st.update(q.l, {q.x, 1}); else { auto x = st.query(q.l-1, q.r); ans[q.id] = x.first - (long long)x.second * q.x; } } for (long long a : ans) cout << a << endl; }