# 【BZOJ1062】糖果雨（NOI2008）-数形结合+二维树状数组

#include <bits/stdc++.h>
using namespace std;
int n,len,len2,len4;
int sum[2][4010][4010],x[1000010],y[1000010];

int lowbit(int x)
{
return x&(-x);
}

int add(int type,int x,int y,int d)
{
x++,y++;
for(int i=x;i<=len2;i+=lowbit(i))
for(int j=y;j<=len4;j+=lowbit(j))
sum[type][i][j]+=d;
}

int Sum(int type,int x,int y)
{
x++,y++;
if (x<=0||y<=0) return 0;
if (x>len2) x=len2;
if (y>len4) y=len4;
int ans=0;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
ans+=sum[type][i][j];
return ans;
}

int calc(int type,int x1,int y1,int x2,int y2)
{
return Sum(type,x2,y2)-Sum(type,x2,y1-1)-Sum(type,x1-1,y2)+Sum(type,x1-1,y1-1);
}

int main()
{
scanf("%d%d",&n,&len);
len2=(len<<1),len4=(len<<2);
for(int i=1;i<=n;i++)
{
int op,t,c,l,r,d,ans;
scanf("%d",&op);
if (op==1)
{
scanf("%d%d%d%d%d",&t,&c,&l,&r,&d);
x[c]=(t-d*l+len2)%len2;
y[c]=r-l;
}
if (op==2)
{
scanf("%d%d%d",&t,&l,&r);
t%=len2;
ans=0;
int flag=(r==len);
ans+=calc(0,t,l+t,t+r,len4);
ans+=calc(0,0,l+t-len2,t+r-len2-flag,len4);
ans+=calc(1,t-r+len2+flag,l-t,len2,len4);
ans+=calc(1,t-r,l-t+len2,t-1,len4);
printf("%d\n",ans);
}
if (op==3)
{
scanf("%d%d",&t,&c);
}