#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999999999999
#define MAXN 300005
struct node
{
ll prop;
ll x;
ll sum;
};
ll N, Q, ans;
ll arr[MAXN];
node tree[4 * MAXN];
void push(ll n, ll l, ll r)
{
ll mid = (l + r) / 2;
ll p = tree[n].x;
tree[n].x = 0;
tree[n].prop = 0;
tree[2 * n].sum = (mid - l + 1) * p;
tree[2 * n + 1].sum = (r - mid) * p;
tree[2 * n].prop = 0;
tree[2 * n].x = p;
tree[2 * n + 1].prop = 0;
tree[2 * n + 1].x = p;
}
void build(ll n, ll l, ll r)
{
if(l == r)
{
tree[n].prop = 0;
tree[n].x = 0;
tree[n].sum = arr[l];
return ;
}
ll mid = (l + r) / 2;
build(2 * n, l, mid);
build(2 * n + 1, mid + 1, r);
tree[n].x = 0;
tree[n].prop = 0;
tree[n].sum = tree[2 * n].sum + tree[2 * n + 1].sum;
}
void update_by(ll n, ll l, ll r,ll i, ll j, ll val)
{
if(l > j or r < i)
return ;
if(l != r and tree[n].x)
push(n, l, r);
if(l >= i and r <= j)
{
tree[n].sum += (val * (r - l + 1));
tree[n].prop += val;
tree[n].x = 0;
return ;
}
ll mid = (l + r) / 2;
update_by(2 * n, l, mid, i, j, val);
update_by(2 * n + 1, mid + 1, r, i, j, val);
tree[n].sum = tree[2 * n].sum + tree[2 * n + 1].sum + ((r - l + 1) * tree[n].prop);
}
void update_to(ll n, ll l, ll r, ll i, ll j, ll val)
{
if(l > j or r < i)
return ;
if(l != r and tree[n].x)
push(n, l, r);
if(l >= i and r <= j)
{
tree[n].prop = 0;
tree[n].x = val;
tree[n].sum = val * (r - l + 1);
return ;
}
ll mid = (l + r) / 2;
update_to(2 * n, l, mid, i, j, val);
update_to(2 * n + 1, mid + 1, r, i, j, val);
tree[n].sum = tree[2 * n].sum + tree[2 * n + 1].sum + ((r - l + 1) * tree[n].prop);
}
ll query(ll n, ll l, ll r, ll i, ll j, ll val = 0)
{
if(l > j or r < i)
{
return 0;
}
if(l != r and tree[n].x)
push(n, l, r);
if(l >= i and r <= j)
{
return tree[n].sum + val * (r - l + 1);
}
ll mid = (l + r) / 2;
ll p = tree[n].prop;
ll a = query(2 * n, l, mid, i, j, val + p);
ll b = query(2 * n + 1, mid + 1, r, i, j, val + p);
return a + b;
}
void solve()
{
cin >> N >> Q;
for(ll i = 1; i <= N; ++ i)
{
cin >> arr[i];
}
N = 1 << (ll) ceil(log2(N));
build(1, 1, N);
for(ll i = 1; i <= Q; ++ i)
{
ll tt;
cin >> tt;
if(tt == 1)
{
ll a, b, x;
cin >> a >> b >> x;
update_by(1, 1, N, a, b, x);
}
else if(tt == 2)
{
ll a, b, x;
cin >> a >> b >> x;
update_to(1, 1, N, a, b, x);
}
else
{
ll a, b;
cin >> a >> b;
cout << query(1, 1, N, a, b) << endl;
}
}
return ;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
solve();
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBJTkYgOTk5OTk5OTk5OTk5OTk5OTk5CiNkZWZpbmUgTUFYTiAzMDAwMDUKCnN0cnVjdCBub2RlCnsKICAgIGxsIHByb3A7CiAgICBsbCB4OwogICAgbGwgc3VtOwp9OwoKbGwgTiwgUSwgYW5zOwpsbCBhcnJbTUFYTl07Cm5vZGUgdHJlZVs0ICogTUFYTl07Cgp2b2lkIHB1c2gobGwgbiwgbGwgbCwgbGwgcikKewogICAgbGwgbWlkID0gKGwgKyByKSAvIDI7CiAgICBsbCBwID0gdHJlZVtuXS54OwoKICAgIHRyZWVbbl0ueCA9IDA7CiAgICB0cmVlW25dLnByb3AgPSAwOwoKICAgIHRyZWVbMiAqIG5dLnN1bSA9IChtaWQgLSBsICsgMSkgKiBwOwogICAgdHJlZVsyICogbiArIDFdLnN1bSA9IChyIC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ieSgyICogbiArIDEsIG1pZCArIDEsIHIsIGksIGosIHZhbCk7CiAgICB0cmVlW25dLnN1bSA9IHRyZWVbMiAqIG5dLnN1bSArIHRyZWVbMiAqIG4gKyAxXS5zdW0gKyAoKHIgLSBsICsgMSkgKiB0cmVlW25dLnByb3ApOwp9Cgp2b2lkIHVwZGF0ZV90byhsbCBuLCBsbCBsLCBsbCByLCBsbCBpLCBsbCBqLCBsbCB2YWwpCnsKICAgIGlmKGwgPiBqIG9yIHIgPCBpKQogICAgICAgIHJldHVybiA7CgogICAgaWYobCAhPSByIGFuZCB0cmVlW25dLngpCiAgICAgICAgcHVzaChuLCBsLCByKTsKCiAgICBpZihsID49IGkgYW5kIHIgPD0gaikKICAgIHsKICAgICAgICB0cmVlW25dLnByb3AgPSAwOwogICAgICAgIHRyZWVbbl0ueCA9IHZhbDsKICAgICAgICB0cmVlW25dLnN1bSA9IHZhbCAqIChyIC0gbCArIDEpOwogICAgICAgIHJldHVybiA7CiAgICB9CgogICAgbGwgbWlkID0gKGwgKyByKSAvIDI7CiAgICB1cGRhdGVfdG8oMiAqIG4sIGwsIG1pZCwgaSwgaiwgdmFsKTsKICAgIHVwZGF0ZV90bygyICogbiArIDEsIG1pZCArIDEsIHIsIGksIGosIHZhbCk7CiAgICB0cmVlW25dLnN1bSA9IHRyZWVbMiAqIG5dLnN1bSArIHRyZWVbMiAqIG4gKyAxXS5zdW0gKyAoKHIgLSBsICsgMSkgKiB0cmVlW25dLnByb3ApOwp9CgpsbCBxdWVyeShsbCBuLCBsbCBsLCBsbCByLCBsbCBpLCBsbCBqLCBsbCB2YWwgPSAwKQp7CiAgICBpZihsID4gaiBvciByIDwgaSkKICAgIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KCiAgICBpZihsICE9IHIgYW5kIHRyZWVbbl0ueCkKICAgICAgICBwdXNoKG4sIGwsIHIpOwoKICAgIGlmKGwgPj0gaSBhbmQgciA8PSBqKQogICAgewogICAgICAgIHJldHVybiB0cmVlW25dLnN1bSArIHZhbCAqIChyIC0gbCArIDEpOwogICAgfQogICAgCgogICAgbGwgbWlkID0gKGwgKyByKSAvIDI7CiAgICBsbCBwID0gdHJlZVtuXS5wcm9wOwogICAgbGwgYSA9IHF1ZXJ5KDIgKiBuLCBsLCBtaWQsIGksIGosIHZhbCArIHApOwogICAgbGwgYiA9IHF1ZXJ5KDIgKiBuICsgMSwgbWlkICsgMSwgciwgaSwgaiwgdmFsICsgcCk7CiAgICByZXR1cm4gYSArIGI7Cn0KCnZvaWQgc29sdmUoKQp7CiAgICBjaW4gPj4gTiA+PiBROwogICAgZm9yKGxsIGkgPSAxOyBpIDw9IE47ICsrIGkpCiAgICB7CiAgICAgICAgY2luID4+IGFycltpXTsKICAgIH0KCiAgICBOID0gMSA8PCAobGwpIGNlaWwobG9nMihOKSk7CiAgICBidWlsZCgxLCAxLCBOKTsKCiAgICBmb3IobGwgaSA9IDE7IGkgPD0gUTsgKysgaSkKICAgIHsKICAgICAgICBsbCB0dDsKICAgICAgICBjaW4gPj4gdHQ7CgogICAgICAgIGlmKHR0ID09IDEpCiAgICAgICAgewogICAgICAgICAgICBsbCBhLCBiLCB4OwogICAgICAgICAgICBjaW4gPj4gYSA+PiBiID4+IHg7CiAgICAgICAgICAgIHVwZGF0ZV9ieSgxLCAxLCBOLCBhLCBiLCB4KTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZih0dCA9PSAyKQogICAgICAgIHsKICAgICAgICAgICAgbGwgYSwgYiwgeDsKICAgICAgICAgICAgY2luID4+IGEgPj4gYiA+PiB4OwogICAgICAgICAgICB1cGRhdGVfdG8oMSwgMSwgTiwgYSwgYiwgeCk7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGxsIGEsIGI7CiAgICAgICAgICAgIGNpbiA+PiBhID4+IGI7CiAgICAgICAgICAgIGNvdXQgPDwgcXVlcnkoMSwgMSwgTiwgYSwgYikgPDwgZW5kbDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gOwp9CgppbnQgbWFpbigpCnsKICAgIHN0ZDo6aW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgc3RkOjpjaW4udGllKG51bGxwdHIpOwoKICAgIC8vIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKTsKICAgIC8vIGZyZW9wZW4oIm91dHB1dC50eHQiLCJ3IixzdGRvdXQpOwoKICAgIHNvbHZlKCk7CiAgICByZXR1cm4gMDsKfQo=