#include #include #include #include using namespace std; using pii = pair; #define rep(i,n) for(int i=0;i<(n);++i) #include template struct SegTree { private: int size; vector v; using F = function; F f; T default_value; public: SegTree(int n, T default_value, F f) : default_value(default_value), f(f) { size = 1; while (size < n) size *= 2; v.resize(2 * size - 1, default_value); } void update(int index, T val) { index = index + size - 1; v[index] = val; while (index > 0) { index = (index - 1) / 2; v[index] = f(v[2 * index + 1], v[2 * index + 2]); } } T query(int l, int r) { return query(l, r, 0, size, 0); } T query(int l, int r, int cur_l, int cur_r, int index) { // [l, r)に対するクエリ if (cur_r <= l || r <= cur_l) return default_value; if (l <= cur_l && cur_r <= r) return v[index]; int mid = (cur_l + cur_r) / 2; return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2)); } }; int main() { int N; cin >> N; int Q; cin >> Q; priority_queue> q; rep(i, N) { int a; cin >> a; q.emplace(pii(a, 0), i); } map> m; rep(i, Q) { int t; cin >> t; int l, r, x; cin >> l >> r >> x; --l; q.emplace(pii(x, 1), i); m[x].emplace_back(l, r); } SegTree T(N, 0, [](int a, int b){return a + b;}); SegTree T2(N, 0, [](int a, int b){return a + b;}); vector ans(Q); while (!q.empty()) { auto p = q.top(); q.pop(); if (p.first.second == 0) { T.update(p.second, p.first.first); T2.update(p.second, 1); } else { int l = m[p.first.first].back().first, r = m[p.first.first].back().second; m[p.first.first].pop_back(); ans[p.second] = T.query(l, r) - T2.query(l, r) * p.first.first; } } rep(i, Q) cout << ans[i] << endl; return 0; }