#include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; #define LLINF 9223372036854775807 #define MOD ll(1e9+7) #define all(x) (x).begin(),(x).end() #define dbg(x) cerr<<#x<<": "< argsort(const vector &v) { vector index(v.size()); iota(index.begin(), index.end(), 0); sort(index.begin(), index.end(),[&v](size_t i1, size_t i2) { return v[i1] < v[i2]; }); return index; } class segtree{ public: segtree(ll size, vector &v){ n = 1; while(n < size){n *= 2;} data = vector(2*n-1, 0); build(v); } void build(vector &v){ for(int i = 0; i < v.size(); i++){ data[n-1+i] = v[i]; } for(int i = n-2; i >= 0; i--){ data[i] = data[2*i+1] + data[2*i+2]; } } void update(ll k, ll a){ k += n-1; data[k] = a; while(k>0){ k = (k-1)/2; data[k] = data[2*k+1] + data[2*k+2]; } } ll getSize(){return n;} ll getData(ll x){return data[n-1+x];} ll query(ll a, ll b, ll k, ll l, ll r){ if(r <= a || b <= l){return 0;} if(a <= l && r <= b){return data[k];} else{ ll vl = query(a, b, 2*k+1, l, (l+r)/2); ll vr = query(a, b, 2*k+2, (l+r)/2, r); return vl + vr; } } void printAllTree(){ ll cnt = 0; ll br = 1; for(int i = 0; i < data.size(); i++){ cout << data[i] << " "; cnt++; if(cnt == br){ cout << endl; cnt = 0; br *= 2; } } } private: ll n; vector data; }; int main(){ ll n, q; cin >> n >> q; vector a(n); vector alive(n,0); for(int i = 0; i < n; i++){ cin >> a[i]; if(a[i] > 0){alive[i] = 1;} } segtree st(n, a); segtree st_alive(n, alive); vector l(q), r(q), x(q); ll t; for(int i = 0; i < q; i++){ cin >> t >> l[i] >> r[i] >> x[i]; l[i]--; r[i]--; } vector order_x = argsort(x); vector sorted = x; sort(all(sorted)); vector> deadline(q+1,vector()); for(int i = 0; i < n; i++){ auto itr = lower_bound(all(sorted),a[i]); deadline[itr-sorted.begin()].push_back(i); } vector ans(q,0); for(int i = 0; i < q; i++){ for(int j = 0; j < deadline[i].size(); j++){ st.update(deadline[i][j], 0); st_alive.update(deadline[i][j],0); } ll ret = st.query(l[order_x[i]], r[order_x[i]]+1, 0, 0, st.getSize()); ll ret_alive = st_alive.query(l[order_x[i]], r[order_x[i]]+1, 0, 0, st.getSize()); ans[order_x[i]] = ret - ret_alive*sorted[i]; } for(int i = 0; i < q; i++){ cout << ans[i] << endl; } return 0; }