#include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define let auto const template struct dynarr: std::vector { using std::vector::vector; using size_type = typename std::vector::size_type; auto&& operator[](size_type i) { return this->at(i); } auto&& operator[](size_type i) const { return this->at(i); } }; #include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define let auto const struct segment_tree { using T = pair; T ope(const T& a, const T& b) { return { a.first + b.first , a.second + b.second }; } T ide() { return { 0, 0 }; } i64 n; vector node; segment_tree(vector init) { n = 1; while(n < init.size()) n *= 2; node.resize(2 * n); rep(i, 0, init.size()) node[i + n] = init[i]; for(int i = n - 1; i >= 1;i--) node[i] = ope(node[i * 2], node[i * 2 + 1]); } void update(i64 i, T x) { i += n; node[i] = x; while(i > 1) { i = i / 2; node[i] = ope(node[i * 2], node[i * 2 + 1]); } } /* [l, r] */ T get(i64 l, i64 r) { T lx = ide(); T rx = ide(); l += n; r += n; while(l < r) { if(l & 1) { lx = ope(lx, node[l]); } if(!(r & 1)) { rx = ope(node[r], rx); } l = (l + 1) >> 1; r = (r - 1) >> 1; } if(l == r) { lx = ope(lx, node[l]); } return ope(lx, rx); } }; int main() { i64 n, q; cin >> n >> q; vector a(n); vector> vec; vector> vec2; rep(i,0,n) cin >> a[i]; rep(i,0,n) vec.push_back({ a[i], i }); rep(i,0,n) vec2.push_back({ a[i], 1 }); segment_tree seg(vec2); sort(all(vec)); i64 ii = 0; vector, pair>> query; rep(i,0,q) { i64 com, l, r, x; cin >> com >> l >> r >> x; query.push_back({ {x, i}, { l, r } }); } vector ans(q); sort(all(query)); for(auto qq: query) { i64 l = qq.second.first; i64 r = qq.second.second; i64 x = qq.first.first; i64 ai = qq.first.second; while(ii < vec.size() && vec[ii].first - x <= 0) { i64 i = vec[ii].second; seg.update(i,{0, 0}); ii++; } auto res = seg.get(l - 1, r - 1); ans[ai] = res.first - res.second * x; } rep(i,0,q) { cout << ans[i] << endl; } }