#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
const int MAX_Q = 100005;
const int BLOCK = 855;
struct Query {
int l, u, v, k, id;
friend bool operator<(Query &A, Query &B) {
if (A.u / BLOCK != B.u / BLOCK) {
return A.u / BLOCK < B.u / BLOCK;
}
return A.v < B.v;
}
};
int n, numQuery;
int a[MAX_N];
Query queries[MAX_Q];
vector<pair<int, int> > compress;
int valueAt[MAX_N + 2 * MAX_Q];
long long sum[MAX_N], sum_in_bl[MAX_N / BLOCK + 2];
int answer[MAX_Q];
void add(int pos, int val) {
if (valueAt[pos] == 0) {
return;
}
int x = valueAt[pos];
sum[x] += val * a[x];
sum_in_bl[x / BLOCK] += val * a[x];
}
int get(int pos, int k) {
for (int i = pos; i <= min(n, (pos / BLOCK + 1) * BLOCK - 1); i++) {
if (sum[i] > k) {
return i - pos;
} else {
k -= sum[i];
}
}
for (int i = pos / BLOCK + 1; i <= n / BLOCK; i++) {
if (sum_in_bl[i] > k) {
for (int j = max(0, i * BLOCK); j <= min(n, (i + 1) * BLOCK - 1); j++) {
if (sum[j] > k) {
return j - pos;
} else {
k -= sum[j];
}
}
break;
} else {
k -= sum_in_bl[i];
}
}
return n - pos + 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("SHOPPING.inp", "r", stdin);
freopen("SHOPPING.out", "w", stdout);
cin >> n >> numQuery;
for (int i = 1; i <= n; i++) {
cin >> a[i];
compress.emplace_back(a[i], -i);
}
for (int i = 1; i <= numQuery; i++) {
cin >> queries[i].l >> queries[i].u >> queries[i].v >> queries[i].k;
queries[i].id = i;
compress.emplace_back(queries[i].u, i);
compress.emplace_back(queries[i].v, i + numQuery);
}
sort(compress.begin(), compress.end(), [&](pair<int, int> &x, pair<int, int> &y) {
if (x.first != y.first) {
return x.first < y.first;
}
if (x.second < 0) {
return (y.second > numQuery);
}
if (x.second <= numQuery) {
if (y.second < 0) {
return true;
}
return x.second < y.second;
}
return false;
});
for (int i = 0; i < (int)compress.size(); i++) {
pair<int, int> x = compress[i];
if (x.second < 0) {
valueAt[i + 1] = -x.second;
} else {
if (x.second > numQuery) {
queries[x.second - numQuery].v = i + 1;
} else {
queries[x.second].u = i + 1;
}
}
}
sort(queries + 1, queries + numQuery + 1);
int curLeft = queries[1].u, curRight = queries[1].u - 1;
for (int i = 1; i <= numQuery; i++) {
while (curRight < queries[i].v) {
add(++curRight, +1);
}
while (curRight > queries[i].v) {
add(curRight--, -1);
}
while (curLeft < queries[i].u) {
add(curLeft++, -1);
}
while (curLeft > queries[i].u) {
add(--curLeft, +1);
}
answer[queries[i].id] = get(queries[i].l, queries[i].k);
}
for (int i = 1; i <= numQuery; i++) {
cout << answer[i] << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWF9OID0gMTAwMDA1Owpjb25zdCBpbnQgTUFYX1EgPSAxMDAwMDU7CmNvbnN0IGludCBCTE9DSyA9IDg1NTsKCnN0cnVjdCBRdWVyeSB7CiAgICBpbnQgbCwgdSwgdiwgaywgaWQ7CiAgICBmcmllbmQgYm9vbCBvcGVyYXRvcjwoUXVlcnkgJkEsIFF1ZXJ5ICZCKSB7CiAgICAgICAgaWYgKEEudSAvIEJMT0NLICE9IEIudSAvIEJMT0NLKSB7CiAgICAgICAgICAgIHJldHVybiBBLnUgLyBCTE9DSyA8IEIudSAvIEJMT0NLOwogICAgICAgIH0KICAgICAgICByZXR1cm4gQS52IDwgQi52OwogICAgfQp9OwoKaW50IG4sIG51bVF1ZXJ5OwppbnQgYVtNQVhfTl07ClF1ZXJ5IHF1ZXJpZXNbTUFYX1FdOwp2ZWN0b3I8cGFpcjxpbnQsIGludD4gPiBjb21wcmVzczsKaW50IHZhbHVlQXRbTUFYX04gKyAyICogTUFYX1FdOwpsb25nIGxvbmcgc3VtW01BWF9OXSwgc3VtX2luX2JsW01BWF9OIC8gQkxPQ0sgKyAyXTsKaW50IGFuc3dlcltNQVhfUV07Cgp2b2lkIGFkZChpbnQgcG9zLCBpbnQgdmFsKSB7CiAgICBpZiAodmFsdWVBdFtwb3NdID09IDApIHsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBpbnQgeCA9IHZhbHVlQXRbcG9zXTsKICAgIHN1bVt4XSArPSB2YWwgKiBhW3hdOwogICAgc3VtX2luX2JsW3ggLyBCTE9DS10gKz0gdmFsICogYVt4XTsKfQoKaW50IGdldChpbnQgcG9zLCBpbnQgaykgewogICAgZm9yIChpbnQgaSA9IHBvczsgaSA8PSBtaW4obiwgKHBvcyAvIEJMT0NLICsgMSkgKiBCTE9DSyAtIDEpOyBpKyspIHsKICAgICAgICBpZiAoc3VtW2ldID4gaykgewogICAgICAgICAgICByZXR1cm4gaSAtIHBvczsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBrIC09IHN1bVtpXTsKICAgICAgICB9CiAgICB9CgogICAgZm9yIChpbnQgaSA9IHBvcyAvIEJMT0NLICsgMTsgaSA8PSBuIC8gQkxPQ0s7IGkrKykgewogICAgICAgIGlmIChzdW1faW5fYmxbaV0gPiBrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSBtYXgoMCwgaSAqIEJMT0NLKTsgaiA8PSBtaW4obiwgKGkgKyAxKSAqIEJMT0NLIC0gMSk7IGorKykgewogICAgICAgICAgICAgICAgaWYgKHN1bVtqXSA+IGspIHsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gaiAtIHBvczsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgayAtPSBzdW1bal07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgayAtPSBzdW1faW5fYmxbaV07CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBuIC0gcG9zICsgMTsKfQoKaW50IG1haW4oKSB7CiAgICBpb3M6O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lY29uZCA8IDApIHsKICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB4LnNlY29uZCA8IHkuc2Vjb25kOwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgfSk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAoaW50KWNvbXByZXNzLnNpemUoKTsgaSsrKSB7CiAgICAgICAgcGFpcjxpbnQsIGludD4geCA9IGNvbXByZXNzW2ldOwogICAgICAgIGlmICh4LnNlY29uZCA8IDApIHsKICAgICAgICAgICAgdmFsdWVBdFtpICsgMV0gPSAteC5zZWNvbmQ7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaWYgKHguc2Vjb25kID4gbnVtUXVlcnkpIHsKICAgICAgICAgICAgICAgIHF1ZXJpZXNbeC5zZWNvbmQgLSBudW1RdWVyeV0udiA9IGkgKyAxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcXVlcmllc1t4LnNlY29uZF0udSA9IGkgKyAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHNvcnQocXVlcmllcyArIDEsIHF1ZXJpZXMgKyBudW1RdWVyeSArIDEpOwoKICAgIGludCBjdXJMZWZ0ID0gcXVlcmllc1sxXS51LCBjdXJSaWdodCA9IHF1ZXJpZXNbMV0udSAtIDE7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbnVtUXVlcnk7IGkrKykgewogICAgICAgIHdoaWxlIChjdXJSaWdodCA8IHF1ZXJpZXNbaV0udikgewogICAgICAgICAgICBhZGQoKytjdXJSaWdodCwgKzEpOwogICAgICAgIH0KICAgICAgICB3aGlsZSAoY3VyUmlnaHQgPiBxdWVyaWVzW2ldLnYpIHsKICAgICAgICAgICAgYWRkKGN1clJpZ2h0LS0sIC0xKTsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKGN1ckxlZnQgPCBxdWVyaWVzW2ldLnUpIHsKICAgICAgICAgICAgYWRkKGN1ckxlZnQrKywgLTEpOwogICAgICAgIH0KICAgICAgICB3aGlsZSAoY3VyTGVmdCA+IHF1ZXJpZXNbaV0udSkgewogICAgICAgICAgICBhZGQoLS1jdXJMZWZ0LCArMSk7CiAgICAgICAgfQogICAgICAgIGFuc3dlcltxdWVyaWVzW2ldLmlkXSA9IGdldChxdWVyaWVzW2ldLmwsIHF1ZXJpZXNbaV0uayk7CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbnVtUXVlcnk7IGkrKykgewogICAgICAgIGNvdXQgPDwgYW5zd2VyW2ldIDw8ICdcbic7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K