// generated at caterpillow.github.io/byot
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
namespace Treap {
// start custom code
struct Lazy {
// chmin and then increment
ll inc;
ll mn;
void operator+=(const Lazy& oth) {
mn = min(mn, oth.mn - inc);
inc += oth.inc;
}
};
const Lazy lid = {0, INF};
// You can implement your own monoid here for custom operations.
struct Value {
ll sum;
ll mx;
ll mxcnt;
ll mx2;
Value make(ll x) {
return {x, x, 1, -INF};
}
bool can_break(const Lazy& lazy) {
return lazy.mn >= mx && lazy.inc == 0;
}
bool can_tag(const Lazy& lazy) {
return mx2 < lazy.mn;
}
void upd(Lazy lazy, int sz) {
assert(mx2 <= lazy.mn);
if (lazy.mn < mx) {
sum -= (mx - lazy.mn) * mxcnt;
mx = lazy.mn;
}
sum += lazy.inc * sz;
mx += lazy.inc;
mx2 += lazy.inc;
}
Value operator+(const Value& oth) const {
Value res {};
res.sum = sum + oth.sum;
if (mx == oth.mx) res.mx = mx, res.mxcnt = mxcnt + oth.mxcnt, res.mx2 = max(mx2, oth.mx2);
else if (mx > oth.mx) res.mx = mx, res.mxcnt = mxcnt, res.mx2 = max(mx2, oth.mx);
else res.mx = oth.mx, res.mxcnt = oth.mxcnt, res.mx2 = max(mx, oth.mx2);
return res;
}
};
const Value vid = {0, -INF, 0, -INF};
// end custom code
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
using ptr = struct Node*;
struct Node {
Value val;
Value agg;
Lazy lazy;
int sz;
int pri;
ptr l, r;
Node() {
pri = mt();
val = vid;
agg = vid;
lazy = lid;
sz = 1;
l = r = nullptr;
}
Node(Value val) : val(val), agg(val) {
pri = mt();
lazy = lid;
sz = 1;
l = r = nullptr;
}
~Node() {
delete l;
delete r;
}
};
int sz(ptr n) { return n ? n->sz : 0; };
Value agg(ptr n) { return n ? n->agg : vid; }
void push(ptr n) {
n->val.upd(n->lazy, 1);
n->agg.upd(n->lazy, sz(n));
if (n->l) n->l->lazy += n->lazy;
if (n->r) n->r->lazy += n->lazy;
n->lazy = lid;
}
ptr pull(ptr n) {
if (!n) return nullptr;
if (n->l) push(n->l);
if (n->r) push(n->r);
n->sz = sz(n->l) + 1 + sz(n->r);
n->agg = agg(n->l) + n->val + agg(n->r);
return n;
}
ptr merge(ptr l, ptr r) {
if (!l || !r) return l ? l : r;
push(l), push(r);
if (l->pri > r->pri) return l->r = merge(l->r, r), pull(l);
else return r->l = merge(l, r->l), pull(r);
}
template<typename... Args>
ptr merge(ptr l, Args... args) {
return merge(l, merge(args...));
}
// [-inf, i) and [i, inf]
pair<ptr, ptr> spliti(ptr n, int i) {
if (!n) return {nullptr, nullptr};
push(n);
if (i <= sz(n->l)) {
auto [l, r] = spliti(n->l, i);
n->l = r;
return {l, pull(n)};
} else {
auto [l, r] = spliti(n->r, i - 1 - sz(n->l));
n->r = l;
return {pull(n), r};
}
}
// cuts out [lo, hi)
tuple<ptr, ptr, ptr> spliti(ptr n, int lo, int hi) {
auto [lm, r] = spliti(n, hi);
auto [l, m] = spliti(lm, lo);
return {l, m, r};
}
void updi(ptr n, int lo, int hi, Lazy lazy) {
if (!n) return;
push(n);
if (lo >= n->sz || hi <= 0 || n->agg.can_break(lazy)) return;
if (lo <= 0 && n->sz <= hi && n->agg.can_tag(lazy)) {
n->lazy += lazy;
push(n);
return;
}
if (lo <= sz(n->l) && sz(n->l) < hi) n->val.upd(lazy, 1);
updi(n->l, lo, hi, lazy);
updi(n->r, lo - 1 - sz(n->l), hi - 1 - sz(n->l), lazy);
pull(n);
}
void heapify(ptr n) {
if (!n) return;
ptr mx = n;
if (n->l && n->l->pri > mx->pri) mx = n->l;
if (n->r && n->r->pri > mx->pri) mx = n->r;
if (mx != n) swap(n->pri, mx->pri), heapify(mx);
}
ptr build(vector<ptr>& ns, int l = 0, int r = -69) {
if (r == -69) r = (int) ns.size() - 1;
if (l > r) return nullptr;
if (l == r) return ns[l];
int m = (l + r) / 2;
ns[m]->l = build(ns, l, m - 1);
ns[m]->r = build(ns, m + 1, r);
heapify(ns[m]);
return pull(ns[m]);
}
}
using namespace Treap;
/*
- include n-way merge (and merge)
- include 3-way split by index (and size, and split by index)
- include build (and heapify)
- include range aggregates
- include lazy prop
- include range updates by index
- enable treap beats template
*/
main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<ptr> ns(n);
for (int i = 0; i < n; i++) {
ll x; cin >> x;
ns[i] = new Node(Value().make(x));
}
ptr treap = build(ns);
while (q--) {
int t; cin >> t;
if (t == 1) {
int l, r, h; cin >> l >> r >> h;
updi(treap, l - 1, r, {0, h});
}
if (t == 2) {
int l, r; cin >> l >> r;
auto [lhs, mid, rhs] = spliti(treap, l - 1, r);
treap = merge(lhs, rhs, mid);
}
if (t == 3) {
int l, r, x; cin >> l >> r >> x;
updi(treap, l - 1, r, {x, INF});
}
push(treap);
cout << agg(treap).sum << '\n';
}
delete treap;
}
Ly8gZ2VuZXJhdGVkIGF0IGNhdGVycGlsbG93LmdpdGh1Yi5pby9ieW90CgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwoKY29uc3QgbGwgSU5GID0gMWUxODsKCm5hbWVzcGFjZSBUcmVhcCB7CgogICAgLy8gc3RhcnQgY3VzdG9tIGNvZGUKCiAgICBzdHJ1Y3QgTGF6eSB7CiAgICAgICAgLy8gY2htaW4gYW5kIHRoZW4gaW5jcmVtZW50CiAgICAgICAgbGwgaW5jOwogICAgICAgIGxsIG1uOwoKICAgICAgICB2b2lkIG9wZXJhdG9yKz0oY29uc3QgTGF6eSYgb3RoKSB7CiAgICAgICAgICAgIG1uID0gbWluKG1uLCBvdGgubW4gLSBpbmMpOwogICAgICAgICAgICBpbmMgKz0gb3RoLmluYzsKICAgICAgICB9CiAgICB9OwoKICAgIGNvbnN0IExhenkgbGlkID0gezAsIElORn07CgogICAgLy8gWW91IGNhbiBpbXBsZW1lbnQgeW91ciBvd24gbW9ub2lkIGhlcmUgZm9yIGN1c3RvbSBvcGVyYXRpb25zLgogICAgc3RydWN0IFZhbHVlIHsKICAgICAgICBsbCBzdW07CiAgICAgICAgbGwgbXg7CiAgICAgICAgbGwgbXhjbnQ7CiAgICAgICAgbGwgbXgyOwoKICAgICAgICBWYWx1ZSBtYWtlKGxsIHgpIHsKICAgICAgICAgICAgcmV0dXJuIHt4LCB4LCAxLCAtSU5GfTsKICAgICAgICB9CgogICAgICAgIGJvb2wgY2FuX2JyZWFrKGNvbnN0IExhenkmIGxhenkpIHsKICAgICAgICAgICAgcmV0dXJuIGxhenkubW4gPj0gbXggJiYgbGF6eS5pbmMgPT0gMDsKICAgICAgICB9CgogICAgICAgIGJvb2wgY2FuX3RhZyhjb25zdCBMYXp5JiBsYXp5KSB7CiAgICAgICAgICAgIHJldHVybiBteDIgPCBsYXp5Lm1uOwogICAgICAgIH0KCiAgICAgICAgdm9pZCB1cGQoTGF6eSBsYXp5LCBpbnQgc3opIHsKICAgICAgICAgICAgYXNzZXJ0KG14MiA8PSBsYXp5Lm1uKTsKICAgICAgICAgICAgaWYgKGxhenkubW4gPCBteCkgewogICAgICAgICAgICAgICAgc3VtIC09IChteCAtIGxhenkubW4pICogbXhjbnQ7CiAgICAgICAgICAgICAgICBteCA9IGxhenkubW47CiAgICAgICAgICAgIH0KICAgICAgICAgICAgc3VtICs9IGxhenkuaW5jICogc3o7CiAgICAgICAgICAgIAogICAgICAgICAgICBteCArPSBsYXp5LmluYzsKICAgICAgICAgICAgbXgyICs9IGxhenkuaW5jOwogICAgICAgIH0KCiAgICAgICAgVmFsdWUgb3BlcmF0b3IrKGNvbnN0IFZhbHVlJiBvdGgpIGNvbnN0IHsKICAgICAgICAgICAgVmFsdWUgcmVzIHt9OwogICAgICAgICAgICByZXMuc3VtID0gc3VtICsgb3RoLnN1bTsKICAgICAgICAgICAgaWYgKG14ID09IG90aC5teCkgcmVzLm14ID0gbXgsIHJlcy5teGNudCA9IG14Y250ICsgb3RoLm14Y250LCByZXMubXgyID0gbWF4KG14Miwgb3RoLm14Mik7CiAgICAgICAgICAgIGVsc2UgaWYgKG14ID4gb3RoLm14KSByZXMubXggPSBteCwgcmVzLm14Y250ID0gbXhjbnQsIHJlcy5teDIgPSBtYXgobXgyLCBvdGgubXgpOwogICAgICAgICAgICBlbHNlIHJlcy5teCA9IG90aC5teCwgcmVzLm14Y250ID0gb3RoLm14Y250LCByZXMubXgyID0gbWF4KG14LCBvdGgubXgyKTsKICAgICAgICAgICAgcmV0dXJuIHJlczsKICAgICAgICB9CiAgICB9OwoKICAgIGNvbnN0IFZhbHVlIHZpZCA9IHswLCAtSU5GLCAwLCAtSU5GfTsKCiAgICAvLyBlbmQgY3VzdG9tIGNvZGUKCiAgICBtdDE5OTM3IG10KGNocm9ubzo6c3RlYWR5X2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSk7CiAgICB1c2luZyBwdHIgPSBzdHJ1Y3QgTm9kZSo7CiAgICAKICAgIHN0cnVjdCBOb2RlIHsKICAgICAgICBWYWx1ZSB2YWw7CiAgICAgICAgVmFsdWUgYWdnOwogICAgICAgIExhenkgbGF6eTsKICAgIAogICAgICAgIGludCBzejsKICAgICAgICBpbnQgcHJpOwogICAgICAgIHB0ciBsLCByOwogICAgCiAgICAgICAgTm9kZSgpIHsKICAgICAgICAgICAgcHJpID0gbXQoKTsKICAgICAgICAgICAgdmFsID0gdmlkOwogICAgICAgICAgICBhZ2cgPSB2aWQ7CiAgICAgICAgICAgIGxhenkgPSBsaWQ7CiAgICAgICAgICAgIHN6ID0gMTsKICAgICAgICAgICAgbCA9IHIgPSBudWxscHRyOwogICAgICAgIH0KICAgIAogICAgICAgIE5vZGUoVmFsdWUgdmFsKSA6IHZhbCh2YWwpLCBhZ2codmFsKSB7CiAgICAgICAgICAgIHByaSA9IG10KCk7CiAgICAgICAgICAgIGxhenkgPSBsaWQ7CiAgICAgICAgICAgIHN6ID0gMTsKICAgICAgICAgICAgbCA9IHIgPSBudWxscHRyOwogICAgICAgIH0KICAgIAogICAgICAgIH5Ob2RlKCkgewogICAgICAgICAgICBkZWxldGUgbDsKICAgICAgICAgICAgZGVsZXRlIHI7CiAgICAgICAgfQogICAgfTsKICAgIAogICAgaW50IHN6KHB0ciBuKSB7IHJldHVybiBuID8gbi0+c3ogOiAwOyB9OwogICAgVmFsdWUgYWdnKHB0ciBuKSB7IHJldHVybiBuID8gbi0+YWdnIDogdmlkOyB9CiAgICAKICAgIHZvaWQgcHVzaChwdHIgbikgewogICAgICAgIG4tPnZhbC51cGQobi0+bGF6eSwgMSk7CiAgICAgICAgbi0+YWdnLnVwZChuLT5sYXp5LCBzeihuKSk7CiAgICAgICAgaWYgKG4tPmwpIG4tPmwtPmxhenkgKz0gbi0+bGF6eTsKICAgICAgICBpZiAobi0+cikgbi0+ci0+bGF6eSArPSBuLT5sYXp5OwogICAgICAgIG4tPmxhenkgPSBsaWQ7CiAgICB9CiAgICAKICAgIHB0ciBwdWxsKHB0ciBuKSB7CiAgICAgICAgaWYgKCFuKSByZXR1cm4gbnVsbHB0cjsKICAgICAgICBpZiAobi0+bCkgcHVzaChuLT5sKTsKICAgICAgICBpZiAobi0+cikgcHVzaChuLT5yKTsKICAgICAgICBuLT5zeiA9IHN6KG4tPmwpICsgMSArIHN6KG4tPnIpOwogICAgICAgIG4tPmFnZyA9IGFnZyhuLT5sKSArIG4tPnZhbCArIGFnZyhuLT5yKTsKICAgICAgICByZXR1cm4gbjsKICAgIH0KICAgIAogICAgcHRyIG1lcmdlKHB0ciBsLCBwdHIgcikgewogICAgICAgIGlmICghbCB8fCAhcikgcmV0dXJuIGwgPyBsIDogcjsKICAgICAgICBwdXNoKGwpLCBwdXNoKHIpOwogICAgICAgIGlmIChsLT5wcmkgPiByLT5wcmkpIHJldHVybiBsLT5yID0gbWVyZ2UobC0+ciwgciksIHB1bGwobCk7CiAgICAgICAgZWxzZSByZXR1cm4gci0+bCA9IG1lcmdlKGwsIHItPmwpLCBwdWxsKHIpOwogICAgfQogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZS4uLiBBcmdzPgogICAgcHRyIG1lcmdlKHB0ciBsLCBBcmdzLi4uIGFyZ3MpIHsKICAgICAgICByZXR1cm4gbWVyZ2UobCwgbWVyZ2UoYXJncy4uLikpOwogICAgfQogICAgCiAgICAvLyBbLWluZiwgaSkgYW5kIFtpLCBpbmZdCiAgICBwYWlyPHB0ciwgcHRyPiBzcGxpdGkocHRyIG4sIGludCBpKSB7CiAgICAgICAgaWYgKCFuKSByZXR1cm4ge251bGxwdHIsIG51bGxwdHJ9OwogICAgICAgIHB1c2gobik7CiAgICAgICAgaWYgKGkgPD0gc3oobi0+bCkpIHsKICAgICAgICAgICAgYXV0byBbbCwgcl0gPSBzcGxpdGkobi0+bCwgaSk7CiAgICAgICAgICAgIG4tPmwgPSByOwogICAgICAgICAgICByZXR1cm4ge2wsIHB1bGwobil9OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGF1dG8gW2wsIHJdID0gc3BsaXRpKG4tPnIsIGkgLSAxIC0gc3oobi0+bCkpOwogICAgICAgICAgICBuLT5yID0gbDsKICAgICAgICAgICAgcmV0dXJuIHtwdWxsKG4pLCByfTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIC8vIGN1dHMgb3V0IFtsbywgaGkpCiAgICB0dXBsZTxwdHIsIHB0ciwgcHRyPiBzcGxpdGkocHRyIG4sIGludCBsbywgaW50IGhpKSB7CiAgICAgICAgYXV0byBbbG0sIHJdID0gc3BsaXRpKG4sIGhpKTsKICAgICAgICBhdXRvIFtsLCBtXSA9IHNwbGl0aShsbSwgbG8pOwogICAgICAgIHJldHVybiB7bCwgbSwgcn07CiAgICB9CiAgICAKICAgIHZvaWQgdXBkaShwdHIgbiwgaW50IGxvLCBpbnQgaGksIExhenkgbGF6eSkgewogICAgICAgIGlmICghbikgcmV0dXJuOwogICAgICAgIHB1c2gobik7CiAgICAgICAgaWYgKGxvID49IG4tPnN6IHx8IGhpIDw9IDAgfHwgbi0+YWdnLmNhbl9icmVhayhsYXp5KSkgcmV0dXJuOwogICAgICAgIGlmIChsbyA8PSAwICYmIG4tPnN6IDw9IGhpICYmIG4tPmFnZy5jYW5fdGFnKGxhenkpKSB7CiAgICAgICAgICAgIG4tPmxhenkgKz0gbGF6eTsKICAgICAgICAgICAgcHVzaChuKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZiAobG8gPD0gc3oobi0+bCkgJiYgc3oobi0+bCkgPCBoaSkgbi0+dmFsLnVwZChsYXp5LCAxKTsKICAgICAgICB1cGRpKG4tPmwsIGxvLCBoaSwgbGF6eSk7CiAgICAgICAgdXBkaShuLT5yLCBsbyAtIDEgLSBzeihuLT5sKSwgaGkgLSAxIC0gc3oobi0+bCksIGxhenkpOwogICAgICAgIHB1bGwobik7CiAgICB9CiAgICAKICAgIHZvaWQgaGVhcGlmeShwdHIgbikgewogICAgICAgIGlmICghbikgcmV0dXJuOwogICAgICAgIHB0ciBteCA9IG47CiAgICAgICAgaWYgKG4tPmwgJiYgbi0+bC0+cHJpID4gbXgtPnByaSkgbXggPSBuLT5sOwogICAgICAgIGlmIChuLT5yICYmIG4tPnItPnByaSA+IG14LT5wcmkpIG14ID0gbi0+cjsKICAgICAgICBpZiAobXggIT0gbikgc3dhcChuLT5wcmksIG14LT5wcmkpLCBoZWFwaWZ5KG14KTsKICAgIH0KICAgIAogICAgcHRyIGJ1aWxkKHZlY3RvcjxwdHI+JiBucywgaW50IGwgPSAwLCBpbnQgciA9IC02OSkgewogICAgICAgIGlmIChyID09IC02OSkgciA9IChpbnQpIG5zLnNpemUoKSAtIDE7CiAgICAgICAgaWYgKGwgPiByKSByZXR1cm4gbnVsbHB0cjsKICAgICAgICBpZiAobCA9PSByKSByZXR1cm4gbnNbbF07CiAgICAgICAgaW50IG0gPSAobCArIHIpIC8gMjsKICAgICAgICBuc1ttXS0+bCA9IGJ1aWxkKG5zLCBsLCBt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GwgPj4gcjsKICAgICAgICAgICAgYXV0byBbbGhzLCBtaWQsIHJoc10gPSBzcGxpdGkodHJlYXAsIGwgLSAxLCByKTsKICAgICAgICAgICAgdHJlYXAgPSBtZXJnZShsaHMsIHJocywgbWlkKTsKICAgICAgICB9ICAgCiAgICAgICAgaWYgKHQgPT0gMykgewogICAgICAgICAgICBpbnQgbCwgciwgeDsgY2luID4+IGwgPj4gciA+PiB4OwogICAgICAgICAgICB1cGRpKHRyZWFwLCBsIC0gMSwgciwge3gsIElORn0pOwogICAgICAgIH0KCiAgICAgICAgcHVzaCh0cmVhcCk7CiAgICAgICAgY291dCA8PCBhZ2codHJlYXApLnN1bSA8PCAnXG4nOwogICAgfQogICAgZGVsZXRlIHRyZWFwOwp9