#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int N, Q , cnt = 0;
	cin >> N >> Q;
	vector<int> v(N),  dis, ans(Q);
	map<int,int> mp;
	for(int &x : v) cin >> x, mp[x];
	vector<array<int,4>> qry[N + 1];

	for(int _Q = 0 ; _Q < Q; _Q++) { 
		int l,r,x,y;
		cin >> l >> r >> x >> y;
		qry[l].push_back({r , x, y , _Q});
	}
	
	for(auto &pr : mp) pr.second = ++cnt;
	
	vector<vector<pair<int,int>>> fenwick(4 * N), fenwickCount(4 * N);
	
	auto update = [&](int x,int r) {
		int v = x;
		int bcf = fenwick[x].size() ? fenwick[x].back() : 0 ;
		int bcfc = fenwickCount[x].size() ? fenwickCount[x].back() : 0 ;
		for(x = mp[x]; x < (N << 2) ; x += x & -x) fenwick[x].emplace_back(r , v + bcf)
		for(x = mp[v] ; x < (N << 2) ; x += x & -x) fenwickCount[x].emplace_back(r , bcfc + 1);
	};
	
	auto query = [&](int topK,int r) {
		int res = 0,tot = 0, sum = 0;
		for(int lg = (1 << 20) ; lg ; lg >>= 1) {
			if(res + lg < (N << 2) and tot + fenwickCount[res ^ lg].get(r) <= topK) {
				res ^= lg;
				tot += fenwickCount[res];
				sum += fenwick[res];
			}
		}
		return sum;
			
	};
	
	/// query processing offline mode
	for(int i = N - 1 ; i >= 0; i--) {
		update(v[i]);
		for(auto &ar : qry[i]) {
			int l = i , r = ar[0] , x = ar[1] , y = ar[2] ;
			int idx = ar[3];
			// process query
			
			ans[idx] = 0 ;///
		}	
	}
	
	// output 
	for(int X : ans) cout << X << " ";
}