#include #define int long long #define mod (int)(1e9+7) #define inf (int)(3e18) #define rep(i,n) for(int i=0;i #define PiP pair> #define PP pair #define all(v) v.begin(),v.end() #define mkp make_pair #define mkt make_tuple #define prique(T) priority_queue,greater> #define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end()) using namespace std; template inline void chmax(T& a, T b) { a = max(a, b); } template inline void chmin(T& a, T b) { a = min(a, b); } bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0)return false; } return x != 1; } int gcd(int x, int y) { if (y == 0)return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m) { int res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x)return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } int get_rand(int MIN, int MAX) { random_device rnd; mt19937_64 mt64(rnd()); uniform_int_distributionengine(MIN, MAX); return engine(mt64); } /*--------Library Zone!--------*/ struct BIT { int size; vectorbit, ko; BIT(int x) { size = x + 1; bit.resize(size, 0); ko.resize(size, 0); } P sum(int x) { P s = { 0,0 }; while (x) { s.first += bit[x]; s.second += ko[x]; x -= x & -x; } return s; } void add(int x, int y) { while (x < size) { bit[x] += y; ko[x]++; x += x & -x; } } }; int n, q; struct info { int type, x, l, r, index; bool operator<(const info& a)const { return x > a.x; } bool operator>(const info& a)const { return x < a.x; } }; vectorquery; int ans[114514]; signed main() { cin >> n >> q; rep(i, n) { int a; cin >> a; query.push_back({ 1,a,i + 1,i + 1 ,0 }); } rep(i, q) { int l, r, x; cin >> l >> l >> r >> x; query.push_back({ 2,x,l,r,i }); } sort(all(query)); BIT B(n); for (info p : query) { if (p.type == 1) { B.add(p.l, p.x); } else { P ri = B.sum(p.r); P le = (p.l != 1 ? B.sum(p.l - 1) : P{ 0, 0 }); ans[p.index] = ri.first - le.first - p.x * (ri.second - le.second); } } rep(i, q) { cout << ans[i] << endl; } }