#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; struct ST { ll x, y; ST():x(0),y(0){} ST(ll x, ll y):x(x),y(y){} }; ST combine(const ST& lhs, const ST& rhs){ ST st; st.x = lhs.x + rhs.x; st.y = lhs.y + rhs.y; return st; } template class SegmentTree { int N; vector t; void build(){ for(int i=N-1; i>0; i--){ t[i] = combine(t[i<<1], t[i<<1|1]); } } public: SegmentTree(int n_){ N = 1; while(N < n_) N *= 2; t.resize(2*N); for(int i=N; i<2*N; i++){ t[i] = S(); } build(); } SegmentTree(const vector& v){ N = 1; while(N < v.size()) N *= 2; t.resize(2*N); for(int i=0; i>=1;){ t[p] = combine(t[p<<1], t[p<<1|1]); } } S query(int l, int r){ S resl, resr; for(l+=N, r+=N; l>=1, r>>=1){ if(l&1) resl = combine(resl, t[l++]); if(r&1) resr = combine(t[--r], resr); } return combine(resl, resr); } }; struct X { int i; int l, r; ll x; }; bool operator<(const X& a, const X& b){ return a.x > b.x; } int main(){ int N, Q; cin >> N >> Q; priority_queue> que; rep(i,N){ ll a; cin >> a; que.push(make_pair(a,i)); } vector query; rep(i,Q){ X st; cin >> st.i >> st.l >> st.r >> st.x; st.l--; st.r--; st.i = i; query.push_back(st); } sort(ALLOF(query)); vector ret(Q); SegmentTree segtree(N+1); ll sum = 0; ll prev = -1; rep(i,query.size()){ ll x = query[i].x; while(true){ if(que.empty()) break; pair p = que.top(); if(p.first >= x){ segtree.modify(p.second,ST(p.first,1)); que.pop(); }else{ break; } } ST st = segtree.query(query[i].l, query[i].r+1); ret[query[i].i] = st.x - st.y * x; } rep(i,ret.size()){ cout << ret[i] << endl; } return 0; }