#include<bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
#define pii pair<int,int>
#define vint vector<int>
#define pb push_back
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
#define Help freopen("a.cpp","r",stdin)
#define db double
#define ld long db
using namespace std;
void Freopen(){
	freopen(title".in","r",stdin);
	freopen(title".out","w",stdout);
}
int read(){
	int g=0,f=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
	return g*f;
}
const int N=1e5+5;
const int size=260;
const int Maxn=1e5;
int maxn[N],L[N],R[N],a[N],fa[N],v[N],tag[400],bel[N],n,q;
struct node{int num,rt;}G[400][N];
void init(int x){
	maxn[x]=0;
	rep(i,L[x],R[x]){
		maxn[x]=max(maxn[x],a[i]);
		G[x][a[i]].num++;
		if(G[x][a[i]].rt)fa[i]=G[x][a[i]].rt;
		else v[i]=a[i],G[x][a[i]].rt=fa[i]=i;
	}
}
void clear(int x){
	rep(i,L[x],R[x]){a[i]=v[fa[i]];G[x][a[i]].num=G[x][a[i]].rt=0;a[i]-=tag[x];}
	tag[x]=0;rep(i,L[x],R[x])fa[i]=0;
}
void solve(int x,int a,int b){
	node &s=G[x][a],&t=G[x][b];
	if(t.rt)fa[s.rt]=t.rt;else t.rt=s.rt,v[s.rt]=b;
	t.num+=s.num,s.num=s.rt=0;
}
void updata(int x,int w){
	if(2*w<=maxn[x]-tag[x]){rep(i,1+tag[x],w+tag[x])if(G[x][i].rt)solve(x,i,i+w);tag[x]+=w;}
	else{rep(i,tag[x]+w+1,maxn[x])if(G[x][i].rt)solve(x,i,i-w);maxn[x]=min(maxn[x],tag[x]+w);}
}
void change(int l,int r,int x){
	int ql=bel[l],qr=bel[r];clear(ql);if(ql!=qr)clear(qr);
	if(ql!=qr){
		rep(i,l,R[ql])if(a[i]>x)a[i]-=x;
		rep(i,L[qr],r)if(a[i]>x)a[i]-=x;
		rep(i,ql+1,qr-1)updata(i,x);
		init(ql),init(qr);
	}else{rep(i,l,r)if(a[i]>x)a[i]-=x;init(ql);}
}
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
int query(int l,int r,int x){
	int ql=bel[l],qr=bel[r],ans=0;
	if(ql!=qr){
		rep(i,l,R[ql])if(v[get(i)]-tag[ql]==x)ans++;
		rep(i,L[qr],r)if(v[get(i)]-tag[qr]==x)ans++;
		rep(i,ql+1,qr-1)if(x+tag[i]<=Maxn)ans+=G[i][x+tag[i]].num;
	}else{rep(i,l,r)if(v[get(i)]-tag[ql]==x)ans++;}
	return ans;
}
signed main(){
	n=read(),q=read();
	rep(i,1,n)a[i]=read(),bel[i]=(i-1)/size+1,L[bel[i]]=Maxn;
	rep(i,1,n)L[bel[i]]=min(L[bel[i]],i),R[bel[i]]=max(R[bel[i]],i);
	rep(i,1,bel[n])init(i),tag[i]=0;
	rep(i,1,q){
		int op=read(),l=read(),r=read(),x=read();
		if(op==1)change(l,r,x);else cout<<query(l,r,x)<<'\n';
	}return signed();
}