#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 5;
const long long INF = (1LL << 60);
int n, q;
long long a[N];
struct Node {
long long sum;
long long mx;
long long smx;
int cmx;
long long mn;
long long smn;
int cmn;
Node() {}
Node(long long x) {
sum = x;
mx = x;
smx = -INF;
cmx = 1;
mn = x;
smn = INF;
cmn = 1;
}
};
Node t[4 * N];
Node comb(const Node &L, const Node &R) {
Node cur;
cur.sum = L.sum + R.sum;
if (L.mx == R.mx) {
cur.mx = L.mx;
cur.cmx = L.cmx + R.cmx;
cur.smx = max(L.smx, R.smx);
} else if (L.mx > R.mx) {
cur.mx = L.mx;
cur.cmx = L.cmx;
cur.smx = max(L.smx, R.mx);
} else {
cur.mx = R.mx;
cur.cmx = R.cmx;
cur.smx = max(L.mx, R.smx);
}
if (L.mn == R.mn) {
cur.mn = L.mn;
cur.cmn = L.cmn + R.cmn;
cur.smn = min(L.smn, R.smn);
} else if (L.mn < R.mn) {
cur.mn = L.mn;
cur.cmn = L.cmn;
cur.smn = min(L.smn, R.mn);
} else {
cur.mn = R.mn;
cur.cmn = R.cmn;
cur.smn = min(L.mn, R.smn);
}
return cur;
}
void pull(int x) {
t[x] = comb(t[x * 2], t[x * 2 + 1]);
}
void apply_chmin(int x, long long y) {
if (t[x].mx <= y) {
return;
}
long long old_mx = t[x].mx;
t[x].sum -= 1ll * (old_mx - y) * t[x].cmx;
if (t[x].mn == old_mx) {
t[x].mn = y;
} else if (t[x].smn == old_mx) {
t[x].smn = y;
}
t[x].mx = y;
}
void apply_chmax(int x, long long y) {
if (t[x].mn >= y) {
return;
}
long long old_mn = t[x].mn;
t[x].sum += 1ll * (y - old_mn) * t[x].cmn;
if (t[x].mx == old_mn) {
t[x].mx = y;
} else if (t[x].smx == old_mn) {
t[x].smx = y;
}
t[x].mn = y;
}
void push(int x) {
apply_chmin(x * 2, t[x].mx);
apply_chmin(x * 2 + 1, t[x].mx);
apply_chmax(x * 2, t[x].mn);
apply_chmax(x * 2 + 1, t[x].mn);
}
void build(int x, int l, int r) {
if (l == r) {
t[x] = Node(a[l]);
return;
}
int m = (l + r) / 2;
build(x * 2, l, m);
build(x * 2 + 1, m + 1, r);
pull(x);
}
void upd_min(int x, int l, int r, int tl, int tr, long long y) {
if (tl > tr || t[x].mx <= y) {
return;
}
if (l == tl && r == tr && t[x].smx < y) {
apply_chmin(x, y);
return;
}
push(x);
int m = (l + r) / 2;
upd_min(x * 2, l, m, tl, min(m, tr), y);
upd_min(x * 2 + 1, m + 1, r, max(m + 1, tl), tr, y);
pull(x);
}
void upd_max(int x, int l, int r, int tl, int tr, long long y) {
if (tl > tr || t[x].mn >= y) {
return;
}
if (l == tl && r == tr && t[x].smn > y) {
apply_chmax(x, y);
return;
}
push(x);
int m = (l + r) / 2;
upd_max(x * 2, l, m, tl, min(m, tr), y);
upd_max(x * 2 + 1, m + 1, r, max(m + 1, tl), tr, y);
pull(x);
}
long long get_sum(int x, int l, int r, int tl, int tr) {
if (tl > tr) {
return 0;
}
if (l == tl && r == tr) {
return t[x].sum;
}
push(x);
int m = (l + r) / 2;
return get_sum(x * 2, l, m, tl, min(m, tr)) +
get_sum(x * 2 + 1, m + 1, r, max(m + 1, tl), tr);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int l, r;
long long x;
cin >> l >> r >> x;
upd_min(1, 1, n, l, r, x);
} else if (type == 2) {
int l, r;
long long x;
cin >> l >> r >> x;
upd_max(1, 1, n, l, r, x);
} else {
int l, r;
cin >> l >> r;
cout << get_sum(1, 1, n, l, r) << '\n';
}
}
return 0;
}