#include <cstdio>
#include <iostream>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define forv(i, v) for(int i = 0; i < int(v.size()); i++)
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
inline int ni() { int a; scanf("%d", &a); return a; }
const int max_n = 100000;
vector < int > values[4 * max_n];
int arr[max_n];
char buffer[8];
void build(int v, int l, int r) {
if(l == r) {
values[v].pb(arr[l]);
return;
}
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m + 1, r);
forv(i, values[2 * v]) values[v].pb(values[2 * v][i]);
forv(i, values[2 * v + 1]) values[v].pb(values[2 * v + 1][i]);
sort(all(values[v]));
}
int calc(int v, int a, int b) {
//vector < int > :: iterator l = lower_bound(values[v].begin(), values[v].end(), a);
//vector < int > :: iterator r = lower_bound(values[v].begin(), values[v].end(), b + 1);
//return (r - l);
int res = 0;
for(int i = 0; i < int(values[v].size()); i++) {
if(a <= values[v][i] && values[v][i] <= b) ++res;
}
return res;
}
int query(int v, int l, int r, int tl, int tr, int a, int b) {
if(l == tl && r == tr) {
return calc(v, a, b);
}
int res = 0, m = (l + r) / 2;
if(tl <= m) res += query(2 * v, l, m, tl, min(m, tr), a, b);
if(tr > m) res += query(2 * v + 1, m + 1, r, max(m + 1, tl), tr, a, b);
return res;
}
int main() {
//freopen("h8.in", "r", stdin);
//freopen("h8.out", "w", stdout);
int n = ni(), m = ni();
forn(i, n) {
arr[i] = ni();
}
build(1, 0, n - 1);
forn(i, m) {
scanf("%s", buffer);
int l = ni() - 1;
int r = ni() - 1;
int a = ni();
int b = ni();
printf("%d\n", query(1, 0, n - 1, l, r, a, b));
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNhc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