#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; struct query { ll val; ll type; ll l; ll r; ll index; query(ll a, ll b, ll c, ll d, ll e) { val = a; type = b; l = c; r = d; index = e; } }; bool operator > (query a, query b) { return a.val > b.val; } bool operator < (query a, query b) { return a.val < b.val; } template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //const ll mod = 1000000007; struct SegmentTree { private: int n; vector node; public: SegmentTree() { int sz = 100050; n = 1; while(n < sz) n *= 2; node.resize(2*n-1, 0); for(int i=0; i=0; i--) node[i] = min(node[2*i+1], node[2*i+2]); } void update(int x, int val) { x += (n - 1); node[x] = val; while(x > 0) { x = (x - 1) / 2; node[x] = (node[2*x+1] + node[2*x+2]); } } // hannkaikukann ll getsum(int a, int b, int k=0, int l=0, int r=-1) { if(r < 0) r = n; if(r <= a || b <= l) return 0; if(a <= l && r <= b) return node[k]; ll vl = getsum(a, b, 2*k+1, l, (l+r)/2); ll vr = getsum(a, b, 2*k+2, (l+r)/2, r); return (vl + vr); } }; ll ans[105000]; int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; SegmentTree val, num; vector v; for(int i = 1; i <= N; i++) { ll a; cin >> a; v.emplace_back(a, 1, -1, -1, i); } for(int i = 1; i <= Q; i++) { ll a, l, r, x; cin >> a >> l >> r >> x; v.emplace_back(x, 2, l, r, i); } sort(v.begin(), v.end(), greater()); for(int i = 0; i < v.size(); i++) { if(v[i].type == 1) { val.update(v[i].index, v[i].val); num.update(v[i].index, 1); continue; } else { ll VAL = val.getsum(v[i].l, v[i].r + 1); ll NUM = num.getsum(v[i].l, v[i].r + 1); ans[v[i].index] = VAL - NUM * v[i].val; } } for(int i = 1; i <= Q; i++) { cout << ans[i] << endl; } return 0; }