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

#include <cstdio>
#include <cassert>

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

#define NAME "permutation"

const int maxN = (int)1e5;
const int logK = 9, maxK = 1 << logK, AND = maxK - 1;

int n, k, p[maxN], q[maxN];
int s[maxK][maxK];

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]), q[--p[i]] = i;
  forn(i, n)
    s[i >> logK][(p[i] >> logK) + 1]++;
  forn(i, maxK)
    forn(j, maxK - 1)
      s[i][j + 1] += s[i][j];
  while (k--)
  {
    int L, R, X, Y, cur = 0;
    scanf("%d%d%d%d", &L, &R, &X, &Y), L--, X--;
    while (L < R && (L & AND)) cur += (X <= p[L] && p[L] < Y), L++;
    while (L < R && (R & AND)) R--, cur += (X <= p[R] && p[R] < Y);
    while (X < Y && (X & AND)) cur += (L <= q[X] && q[X] < R), X++;
    while (X < Y && (Y & AND)) Y--, cur += (L <= q[Y] && q[Y] < R);
    L >>= logK, R >>= logK, X >>= logK, Y >>= logK;
    for (; L < R; L++)
      cur += s[L][Y] - s[L][X];
    printf("%d\n", cur);
  }
  return 0;
}
