// warm heart, wagging tail,and a smile just for you! // // ▒█████▒▒ // ██████████▒ // ▒████████████▒ // ██████████████████ // ████████████████████▒ // ▒██████████████████████▒ // ▒███████████████████████ // ▒████▒▒▒▒▒▒█████████████████▒ // ███▒▒▒▒▒▒██████████████████████▒▒▒ // ▒██▒▒███████████████████████▒▒▒▒▒██████ // ▒█████████████████████████▒▒▒▒▒▒█████████▒ // ▒█████████████████████▒▒▒▒▒▒██████████████ // ▒████ ████▒▒▒▒▒████ ████▒ // ▒█████▒ ████ ▒▒▒▒███████ ████ ██████▒ // ▒██▒▒▒▒▒ ██████ █████████ ██████ ██▒▒▒██▒ // █████████ ████████ █████████ ████████ ▒▒▒▒█████ // ▒█████████ ██████ ████████▒ ██████ █████████ // ▒██████████ ████ █████▒▒▒▒▒▒ ████ ██████████ // ████████████ ▒▒▒▒▒▒▒████████ ███████████▒ // ▒██████████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████████████████████████████████▒ // ███▒▒▒▒▒▒▒▒▒▒▒▒█████████████████████████████████████████▒▒████████▒ // ▒▒▒▒▒▒▒▒▒██████████████ ███████▒▒▒▒███████████ // █████████████████████████ ███████▒▒▒██████████████▒ // █████████████████████████████ ███████▒▒▒██████████████████▒ // ██████████████████████████████████████████████████████████████████████ // ██████████████████████████████████████████████████████████████████▒ // ▒█████████████████▒▒▒▒▒▒▒██████████████████████████████████▒▒▒ // #include "bits/stdc++.h" using namespace std; #define MOD 1000000007 //#define MOD 998244353 const double EPS = 1e-9; #define INF (1LL<<60) #define D double #define fs first #define sc second #define int long long #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i) #define REP(i,n) FOR(i,0,(n)) #define RREP(i,n) RFOR(i,0,(n)) #define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i,a,b) ((a)<=(i) && (i)<(b)) #define debug(x) cout << #x << " = " << (x) << endl; #define SP << " " << typedef pair P; typedef vector vec; typedef vector> mat; template struct SegmentTree{ using F = function; int n; F f; T ti; vector dat; SegmentTree(){}; SegmentTree(F f,T ti):f(f),ti(ti){} void init(int n_){ n=1; while(n &v){ int n_=v.size(); init(n_); for(int i=0;i>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } void add(int k,T x){ dat[k+=n]+=x; while(k>>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } T query(int a,int b){ T vl=ti,vr=ti; for(int l=a+n,r=b+n;l>=1,r>>=1) { if(l&1) vl=f(vl,dat[l++]); if(r&1) vr=f(dat[--r],vr); } return f(vl,vr); } template int find(int a, int b, C &check, T x, int k=1, int l=0, int r=-1){ if(r<0)r=n; if(!check(f(x,dat[k]))||r<=a||b<=l)return -1; if(r-l==1)return l; int xl = find(a,b,check,x,(k<<1),l,(l+r)/2); if(xl>=0)return xl; x = f(x,dat[(k<<1)]); return find(a,b,check,x,(k<<1)|1,(l+r)/2,r); } template int find(int a, int b, C &check){ T x=ti; return find(a,b,check,x); } }; signed main(){ ios::sync_with_stdio(false); cin.tie(0); int n,q; cin >> n >> q; vector

a(n); REP(i,n) cin >> a[i].fs, a[i].sc = i; sort(a.rbegin(),a.rend()); auto f = [](int a,int b){return a+b;}; SegmentTree seg1(f,0), seg2(f,0); seg1.build(vec(n,0)); seg2.build(vec(n,0)); vec ans(q); using T = tuple; vector b(q); REP(i,q){ int x,y,z,w; cin >> w >> x >> y >> z; b[i] = T(z,x-1,y,i); } sort(b.rbegin(),b.rend()); int id = 0; REP(i,q){ int x,l,r,in; tie(x,l,r,in) = b[i]; while(id= x){ seg1.update(a[id].sc,a[id].fs); seg2.update(a[id].sc,1); id++; } ans[in] = seg1.query(l,r) - seg2.query(l,r)*x; } REP(i,q) cout << ans[i] << "\n"; return 0; }