#include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; struct P { P &operator+=(const P &p) { s += p.s; c += p.c; return *this; } ll s; int c; }; struct BIT { BIT(int n) : b(n + 1), n(n) {} void add(int i, P v) { for (int k = i + 1; k <= n; k += k & -k) b[k] += v; } P sum(int k) { P s = { 0, 0 }; for (; k > 0; k -= k & -k) s += b[k]; return s; } vector

b; int n; }; struct Q { bool operator<(const Q &q) const { return x > q.x; } ll s; int l, r, x, h; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a.begin(), a.end(), greater<>()); vector q(m); for (int h = 0; h < m; h++) { int t, l, r, x; cin >> t >> l >> r >> x; l--; q[h] = { 0, l, r, x, h }; } sort(q.begin(), q.end()); BIT bt(n); int i = 0; for (int h = 0; h < m; h++) { int x = q[h].x; for (; i < n && a[i].first > x; i++) { bt.add(a[i].second, { a[i].first, 1 }); } P p0 = bt.sum(q[h].l); P p1 = bt.sum(q[h].r); q[h].s = (p1.s - p0.s) - (p1.c - p0.c) * (ll)x; } vector r(n); for (int h = 0; h < m; h++) { r[q[h].h] = q[h].s; } for (int h = 0; h < m; h++) { cout << r[h] << '\n'; } return 0; }