// #define _GLIBCXX_DEBUG // for STL debug (optional) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long int; using int64 = long long int; template void chmax(T &a, T b) {a = max(a, b);} template void chmin(T &a, T b) {a = min(a, b);} template void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; // 抽象 SegmentTree (0-indexed・一点更新・区間取得) template struct SegmentTree { using Function = function< MonoidType(MonoidType, MonoidType) >; // node, identity element int n; vector node; MonoidType E0; // update / combine function Function upd_f, cmb_f; void build(int m, vector v = vector()) { if(v != vector()) m = v.size(); n = 1; while(n < m) n *= 2; node = vector(2*n-1, E0); if(v != vector()) { for(int i=0; i=0; i--) { node[i] = cmb_f(node[2*i+1], node[2*i+2]); } } } // initialize SegmentTree() {} SegmentTree(int n_, MonoidType E0_, Function upd_f_, Function cmb_f_, vector v = vector()) : E0(E0_), upd_f(upd_f_), cmb_f(cmb_f_) { build(n_, v); } // update k-th element (applied value: x) void update(int k, MonoidType x) { k += n - 1; node[k] = upd_f(node[k], x); while(k > 0) { k = (k - 1) / 2; node[k] = cmb_f(node[2*k+1], node[2*k+2]); } } // range query for [a, b) // 非再帰のアイデア: http://d.hatena.ne.jp/komiyam/20131202/1385992406 MonoidType query(int a, int b) { MonoidType vl = E0, vr = E0; for(int l=a+n, r=b+n; l>=1, r>>=1) { if(l & 1) vl = cmb_f(vl, node[(l++)-1]); if(r & 1) vr = cmb_f(node[(--r)-1], vr); } return cmb_f(vl, vr); } }; int main() { int N, Q; cin >> N >> Q; vector A(N); vector< tuple > queries; for(int i=0; i> A[i]; queries.emplace_back(A[i], -1, -1, i, 1); } for(int i=0; i> q >> l >> r >> x; l--; r--; queries.emplace_back(x, l, r, i, 2); } SegmentTree seg(N, 0, [](int a, int b) { return a + b; }, [](int a, int b) { return a + b; }); SegmentTree cnt(N, 0, [](int a, int b) { return a + b; }, [](int a, int b) { return a + b; }); sort(queries.begin(), queries.end(), [](auto x, auto y) { int xv, xl, xr, xt; tie(xv, xl, xr, std::ignore, xt) = x; int yv, yl, yr, yt; tie(yv, yl, yr, std::ignore, yt) = y; if(xv != yv) return xv > yv; if(xt != yt) return xt < yt; return xl < yl; }); vector ans(Q); for(auto e : queries) { int v, l, r, k, t; tie(v, l, r, k, t) = e; if(t == 1) { seg.update(k, v); cnt.update(k, 1); } if(t == 2) { r++; ans[k] = seg.query(l, r) - cnt.query(l, r) * v; } } for(int i=0; i