#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#include <immintrin.h>
#include <cstdio>

const int Maxn = 5e5 + 5;
int n, m, as[Maxn], *ass;

void read(int &value)
{
	value = 0;
	bool minus = 0;
	char now = getchar();
	while (now < '0' || now > '9')
	{
		if (now == '-') 	minus = 1;
		now = getchar();
	}
	while (now >= '0' && now <= '9')
	{
		value = (value << 3) + (value << 1) + (now ^ '0');
		now = getchar();
	}
	if (minus)	value = -value;
}

void modify(int *ls, int *rs, int x)
{
	__m256i now = _mm256_set_epi32(x, x, x, x, x, x, x, x);
	while ((long long)(ls) % 32 && ls <= rs)
	{
		if (*ls > x)	*ls -= x;
		++ls;
	}
	while (ls + 15 <= rs)
	{
		__m256i tmp = *(__m256i*)ls;
		*(__m256i*)ls = _mm256_add_epi32(tmp, _mm256_mullo_epi32(_mm256_cmpgt_epi32(tmp, now), now));
		ls += 16;
	}
	while (ls <= rs)
	{
		if (*ls > x)	*ls -= x;
		++ls;
	}
}

int query(int *ls, int *rs, int x)
{
	__m256i cur = _mm256_set_epi32(x, x, x, x, x, x, x, x);
	__m256i unit = _mm256_setzero_si256();
	int res = 0;
	while ((long long)(ls) % 32 && ls <= rs)
	{
		if (*ls == x)	++res;
		++ls;
	}
	while (ls + 7 <= rs)
	{
		unit = _mm256_sub_epi32(unit, _mm256_cmpeq_epi32(*(__m256i*)ls, cur));
		ls += 8;
	}
	while (ls <= rs)
	{
		if (*ls == x)	++res;
		++ls;
	}
	for (int i = 0; i < 8; ++i) 	res += ((int*)&unit)[i];
	return res;
}

signed main()
{
	read(n), read(m);
	ass = new int[128];
	while ((long long)(ass) % 32)	++ass;
	for (int i = 1; i <= n; ++i)	read(as[i]);
	for (int i = 1; i <= m; ++i)
	{
		int opt, opl, opr, opx;
		read(opt), read(opl), read(opr), read(opx);
		if (opt == 1)	modify(as + opl, as + opr, opx);
		else	printf("%d\n", query(as + opl, as + opr, opx));
	}
	return 0;
}