#include #include #include #include using namespace std; #define F first #define S second constexpr int kN = int(1E5 + 10); typedef long long int ll; struct Query { int l, r, x, id; }; struct BIT { ll val[kN]; void init() {memset(val, 0, sizeof(val));} void add(int pos, int x) { while (pos < kN) { val[pos] += x; pos += pos & -pos; } return ; } ll ask(int pos) { ll ans = 0; while (pos) { ans += val[pos]; pos ^= pos & -pos; } return ans; } }; bool cmp(Query a, Query b) {return a.x < b.x;} BIT cnt, tot; int a[kN]; ll ans[kN]; pair na[kN]; Query query[kN]; int main() { int n, q, k, now = 1; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= q; i++) scanf("%d%d%d%d", &k, &query[i].l, &query[i].r, &query[i].x); for (int i = 1; i <= q; i++) query[i].id = i; cnt.init(); tot.init(); for (int i = 1; i <= n; i++) tot.add(i, a[i]); for (int i = 1; i <= n; i++) cnt.add(i, 1); for (int i = 1; i <= n; i++) na[i] = {a[i], i}; sort(na + 1, na + n + 1); sort(query + 1, query + q + 1, cmp); for (int i = 1; i <= q; i++) { while (now <= n) { if (na[now].F < query[i].x) { cnt.add(na[now].S, -1); tot.add(na[now].S, -na[now].F); now++; } else break; } ans[query[i].id] = tot.ask(query[i].r) - tot.ask(query[i].l - 1) - (cnt.ask(query[i].r) - cnt.ask(query[i].l - 1)) * query[i].x; } for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]); }