#include using namespace std; using ll = long long; template struct fenwick_tree { using U = T; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int N, Q, l, r, x; cin >> N >> Q; vector a(N), ca; vector> query(Q); vector ans(Q); for(auto &&v : a) cin >> v; ca = a; ca.reserve(N + Q); for(int i = 0; i < Q; i++){ cin >> l >> l >> r >> x; query[i] = make_tuple(--l, r, x); ca.emplace_back(x); } sort(ca.begin(), ca.end()); ca.erase(unique(ca.begin(), ca.end()), ca.end()); int m = ca.size(); vector> tb(m), tb2(m); for(int i = 0; i < N; i++){ tb[lower_bound(ca.begin(), ca.end(), a[i]) - ca.begin()].emplace_back(i); } for(int i = 0; i < Q; i++){ tb2[lower_bound(ca.begin(), ca.end(), get<2>(query[i])) - ca.begin()].emplace_back(i); } fenwick_tree fw1(N), fw2(N); for(int i = m - 1; i >= 0; i--){ for(auto &&p : tb[i]){ fw1.add(p, a[p]); fw2.add(p, 1); } for(auto &&p : tb2[i]){ tie(l, r, x) = query[p]; ans[p] = fw1.sum(l, r) - fw2.sum(l, r) * x; } } for(auto &&v : ans) cout << v << '\n'; }