flee_hubu @ 2024-07-30 11:19:18
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
typedef long long LL;
int n, m;
struct Node
{
int l, r;
LL add, change ,maxv;
}tr[N * 4];
LL a[N];
bool st = false;
void pushup(int u)
{
tr[u].maxv = max(tr[u << 1].maxv, tr[u << 1 | 1].maxv);
}
void pushdown(int u)
{
Node& root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if (root.add != 0)
{
left.add += root.add, left.maxv += root.add;
right.add += root.add, right.maxv += root.add;
root.add = 0;
}
if (st == true)
{
left.change = root.change, left.maxv = root.change;
right.change = root.change, right.maxv = root.change;
st = false;
}
}
void modify(int u, int l, int r, int x)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].maxv += x;
tr[u].add += x;
}
else
{
st = false;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, x);
if (r > mid) modify(u << 1 | 1, l, r, x);
pushup(u);
}
}
void cmodify(int u, int l, int r, int x)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].maxv = x;
tr[u].change = x;
tr[u].add = 0;
}
else
{
st = true;
tr[u].add = 0;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) cmodify(u << 1, l, r, x);
if (r > mid) cmodify(u << 1 | 1, l, r, x);
pushup(u);
}
}
LL query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r)
return tr[u].maxv;
pushdown(u);
LL maxn = -1e17;
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) maxn = max(maxn , query(u << 1, l, r));
if (r > mid) maxn = max(maxn, query(u << 1 | 1, l, r));
return maxn;
}
void build(int u, int l, int r)
{
if (l == r) tr[u] = { l , r , 0 , 0 , a[l] };
else
{
tr[u] = { l , r , 0 , 0 , 0 };
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
build(1, 1, n);
while (m--)
{
int op, l, r, x;
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d%d", &l, &r, &x);
cmodify(1, l, r, x);
}
else if (op == 2) {
scanf("%d%d%d", &l, &r, &x);
modify(1, l, r, x);
}
else
{
scanf("%d%d", &l, &r);
printf("%lld\n" , query(1, l, r));
}
}
return 0;
}