#include #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr double eps = 1e-9; constexpr ll MOD = 1000000007LL; template bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }; template bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }; template ostream &operator<<(ostream &os, vector v) { for (int i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "\n" : " "); } return os; } template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } template typename enable_if::value == 0>::type fill_v(T &t, const V &v) { t = v; } template typename enable_if::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) { fill_v(e, v); } }; struct Segtree { int n; vector dat; Segtree(int n_) { n = 1; while (n < n_) { n <<= 1; } dat.resize(2 * n, 0); } void upd(int k, int x) { k += n; dat[k] = x; k >>= 1; while (k > 0) { dat[k] = dat[k << 1] + dat[k << 1 | 1]; k >>= 1; } } ll get(int a, int b, int k, int l, int r) { if (b <= l || r <= a) { return 0; } if (a <= l && r <= b) { return dat[k]; } return get(a, b, 2 * k, l, (l + r) / 2) + get(a, b, 2 * k + 1, (l + r) / 2, r); } ll get(int a, int b) { if (a >= b) { return 0; } return get(a, b, 1, 0, n); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; Segtree seg(n + 1), seg2(n + 1); vector a(n); vector

v; for (int i = 0; i < n; i++) { cin >> a[i]; seg.upd(i, a[i]); v.push_back(P(a[i], i)); } sort(all(v)); struct st { ll x, l, r, id; bool operator<(const st &to) { return x < to.x; } }; vector qs; for (int i = 0; i < q; i++) { int t, l, r, x; cin >> t >> l >> r >> x; --l; qs.push_back({x, l, r, i}); } sort(all(qs)); int id = 0; vector res(q); for (int i = 0; i < q; i++) { ll x = qs[i].x, l = qs[i].l, r = qs[i].r; while (id < n && v[id].first < x) { seg.upd(v[id].second, 0); seg2.upd(v[id].second, 1); id++; } res[qs[i].id] = seg.get(l, r); ll co = seg2.get(l, r); res[qs[i].id] -= (r - l - co) * x; } for (int i = 0; i < q; i++) { cout << res[i] << endl; } }