#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// /* #include #include #include using namespace __gnu_pbds; // find_by_order(), order_of_key() template using pbds_set = tree, rb_tree_tag, tree_order_statistics_node_update>; template using pbds_map = tree, rb_tree_tag, tree_order_statistics_node_update>; */ using T = vector; struct SegTree { int N; int head; vector x; T defaultT; using func = function; func merger; lint _get(int begin, int end, int pos, int l, int r, lint q) const { if (r <= begin or l >= end) return 0; if (l >= begin and r <= end) { int i = lower_bound(ALL(x[pos]), plint(q, 0)) - x[pos].begin(); if (i == (int)x[pos].size()) return 0; else return x[pos].back().second - (i ? x[pos][i - 1].second : 0) - q * (x[pos].size() - i); } return _get(begin, end, 2 * pos + 1, l, (l + r) / 2, q) + _get(begin, end, 2 * pos + 2, (l + r) / 2, r, q); } SegTree(int N, T defaultT, func merger) : N(N), defaultT(defaultT), merger(merger) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; } SegTree(const vector &vals, T defaultT, func merger) : N(vals.size()), defaultT(defaultT), merger(merger) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; copy(vals.begin(), vals.end(), x.begin() + head); IREP(i, head) x[i] = merger(x[i * 2 + 1], x[i * 2 + 2]); } SegTree() : SegTree(0, T(), [](T, T) { return T(); }) {} void build(const vector &vals) { copy(vals.begin(), vals.end(), x.begin() + head); IREP(i, head) x[i] = merger(x[i * 2 + 1], x[i * 2 + 2]); } void update(int pos, T val) { pos += head, x[pos] = val; while (pos) pos = (pos - 1) / 2, x[pos] = merger(x[pos*2+1], x[pos*2+2]); } lint get(int begin, int end, lint q) const { return _get(begin, end, 0, 0, (int)x.size() / 2, q); } // friend ostream &operator<<(ostream &os, const SegTree &s) { os << "["; REP(i, s.N) os << s.get(i, i + 1) << ","; os << "]"; return os; } }; // 参考 要素数N・非負入力を想定したRange Maximum Query // SegTree rMq(N, 0, [](int a, int b) { return max(a, b); }); int main() { int N, Q; cin >> N >> Q; vector> A(N); REP(i, N) { lint a; cin >> a; A[i].emplace_back(a, a); } SegTree st(A, vector(), [](T l, T r) { if (l.empty()) return r; if (r.empty()) return l; vector ret; int N = l.size(); int M = r.size(); int i = 0, j = 0; while (true) { if (i >= N and j >= M) break; if (i >= N or (j < M and l[i].first >= r[j].first)) { ret.emplace_back(r[j].first, r[j].first); j++; } else { ret.emplace_back(l[i].first, l[i].first); i++; } REP(i, ret.size() - 1) ret[i + 1].second = ret[i + 1].first + ret[i].second; } return ret; }); REP(_, Q) { int q, l, r, x; cin >> q >> l >> r >> x; cout << st.get(l - 1, r, x) << endl; } }