#include<bits/stdc++.h>
#define forUp(i,a,b) for(int i=(a);i<=(b);++i)
#define forUP(i,a,b) for(int i=(a);i<(b);++i)
#define forDown(i,a,b) for(int i=(a);i>=(b);--i)
#define forG(u,v) for(int __i=head[u],v=to[__i];__i;__i=nxt[__i],v=to[__i])
#define forWG(u,v,c) for(int __i=head[u],v=to[__i],c=w[__i];__i;__i=nxt[__i],v=to[__i],c=w[__i])
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define popc __builtin_popcount
#define popc64 __builtin_popcountll
#define seteps(n) fixed<<setprecision(n)
bool __mst;using uint=unsigned int;using int64=long long;using uint64=unsigned long long;using int128=__int128;using uint128=unsigned __int128;using float64=double;using float80=long double;
constexpr int INF=0x3f3f3f3f,MINF=0xcfcfcfcf;constexpr int64 INF64=0x3f3f3f3f3f3f3f3f,MINF64=0xcfcfcfcfcfcfcfcf;constexpr float64 INFDB=1e50,eps=1e-6;
template<class _Tp>inline void chkMax(_Tp &x,const _Tp &y){if(x<y)x=y;}template<class _Tp>inline void chkMin(_Tp &x,const _Tp &y){if(x>y)x=y;}
inline int addMod(const int &x,const int &y,const int mod){int ans=x+y;return ans>=mod?ans-mod:ans;}inline int subMod(const int &x,const int &y,const int mod){int ans=x-y;return ans<0?ans+mod:ans;}
constexpr int N=1e6+10,M=1e6+10,V=1e6+11,B=1100;int __test_num=1,__task_id,__tst;using namespace std;void __init();
namespace LXcjh4998{
namespace internal{
namespace type_traits{
template<class _Tp>using __is_signed_int128=typename std::conditional<std::is_same<_Tp,__int128_t>::value||std::is_same<_Tp,__int128>::value,std::true_type,std::false_type>::type;
template<class _Tp>using __is_unsigned_int128=typename std::conditional<std::is_same<_Tp,__uint128_t>::value||std::is_same<_Tp,unsigned __int128>::value,std::true_type,std::false_type>::type;
template<class _Tp>using __make_unsigned_int128=typename std::conditional<std::is_same<_Tp,__int128_t>::value,__uint128_t,unsigned __int128>;
template<class _Tp>using __is_integer=typename std::conditional<std::is_integral<_Tp>::value||__is_signed_int128<_Tp>::value||__is_unsigned_int128<_Tp>::value,std::true_type,std::false_type>::type;
template<class _Tp>using __is_signed_integer=typename std::conditional<(__is_integer<_Tp>::value&&std::is_signed<_Tp>::value)||__is_signed_int128<_Tp>::value,std::true_type,std::false_type>::type;
template<class _Tp>using __is_unsigned_integer=typename std::conditional<(__is_integer<_Tp>::value&&std::is_unsigned<_Tp>::value)||__is_unsigned_int128<_Tp>::value,std::true_type,std::false_type>::type;
template<class _Tp>using __to_unsigned=typename std::conditional<__is_signed_int128<_Tp>::value,__make_unsigned_int128<_Tp>,typename std::conditional<std::is_signed<_Tp>::value,std::make_unsigned<_Tp>,std::common_type<_Tp>>::type>::type;
template<class _Tp>using __is_signed_integer_type=std::enable_if_t<__is_signed_integer<_Tp>::value>;
template<class _Tp>using __is_unsigned_integer_type=std::enable_if_t<__is_unsigned_integer<_Tp>::value>;
template<class _Tp>using __to_unsigned_type=typename __to_unsigned<_Tp>::type;
}using namespace type_traits;
}//namespace internal
}//namespace LXcjh4998
namespace LXcjh4998{
namespace internal{
namespace check_char_type{
inline bool __is_digit(const char &c){return 48<=c&&c<=57;}
inline bool __is_not_blank(const char &c){return c^32&&c^10&&c^13&&c&&~c;}
}using namespace check_char_type;
}//namespace internal
namespace fastIO{
//#define LOCAL
#ifndef LOCAL
constexpr int __maxn=1<<20;char __in[__maxn],__out[__maxn],*__p1=__in,*__p2=__in,*__p3=__out;
#define getchar() (__p1==__p2&&(__p2=(__p1=__in)+fread(__in,1,__maxn,stdin),__p1==__p2)?EOF:*__p1++)
#define flush() (fwrite(__out,1,__p3-__out,stdout))
#define putchar(ch) (__p3==__out+__maxn&&(flush(),__p3=__out),*__p3++=(ch))
class __flush{public:~__flush(){flush();}}__var_to_flush;
#else
#ifdef __linux__
#define getchar() getchar_unlocked()
#define putchar(ch) putchar_unlocked(ch)
#else
#define getchar() _getchar_nolock()
#define putchar(ch) _putchar_nolock(ch)
#endif
#endif
template<typename _Tp,internal::__is_signed_integer_type<_Tp>* =nullptr>inline _Tp read(_Tp &x){x=0;bool flag(false);char ch=getchar();while(!internal::__is_digit(ch))flag^=!(ch^45),ch=getchar();while(internal::__is_digit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return flag?x=-x:x;}
template<typename _Tp,internal::__is_unsigned_integer_type<_Tp>* =nullptr>inline _Tp read(_Tp &x){x=0;char ch=getchar();while(!internal::__is_digit(ch))ch=getchar();while(internal::__is_digit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x;}
inline char read(char &x){do x=getchar();while(!internal::__is_not_blank(x));return x;}
inline void read(char *x){char ch;read(ch);do *(x++)=ch;while(internal::__is_not_blank(ch=getchar()));}
inline void read(std::string &x){char ch;read(ch),x.clear();do x+=ch;while(internal::__is_not_blank(ch=getchar()));}
template<typename _Tp,typename ..._Args>inline void read(_Tp &x,_Args &... y){read(x),read(y...);}
template<typename _Tp,internal::__is_signed_integer_type<_Tp>* =nullptr>inline void write(_Tp x){x<0?x=-x,putchar(45):0;short stk[50],tp(0);do stk[++tp]=x%10,x/=10;while(x);while(tp)putchar(stk[tp--]|48);}
template<typename _Tp,internal::__is_unsigned_integer_type<_Tp>* =nullptr>inline void write(_Tp x){short stk[50],tp(0);do stk[++tp]=x%10,x/=10;while(x);while(tp)putchar(stk[tp--]|48);}
inline char write(const char &x){return putchar(x);}
template<typename _Tp>inline void write(_Tp *x){while(*x)putchar(*(x++));}
inline void write(const std::string &x){for(const char &ch:x)putchar(ch);}
template<typename _Tp,typename ..._Args>inline void write(const _Tp &x,const _Args&... y){write(x),write(y...);}
template<typename _Tp>inline void writeln(const _Tp &x){write(x),putchar(10);}
template<typename _Tp,typename ..._Args>inline void writeln(const _Tp &x,const _Args&... y){write(x),write(y...),putchar(10);}
#undef getchar
#undef putchar
#ifndef LOCAL
#undef flush
#endif
}using namespace fastIO;
}//namespace LXcjh4998
using namespace LXcjh4998;
int n,q,A[N],op[M],l[M],r[M],x[M],ans[M],pre[N];
int fa[N],root[V],val[N],sz[V],mx,sub;
int find(int x){while(x!=fa[x])x=fa[x]=fa[fa[x]];return x;}
inline void merge(int u,int v){
if(root[u])fa[root[v]]=root[u];
else val[root[u]=root[v]]=u;
sz[u]+=sz[v];root[v]=sz[v]=0;
}
inline void build(int bl,int br){
mx=sub=0;
forUp(pos,bl,br){
chkMax(mx,A[pos]);
if(root[A[pos]])fa[pos]=root[A[pos]];
else val[root[A[pos]]=fa[pos]=pos]=A[pos];
++sz[A[pos]];
}
}
inline void modify(int x){
if((x<<1)<=mx-sub){
forUp(v,sub+1,sub+x)if(root[v])merge(v+x,v);
sub+=x;
}
else{
forUp(v,sub+x+1,mx)if(root[v])merge(v-x,v);
mx=x+sub;
}
}
inline void rebuild(int bl,int br,int ql,int qr,int x){
forUp(pos,bl,br)A[pos]=val[find(pos)],root[A[pos]]=sz[A[pos]]=0,A[pos]-=sub;
forUp(pos,bl,br)val[pos]=0;
forUp(pos,ql,qr)if(A[pos]>x)A[pos]-=x;
build(bl,br);
}
inline void query(int bl,int br,int ql,int qr,int x,int qid){
if(bl==ql&&qr==br)ans[qid]+=sz[x+sub];
else forUp(pos,ql,qr)if(val[find(pos)]-sub==x)++ans[qid];
}
bool __med;void __solve(int __test_id){
read(n,q);
forUp(i,1,n)read(A[i]),pre[i]=pre[i-1]+(A[i]==0);
forUp(i,1,q)read(op[i],l[i],r[i],x[i]),ans[i]=x[i]==0?pre[r[i]]-pre[l[i]-1]:0;
int bl=1-B,br=0,btot=(n-1)/B+1;forUp(bid,1,btot){
bl+=B,br=min(br+B,n);
build(bl,br);
forUp(i,1,q){
int ql=l[i],qr=r[i],qx=x[i];
if(qr<bl||br<ql||qx==0||qx+sub>mx)continue;
chkMax(ql,bl),chkMin(qr,br);
if(op[i]==1){
if(ql==bl&&br==qr)modify(qx);
else rebuild(bl,br,ql,qr,qx);
}
else query(bl,br,ql,qr,qx,i);
}
forUp(pos,bl,br)root[A[pos]]=0;
forUp(v,1,mx)sz[v]=0;
}
forUp(i,1,q)if(op[i]==2)writeln(ans[i]);
}
signed main(){
__init();
forUp(i,1,__test_num)__solve(i);
cerr<<1000.0*(clock()-__tst)/CLOCKS_PER_SEC<<"ms "<<((&__mst)-(&__med))/1024.0/1024.0<<"MB"<<'\n';
return 0;
}
void __init(){
__tst=clock();
#ifndef use_file
//#define use_file
#endif
#ifdef use_file
const string __file_name="test";freopen((__file_name+".in").c_str(),"r",stdin);freopen((__file_name+".out").c_str(),"w",stdout);
#endif
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//scanf("%d",&__test_num);
//cin>>__test_num;
}