//#pragma GCC optimize("Ofast") //#pragma GCC target("avx") //#undef LOCAL #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template using V = vector; template using VV = V>; #ifdef LOCAL struct PrettyOS { ostream& os; bool first; template auto operator<<(T&& x) { if (!first) os << ", "; first = false; os << x; return *this; } }; template void dbg0(T&&... t) { (PrettyOS{cerr, true} << ... << t); } #define dbg(...) \ do { \ cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \ dbg0(__VA_ARGS__); \ cerr << endl; \ } while (false); #else #define dbg(...) #endif template ostream& operator<<(ostream& os, const pair& p) { return os << "P(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& os, const V& v) { os << "["; for (auto d : v) os << d << ", "; return os << "]"; } template struct Fenwick { int n; V seg; Fenwick(int _n = 0) : n(_n), seg(n + 1) {} /// i番目の要素にxを追加する void add(int i, T x) { i++; while (i <= n) { seg[i] += x; i += i & -i; } } /// [0, i)のsum T sum(int i) { T s = 0; while (i > 0) { s += seg[i]; i -= i & -i; } return s; } /// [a, b)のsum T sum(int a, int b) { return sum(b) - sum(a); } /// sum[0, idx) >= xなる最小のidx(sum[0, n) < x なら n+1) int sum_lower_bound(T x) { if (x <= 0) return 0; int res = 0, len = 1; while (2 * len <= n) len *= 2; for (; len >= 1; len /= 2) { if (res + len <= n && seg[res + len] < x) { res += len; x -= seg[res]; } } return res + 1; } }; template struct Fenwick2D { using P = pair; V

points; VV ys; V> fws; int lg, sz; Fenwick2D(V

_points) : points(_points) { sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); int n = int(points.size()); lg = 1; while ((1 << lg) < n) lg++; sz = 1 << lg; ys = VV(2 * sz); for (int i = 0; i < n; i++) ys[sz + i].push_back(points[i].second); for (int i = sz - 1; i >= 1; i--) { ys[i] = V(ys[2 * i].size() + ys[2 * i + 1].size()); merge(ys[2 * i].begin(), ys[2 * i].end(), ys[2 * i + 1].begin(), ys[2 * i + 1].end(), ys[i].begin()); } fws = V>(2 * sz); for (int i = 1; i < 2 * sz; i++) { fws[i] = Fenwick(int(ys[i].size())); } } void add(P p, D x) { int k = int(lower_bound(points.begin(), points.end(), p) - points.begin()); k += sz; while (k) { int yid = lower_bound(ys[k].begin(), ys[k].end(), p.second) - ys[k].begin(); fws[k].add(yid, x); k >>= 1; } } D sum(int a, int b, I lw, I up, int l, int r, int k) { if (b <= l || r <= a) return D(0); if (a <= l && r <= b) { int lid = lower_bound(ys[k].begin(), ys[k].end(), lw) - ys[k].begin(); int uid = lower_bound(ys[k].begin(), ys[k].end(), up) - ys[k].begin(); return fws[k].sum(lid, uid); } int mid = (l + r) / 2; return sum(a, b, lw, up, l, mid, 2 * k) + sum(a, b, lw, up, mid, r, 2 * k + 1); } D sum(P lw, P up) { int a = lower_bound(points.begin(), points.end(), lw.first, [&](P p, I x) { return p.first < x; }) - points.begin(); int b = lower_bound(points.begin(), points.end(), up.first, [&](P p, I x) { return p.first < x; }) - points.begin(); return sum(a, b, lw.second, up.second, 0, sz, 1); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; using P = pair; V

a(n); for (int i = 0; i < n; i++) { a[i].first = i; cin >> a[i].second; } auto fen2d0 = Fenwick2D(a); auto fen2d1 = Fenwick2D(a); for (auto p: a) { fen2d0.add(p, 1); fen2d1.add(p, p.second); } for (int ph = 0; ph < q; ph++) { int ty, l, r; ll x; cin >> ty >> l >> r >> x; l--; ll y = fen2d0.sum(P(l, x), P(r, TEN(9) + 1)); ll z = fen2d1.sum(P(l, x), P(r, TEN(9) + 1)); cout << z - y * x << "\n"; } return 0; }