/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 * OK, 0.574 sec, 1.2 mb
 */

#include <cstdio>
#include <cstdlib>
#include <cassert>

#include <algorithm>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

#define NAME "permutation"

const int maxN = (int)1e5;
const int K1 = 1 << 8, AND1 = K1 - 1;
const int K2 = 1 << 13, AND2 = K2 - 1;

int n, k, p[maxN];
int s1[maxN + K1];
int s2[maxN + K2];

int main()
{
  assert(freopen(NAME ".in", "r", stdin));
  assert(freopen(NAME ".out", "w", stdout));

  scanf("%d%d", &n, &k);
  forn(i, n)
    scanf("%d", &p[i]), s2[i] = s1[i] = p[i];
  for (int i = 0; i < n; i += K1)
    sort(s1 + i, s1 + i + K1);
  for (int i = 0; i < n; i += K2)
    sort(s2 + i, s2 + i + K2);
  while (k--)
  {
    int L, R, X, Y, cur = 0;
    scanf("%d%d%d%d", &L, &R, &X, &Y), L--;

    while (L < R)
    {
      if (L + K2 <= R && (L & AND2) == 0)
        cur += lower_bound(s2 + L, s2 + L + K2, Y + 1) - lower_bound(s2 + L, s2 + L + K2, X), L += K2;
      else if (L + K1 <= R && (L & AND1) == 0)
        cur += lower_bound(s1 + L, s1 + L + K1, Y + 1) - lower_bound(s1 + L, s1 + L + K1, X), L += K1;
      else
        cur += (X <= p[L] && p[L] <= Y), L++;
    }
    printf("%d\n", cur);
  }
  return 0;
}
