#include using namespace std; using lint = long long; template using V = vector; template using VV = V< V >; struct SegmentTree { using T = pair; static T op(const T& a, const T& b) { return {a.first + b.first, a.second + b.second}; } static constexpr T e() { return {0, 0}; } const int n; V t; SegmentTree(int n) : n(n), t(2 * n, e()) {} T& operator[](int i) { return t[i + n]; } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } T acc(int l, int r) const { T resl = e(), resr = e(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } void set(int i, const T& a) { t[i += n] = a; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, q; cin >> n >> q; V<> a(n); for (auto&& e : a) cin >> e; struct Q { int id, l, r, x; }; V qs(q); for (int i = 0; i < q; ++i) { int tp; cin >> tp; int l, r, x; cin >> l >> r >> x, --l; qs[i] = {i, l, r, x}; } sort(begin(qs), end(qs), [](const Q& l, const Q& r) { return l.x > r.x; }); V<> idx(n); iota(begin(idx), end(idx), 0); sort(begin(idx), end(idx), [&](int i, int j) { return a[i] > a[j]; }); V res(q); auto itr = begin(idx); SegmentTree st(n); for (const auto& e : qs) { while (itr != end(idx) and a[*itr] >= e.x) { st.set(*itr, {a[*itr], 1}); ++itr; } auto p = st.acc(e.l, e.r); res[e.id] = p.first - p.second * e.x; } for (lint e : res) { cout << e << '\n'; } }