#include #include #include #include #include template using MinHeap = std::priority_queue, std::greater>; template struct SegmentTree { using Merger = std::function; int length; std::vector dat; T unit; Merger merge; SegmentTree() = default; SegmentTree(int n, const T& unit, const Merger& merge) : length(1), unit(unit), merge(merge) { while (length < n) length <<= 1; dat.assign(length * 2, unit); } template SegmentTree(const Container& elems, const T& unit, const Merger& merge) : length(1), unit(unit), merge(merge) { int n = elems.size(); while (length < n) length <<= 1; dat.assign(length * 2, unit); std::copy(elems.begin(), elems.end(), dat.begin() + length); for (int nidx = length - 1; nidx >= 1; --nidx) { T vl = dat[nidx * 2 + 0]; T vr = dat[nidx * 2 + 1]; dat[nidx] = merge(vl, vr); } } void update(int nidx, const T& elem) { nidx += length; dat[nidx] = elem; while (nidx > 0) { nidx >>= 1; T vl = dat[nidx * 2 + 0]; T vr = dat[nidx * 2 + 1]; dat[nidx] = merge(vl, vr); } } T fold(int ql, int qr) const { ql = std::max(ql, 0); qr = std::min(qr, length); ql += length, qr += length; T lacc = unit, racc = unit; while (ql < qr) { if (ql & 1) { lacc = merge(lacc, dat[ql]); ++ql; } if (qr & 1) { --qr; racc = merge(dat[qr], racc); } ql >>= 1, qr >>= 1; } return merge(lacc, racc); } T get(int idx) const { return dat[idx + length]; } T fold_all() const { return dat[1]; } }; using lint = long long; struct Query { int l, r, id; lint x; }; void solve() { int n, q; std::cin >> n >> q; std::vector xs(n); for (auto& x : xs) std::cin >> x; std::vector ts(q); for (int i = 0; i < q; ++i) { auto& t = ts[i]; std::cin >> t.id >> t.l >> t.r >> t.x; --t.l; t.id = i; } std::sort(ts.begin(), ts.end(), [](Query lhs, Query rhs) { return lhs.x < rhs.x; }); SegmentTree sumseg(xs, 0, [](auto a, auto b) { return a + b; }), cntseg(n, 0, [](auto a, auto b) { return a + b; }); for (int i = 0; i < n; ++i) cntseg.update(i, 1); MinHeap> heap; for (int i = 0; i < n; ++i) heap.emplace(xs[i], i); std::vector ans(q); for (auto t : ts) { while (!heap.empty() && heap.top().first <= t.x) { int i = heap.top().second; heap.pop(); sumseg.update(i, 0); cntseg.update(i, 0); } ans[t.id] = sumseg.fold(t.l, t.r) - cntseg.fold(t.l, t.r) * t.x; } for (auto a : ans) std::cout << a << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }