#include #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define ALL(v) (v).begin(), (v).end() #define LLA(v) (v).rbegin(), (v).rend() #define SZ(v) (v).size() #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define STRING(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ read(name) using namespace std; using ll = long long; using pii = pair; using pll = pair; using Graph = vector>; template struct edge { int to; T cost; edge(int t, T c) : to(t), cost(c) {} }; template using WGraph = vector>>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; template inline vector make_vec(size_t a, T val) { return vector(a, val); } template inline auto make_vec(size_t a, Ts... ts) { return vector(a, make_vec(ts...)); } void read() {} template inline void read(T &a) { cin >> a; } template inline void read(pair &p) { read(p.first), read(p.second); } template inline void read(vector &v) { for(auto &a : v) read(a); } template inline void read(Head &head, Tail &...tail) { read(head), read(tail...); } template inline void chmax(T &a, T b) { (a < b ? a = b : a); } template inline void chmin(T &a, T b) { (a > b ? a = b : a); } /* SegmentTree(0-indexed) */ template struct SegmentTree { using F = function; const F f; const Monoid e; int n; vector dat; SegmentTree(int _n, const F _f, const Monoid &_e) : f(_f), e(_e) { n = 1; while(n < _n) n *= 2; dat.assign(2 * n, e); } SegmentTree(vector v, const F _f, const Monoid &_e) : f(_f), e(_e) { int sz = v.size(); n = 1; while(n < sz) n *= 2; dat.resize(2 * n); for(int i = 0; i < sz; i++) set(i, v[i]); build(); } void set(int k, Monoid a) { dat[k + n] = a; } void build() { for(int i = n - 1; i > 0; i--) { dat[i] = f(dat[2 * i], dat[2 * i + 1]); } } void update(int k, const Monoid &a) { k += n; dat[k] = a; while(k >>= 1) { dat[k] = f(dat[k * 2], dat[k * 2 + 1]); } } Monoid at(int a) { return query(a, a + 1); } // query for [a,b) Monoid query(int a, int b) { Monoid vl = e, vr = e; a += n, b += n; for(; a < b; a >>= 1, b >>= 1) { if(a & 1) vl = f(vl, dat[a++]); if(b & 1) vr = f(dat[--b], vr); } return f(vl, vr); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); INT(n, q); VEC(ll, a, n); using S = pll; auto op = [](S a, S b) -> S { return S{a.first + b.first, a.second + b.second}; }; S e = {0, 0}; SegmentTree segtree(n, op, e); using A = tuple; // [value,query,index] vector vec; rep(i, n) { vec.emplace_back(a[i], 0, i); } using Q = tuple; // [l,r,value] vector query(q); rep(i, q) { LL(_, l, r, x); l--, r--; query[i] = {l, r, x}; vec.emplace_back(x, 1, i); } sort(ALL(vec), greater{}); vector res(q); for(auto [val, qq, idx] : vec) { if(qq == 0) { segtree.update(idx, S{val, 1}); } else { auto [l, r, x] = query[idx]; auto [v, num] = segtree.query(l, r + 1); res[idx] = v - x * num; } } rep(i, q) { cout << res[i] << endl; } }