// 2log
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int ll
#define rep(i, f, t) for (int i = (f), ed##i = (t); i <= ed##i; ++i)
#define re(i, t) rep (i, 1, t)
#define per(i, t, f) for (int i = (t), ed##i = (f); i >= ed##i; --i)
#define ste(i, f, t, s) for (int i = (f), ed##i = (t); i <= ed##i; i += s)
#define nxt(i, f, g) for (int i = g.h[f]; i; i = g.e[i].n)
#define umod(x) ((x) >= mo && ((x) -= mo))
#define dmod(x) ((x) < 0 && ((x) += mo))
#define y1 y1__
#define fio(x) (freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout))
template <class T, class E>
__attribute__((always_inline)) inline void up(T &x, E &&y) {
if (x < y) x = y;
}
template <class T, class E>
__attribute__((always_inline)) inline void down(T &x, E &&y) {
if (y < x) x = y;
}
const int N = 1e5 + 9;
const int RA = 4;
int n, m;
struct Tr {
struct T {
int ls, rs, sz;
ll cnt, sum;
} d[N * 140];
int tot;
inline void Up(int u) {
d[u].sz = d[d[u].ls].sz + d[d[u].rs].sz;
d[u].cnt = d[d[u].ls].cnt + d[d[u].rs].cnt;
d[u].sum = d[d[u].ls].sum + d[d[u].rs].sum;
}
inline void Splk(ll &k, int &u, vector<int> &vl, vector<int> &vr) {
assert(u);
Adj(u);
if (d[u].cnt <= k) return vl.push_back(u), k -= d[u].cnt, void();
if (!k) return vr.push_back(u), void();
if (!d[u].ls && !d[u].rs) {
// cerr << "Splk corner case\n";
ll val = d[u].sum / d[u].cnt;
// dbg(val);
int v1 = ++tot;
d[v1].sz = 1, d[v1].cnt = k, d[v1].sum = val * d[v1].cnt;
int v2 = ++tot;
d[v2].sz = 1, d[v2].cnt = d[u].cnt - k, d[v2].sum = val * d[v2].cnt;
// dbg(d[v1].cnt, d[v2].cnt);
// dbg(d[v1].sum, d[v2].sum);
k = 0;
vl.push_back(v1), vr.push_back(v2);
return;
}
Splk(k, d[u].ls, vl, vr);
Splk(k, d[u].rs, vl, vr);
}
inline int Merge(int &u, int &v) {
int sz = d[u].sz + d[v].sz;
if (sz <= 5) {
int rt = ++tot;
d[rt].ls = u, d[rt].rs = v;
Up(rt);
return rt;
}
if (d[u].sz * RA < sz) {
int ls = Merge(u, d[v].ls);
int rt = ++tot;
d[rt].ls = ls;
d[rt].rs = d[v].rs;
Up(rt);
Adj(rt);
return rt;
}
if (d[v].sz * RA < sz) {
int rs = Merge(d[u].rs, v);
int rt = ++tot;
d[rt].ls = d[u].ls;
d[rt].rs = rs;
Up(rt);
Adj(rt);
return rt;
}
int rt = ++tot;
d[rt].ls = u, d[rt].rs = v;
Up(rt);
Adj(rt);
return rt;
}
inline int Mer(const vector<int> &vec, int l, int r) {
if (l == r) return vec[l];
int mid = (l + r) >> 1;
int ls = Mer(vec, l, mid);
int rs = Mer(vec, mid + 1, r);
int u = Merge(ls, rs);
Adj(u);
return u;
}
inline void Adj(int &u) {
if (d[u].sz <= 5) return;
if (d[d[u].ls].sz * RA < d[d[u].rs].sz) {
d[++tot] = d[u], u = tot;
int rt = ++tot;
d[rt] = d[d[u].rs];
d[u].rs = d[rt].rs;
d[rt].rs = d[rt].ls;
d[rt].ls = d[u].ls;
d[u].ls = rt;
Up(rt);
Up(u);
} else if (d[d[u].rs].sz * RA < d[d[u].ls].sz) {
d[++tot] = d[u], u = tot;
int rt = ++tot;
d[rt] = d[d[u].ls];
d[u].ls = d[rt].ls;
d[rt].ls = d[rt].rs;
d[rt].rs = d[u].rs;
d[u].rs = rt;
Up(rt);
Up(u);
}
}
} tr;
struct Dat {
ll del;
int rt;
inline void Pop(ll x) {
if (!rt) return del += x, void();
if (tr.d[rt].cnt <= x)
x -= tr.d[rt].cnt, rt = 0;
else {
vector<int> vl, vr;
x = tr.d[rt].cnt - x;
tr.Splk(x, rt, vl, vr);
rt = tr.Mer(vl, 0, vl.size() - 1);
}
del += x;
}
inline ll Ask(ll l, ll r) {
if (!rt) return 0;
if (l > tr.d[rt].cnt) return 0;
int rtbak = rt;
down(r, tr.d[rt].cnt);
ll len = r - l + 1;
// dbg(len);
--l;
vector<int> v1, v2, v3, v4;
tr.Splk(l, rt, v1, v4);
for (auto u : v4) tr.Splk(len, u, v2, v3);
ll ans = 0;
for (auto u : v2) {
ans += tr.d[u].sum;
// dbg(tr.d[u].cnt);
}
rt = rtbak;
return ans;
}
};
inline Dat Mer(Dat x, Dat y) {
x.Pop(y.del);
if (!x.rt || !y.rt)
x.rt |= y.rt;
else {
x.rt = tr.Merge(x.rt, y.rt);
}
return x;
}
struct Seg {
Dat d[N << 2];
inline void Pop(ll x, int u) { d[u].Pop(x); }
inline void Push(int x, int y, int u) {
int v = ++tr.tot;
tr.d[v].sz = 1, tr.d[v].cnt = x, tr.d[v].sum = 1ll * x * y;
if (!d[u].rt) {
d[u].rt = v;
} else {
d[u].rt = tr.Merge(d[u].rt, v);
}
}
inline void Down(int u) {
d[u << 1] = Mer(d[u << 1], d[u]);
d[u << 1 | 1] = Mer(d[u << 1 | 1], d[u]);
d[u].del = 0, d[u].rt = 0;
}
inline void Pop(int L, int R, ll x, int u, int l, int r) {
if (L <= l && r <= R) return Pop(x, u), void();
int mid = (l + r) >> 1;
Down(u);
if (L <= mid) Pop(L, R, x, u << 1, l, mid);
if (mid + 1 <= R) Pop(L, R, x, u << 1 | 1, mid + 1, r);
}
inline void Push(int L, int R, int x, int y, int u, int l, int r) {
if (L <= l && r <= R) return Push(x, y, u), void();
int mid = (l + r) >> 1;
Down(u);
if (L <= mid) Push(L, R, x, y, u << 1, l, mid);
if (mid + 1 <= R) Push(L, R, x, y, u << 1 | 1, mid + 1, r);
}
inline ll Ask(int p, ll L, ll R, int u, int l, int r) {
if (l == r) return d[u].Ask(L, R);
int mid = (l + r) >> 1;
Down(u);
if (p <= mid) return Ask(p, L, R, u << 1, l, mid);
return Ask(p, L, R, u << 1 | 1, mid + 1, r);
}
} seg;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
while (m--) {
int op;
cin >> op;
if (op == 1) {
int l, r, x, y;
cin >> l >> r >> x >> y;
seg.Push(l, r, x, y, 1, 1, n);
} else if (op == 2) {
int l, r;
ll x;
cin >> l >> r >> x;
seg.Pop(l, r, x, 1, 1, n);
} else if (op == 3) {
int p;
ll l, r;
cin >> p >> l >> r;
cout << seg.Ask(p, l, r, 1, 1, n) << '\n';
}
}
// return 0;
}
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using u64=unsigned long long;
using LF=long double;
#define F(i,l,r) for(int i=l;i<=r;++i)
#define G(i,r,l) for(int i=r;i>=l;--i)
#define CT const&
#define IL inline
#define PB push_back
template<class T>
IL void tomn(T &x,T CT y){y<x?x=y,0:0;}
template<class T>
IL void tomx(T &x,T CT y){y>x?x=y,0:0;}
#define DEBUG(x) cerr<<"line:"<<__LINE__<<" "#x"="<<x<<endl
#define CUT cerr<<"**********\n"
const LF EPS=1e-10L;
IL int dcmp(LF x){return fabs(x)<=EPS?0:(x<0?-1:1);}
const LL INF=4e18L;
#define L (p<<1)
#define R (L|1)
const int N=100500;
int n,m;
struct Op{int tp;LL a,b,c,d;}op[N];
namespace S1{
struct D{LL x,y;};
LL qry(int tim,int i,LL p,LL q){
vector<D> st;
F(j,1,tim-1){
Op x=op[j];
if(x.tp<=2&&x.a<=i&&i<=x.b){
if(x.tp==1)st.PB(D{x.c,x.d});
else{
while(!st.empty()&&st.back().x<=x.c)
x.c-=st.back().x,st.pop_back();
if(!st.empty())st.back().x-=x.c;
}
}
}
auto sum=[&](LL r){
LL ret=0;int j=0;
for(;r&&j<(int)st.size();++j){
LL x=min(st[j].x,r);
r-=x,ret+=st[j].y*x;
}return ret;
};return sum(q)-sum(p-1);
}
void main(){
// cerr<<"S1"<<endl;
F(i,1,m)if(op[i].tp==3)
printf("%lld\n",qry(i,op[i].a,op[i].b,op[i].c));
}
}
namespace S3{
struct D{LL a,b;};
IL D operator*(D CT x,D CT y){return D{x.a+y.a,max(x.b+y.a,y.b)};}
IL void operator*=(D&x,D CT y){x=x*y;}
class Seg{
D d[4*N];
LL val[4*N];
void upd(int p,D x){d[p]*=x,val[p]=max(val[p]+x.a,x.b);}
void push(int p){upd(L,d[p]),upd(R,d[p]),d[p]=D{0,-INF};}
int l,r;D x;
void seg(int p,int s,int t){
if(l<=s&&t<=r)return upd(p,x);
int mid=(s+t)>>1;push(p);
l<=mid?seg(L,s,mid):void();
mid<r?seg(R,mid+1,t):void();
}
void build(int p,int s,int t){
val[p]=0,d[p]=D{0,-INF};
if(s==t)return;
int mid=(s+t)>>1;
build(L,s,mid),build(R,mid+1,t);
}
public:
int n;
void build(int n_){n=n_,build(1,1,n);}
void upd(int l_,int r_,D x_){l=l_,r=r_,x=x_,seg(1,1,n);}
LL qry(int i){
int p=1,s=1,t=n;
while(s<t){
int mid=(s+t)>>1;push(p);
i<=mid?(p=L,t=mid):(p=R,s=mid+1);
}return val[p];
}
}sol;
bool main(){
F(i,1,m)if(op[i].tp==1&&op[i].d!=1)
return 0;
// cerr<<"S3"<<endl;
sol.build(n);
F(i,1,m){
Op x=op[i];
if(x.tp==1)sol.upd(x.a,x.b,D{x.c,0});
else if(x.tp==2)sol.upd(x.a,x.b,D{-x.c,0});
else{
LL si=sol.qry(x.a);
printf("%lld\n",min(si,x.c)-min(si,x.b-1));
}
}return 1;
}
}
namespace S2{
class Seg{
LL d[4*N][2];
int n;
// void up(int p){d[p][0]=d[L][0]+d[R][0],d[p][1]=d[L][1]+d[R][1];}
public:
void build(int n_){n=1<<__lg(2*n_+3);}
void upd(int i,LL x,LL y){
y*=x;
for(i+=n;i;i>>=1)d[i][0]+=x,d[i][1]+=y;
}
LL qry(int i,LL r){
int p=1,s=0,t=n-1;LL ans=0;
while(s<t){
int mid=(s+t)>>1;
LL x=(mid<=i&&d[L][0]<=r);
r-=d[L][0]*x,ans+=d[L][1]*x,p=(p<<1)|x;
x?(s=mid+1):(t=mid);
}if(t<=i)ans+=d[p][1]/d[p][0]*r;
return ans;
}
}sol;
struct Upd{int i;LL x,y;};
struct Qry{int i;LL p,q;};
vector<Upd> ud[N];
vector<Qry> qr[N];
LL ans[N];
bool main(){
F(i,1,m)if(op[i].tp==2)
return 0;
// cerr<<"S2"<<endl;
F(i,1,m){
Op x=op[i];
if(x.tp==1)
ud[x.a].PB(Upd{i,x.c,x.d}),
ud[x.b+1].PB(Upd{i,-x.c,x.d});
else qr[x.a].PB(Qry{i,x.b,x.c});
}sol.build(m);
F(i,1,n){
for(Upd x:ud[i])sol.upd(x.i,x.x,x.y);
for(Qry x:qr[i])ans[x.i]=sol.qry(x.i,x.q)-sol.qry(x.i,x.p-1);
}
F(i,1,m)if(op[i].tp==3)printf("%lld\n",ans[i]);
return 1;
}
}
int main(){
#ifdef LOCAL
freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
#endif
scanf("%d%d",&n,&m);
F(i,1,m){
int tp;LL a,b,c,d;scanf("%d%lld%lld%lld",&tp,&a,&b,&c);
if(tp==1)scanf("%lld",&d);
op[i]=Op{tp,a,b,c,d};
}
if(!S3::main()&&!S2::main())
S1::main();
}