#include using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long LL; typedef pair P; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template T gcd(T a,T b){return b?gcd(b,a%b):a;} const LL mod=1000000007; const LL LINF=1LL<<60; const int INF=1<<30; int dx[]={1,0,-1,0,1,-1,1,-1}; int dy[]={0,1,0,-1,1,-1,-1,1}; template< typename T > struct BinaryIndexedTree{ //0-indexed vector< T > bit; BinaryIndexedTree(int sz){ bit.assign(sz, 0); } //[0,k)までの総和を返す T sum(int k){ T ret = 0; for (--k; k >= 0; k = (k&(k+1))-1) ret += bit[k]; return ret; } //[l,r)の総和を返す T sum(int l, int r){ return sum(r) - sum(l); } //kにx加算する void add(int k, T x){ for (; k < bit.size(); k |= k+1) bit[k] += x; } }; int main(){ int n,q;cin >> n >> q; vector

a(n); for (int i = 0; i < n; i++) { cin >> a[i].fs; a[i].sc = i; } BinaryIndexedTree S(n); for (int i = 0; i < n; i++) { S.add(i,a[i].fs); } sort(ALL(a)); vector> Q(q); for (int i = 0; i < q; i++) { int t,l,r,x;cin >> t >> l >> r >> x; l--; Q[i] = mp(mp(x,i), mp(l,r)); } sort(ALL(Q)); BinaryIndexedTree bit(n); int idx = 0; vector ans(q); for (int i = 0; i < q; i++) { LL x = Q[i].fs.fs, id = Q[i].fs.sc; int l = Q[i].sc.fs, r = Q[i].sc.sc; while(idx < n && a[idx].fs <= x){ bit.add(a[idx].sc, 1); S.add(a[idx].sc, -a[idx].fs); idx++; } ans[id] = S.sum(l,r) - x * (r - l - bit.sum(l,r)); } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }