#include <bits/stdc++.h>
using namespace std;
int main(){
int n , q;
cin >> n >> q;
vector<int> a(n);
for(int i=0;i<n;++i)cin >> a[i];
vector<int> red = a;
vector<vector<int>> queries;
for(int i=0;i<q;++i){
int l,r,v1,v2;
cin >> l >> r >> v1 >> v2 ;
queries.push_back({r,v2,i,1});
queries.push_back({l,v1-1,i,-1});
red.push_back(v2);
red.push_back(v1-1);
}
sort(begin(red), end(red));
red.erase( unique(begin(red), end(red)) , end(red));
int m = red.size() + 2;
sort(begin(queries), end(queries));
vector<int> bit(m,0);
auto update = [&](int index,int value)->void{
for(;index<m;index+=index&-index)bit[index]+=value;
};
auto ask = [&](int index)->int{
int ret=0;
for(;index>0;index-=index&-index) ret += bit[index];
return ret;
};
auto compressed_value = [&](int val)->int{
return lower_bound(begin(red), end(red), val) - begin(red) + 1;
};
int it = 0;
vector<int> answer(q,0);
for(auto query : queries){
const int value = compressed_value(query[1]);
for(;it<n && it<=query[0];++it) update( compressed_value(a[it]), 1);
answer[query[2]] += query[3] * ask(value);
}
for(int i=0;i<q;++i) cout << answer[i] << "\n" ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCl7CiAgICBpbnQgbiAsIHE7CiAgICBjaW4gPj4gbiA+PiBxOwogICAgCiAgICB2ZWN0b3I8aW50PiBhKG4pOwogICAgZm9yKGludCBpPTA7aTxuOysraSljaW4gPj4gYVtpXTsKICAgIAogICAgdmVjdG9yPGludD4gcmVkID0gYTsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gcXVlcmllczsKICAgIGZvcihpbnQgaT0wO2k8cTsrK2kpewogICAgICAgIGludCBsLHIsdjEsdjI7CiAgICAgICAgY2luID4+IGwgPj4gciA+PiB2MSA+PiB2MiA7CiAgICAgICAgCiAgICAgICAgcXVlcmllcy5wdXNoX2JhY2soe3IsdjIsaSwxfSk7CiAgICAgICAgcXVlcmllcy5wdXNoX2JhY2soe2wsdjEtMSxpLC0xfSk7CiAgICAgICAgcmVkLnB1c2hfYmFjayh2Mik7CiAgICAgICAgcmVkLnB1c2hfYmFjayh2MS0xKTsKICAgIH0KICAgIAogICAgc29ydChiZWdpbihyZWQpLCBlbmQocmVkKSk7CiAgICByZWQuZXJhc2UoIHVuaXF1ZShiZWdpbihyZWQpLCBlbmQocmVkKSkgLCBlbmQocmVkKSk7CiAgICBpbnQgbSA9IHJlZC5zaXplKCkgKyAyOwogICAgCiAgICBzb3J0KGJlZ2luKHF1ZXJpZXMpLCBlbmQocXVlcmllcykpOwogICAgdmVjdG9yPGludD4gYml0KG0sMCk7CiAgICAKICAgIGF1dG8gdXBkYXRlID0gWyZdKGludCBpbmRleCxpbnQgdmFsdWUpLT52b2lkewogICAgICAgIGZvcig7aW5kZXg8bTtpbmRleCs9aW5kZXgmLWluZGV4KWJpdFtpbmRleF0rPXZhbHVlOwogICAgfTsKICAgIAogICAgYXV0byBhc2sgPSBbJl0oaW50IGluZGV4KS0+aW50ewogICAgICAgIGludCByZXQ9MDsKICAgICAgICBmb3IoO2luZGV4PjA7aW5kZXgtPWluZGV4Ji1pbmRleCkgcmV0ICs9IGJpdFtpbmRleF07CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH07CiAgICAKICAgIGF1dG8gY29tcHJlc3NlZF92YWx1ZSA9IFsmXShpbnQgdmFsKS0+aW50ewogICAgICAgIHJldHVybiBsb3dlcl9ib3VuZChiZWdpbihyZWQpLCBlbmQocmVkKSwgdmFsKSAtIGJlZ2luKHJlZCkgKyAxOwogICAgfTsKICAgIAogICAgaW50IGl0ID0gMDsKICAgIAogICAgdmVjdG9yPGludD4gYW5zd2VyKHEsMCk7CiAgICBmb3IoYXV0byBxdWVyeSA6IHF1ZXJpZXMpewogICAgICAgIGNvbnN0IGludCB2YWx1ZSA9IGNvbXByZXNzZWRfdmFsdWUocXVlcnlbMV0pOwogICAgICAgIGZvcig7aXQ8biAmJiBpdDw9cXVlcnlbMF07KytpdCkgdXBkYXRlKCBjb21wcmVzc2VkX3ZhbHVlKGFbaXRdKSwgMSk7CiAgICAgICAgYW5zd2VyW3F1ZXJ5WzJdXSArPSBxdWVyeVszXSAqIGFzayh2YWx1ZSk7CiAgICB9CiAgICAKICAgIGZvcihpbnQgaT0wO2k8cTsrK2kpIGNvdXQgPDwgYW5zd2VyW2ldIDw8ICJcbiIgOwp9