#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif #include using S = long long; S op(S a, S b) { return a + b; } S e() { return 0; } void solve() { int N, Q; cin >> N >> Q; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; set> st; for (int i = 0; i < N; i++) st.insert({A[i], i}); vector> Qs(Q); for (int i = 0; i < Q; i++) { int t; cin >> t; int l, r, x; cin >> l >> r >> x; Qs[i] = {x, l, r, i}; } debug(Qs); // cnt, sum atcoder::segtree cnt(N); atcoder::segtree sum(A); sort(Qs.begin(), Qs.end()); auto Query = [&](int l, int r, long long x) -> S { l--; return x * cnt.prod(l, r) + sum.prod(l, r) - x * (r - l); }; vector ans(N); for (int ii = 0; ii < Q; ii++) { auto [x, l, r, i] = Qs[ii]; while (!st.empty() && st.begin()->first <= x) { auto it = st.begin(); auto [val, idx] = *it; st.erase(it); cnt.set(idx, 1); sum.set(idx, 0); } ans[i] = Query(l, r, x); } for (int i = 0; i < Q; i++) cout << ans[i] << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }