#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 << " ";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7CglpbnQgTiwgUSAsIGNudCA9IDA7CgljaW4gPj4gTiA+PiBROwoJdmVjdG9yPGludD4gdihOKSwgIGRpcywgYW5zKFEpOwoJbWFwPGludCxpbnQ+IG1wOwoJZm9yKGludCAmeCA6IH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B7CgkJCQlyZXMgXj0gbGc7CgkJCQl0b3QgKz0gZmVud2lja0NvdW50W3Jlc107CgkJCQlzdW0gKz0gZmVud2lja1tyZXNdOwoJCQl9CgkJfQoJCXJldHVybiBzdW07CgkJCQoJfTsKCQoJLy8vIHF1ZXJ5IHByb2Nlc3Npbmcgb2ZmbGluZSBtb2RlCglmb3IoaW50IGkgPSBOIC0gMSA7IGkgPj0gMDsgaS0tKSB7CgkJdXBkYXRlKHZbaV0pOwoJCWZvcihhdXRvICZhciA6IHFyeVtpXSkgewoJCQlpbnQgbCA9IGkgLCByID0gYXJbMF0gLCB4ID0gYXJbMV0gLCB5ID0gYXJbMl0gOwoJCQlpbnQgaWR4ID0gYXJbM107CgkJCS8vIHByb2Nlc3MgcXVlcnkKCQkJCgkJCWFuc1tpZHhdID0gMCA7Ly8vCgkJfQkKCX0KCQoJLy8gb3V0cHV0IAoJZm9yKGludCBYIDogYW5zKSBjb3V0IDw8IFggPDwgIiAiOwp9
prog.cpp: In lambda function:
prog.cpp:25:31: error: operands to ?: have different types ‘__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type’ {aka ‘std::pair<int, int>’} and ‘int’
int bcf = fenwick[x].size() ? fenwick[x].back() : 0 ;
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:26:37: error: operands to ?: have different types ‘__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type’ {aka ‘std::pair<int, int>’} and ‘int’
int bcfc = fenwickCount[x].size() ? fenwickCount[x].back() : 0 ;
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:27:82: error: expected ‘;’ before ‘for’
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);
~~~
prog.cpp:28:45: error: expected ‘;’ before ‘)’ token
for(x = mp[v] ; x < (N << 2) ; x += x & -x) fenwickCount[x].emplace_back(r , bcfc + 1);
^
;
prog.cpp: In lambda function:
prog.cpp:34:60: error: ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::pair<int, int> > >, std::vector<std::pair<int, int> > >::value_type’ {aka ‘class std::vector<std::pair<int, int> >’} has no member named ‘get’
if(res + lg < (N << 2) and tot + fenwickCount[res ^ lg].get(r) <= topK) {
^~~
prog.cpp:36:9: error: no match for ‘operator+=’ (operand types are ‘int’ and ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::pair<int, int> > >, std::vector<std::pair<int, int> > >::value_type’ {aka ‘std::vector<std::pair<int, int> >’})
tot += fenwickCount[res];
prog.cpp:37:9: error: no match for ‘operator+=’ (operand types are ‘int’ and ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::pair<int, int> > >, std::vector<std::pair<int, int> > >::value_type’ {aka ‘std::vector<std::pair<int, int> >’})
sum += fenwick[res];
prog.cpp: In function ‘int main()’:
prog.cpp:46:14: error: no match for call to ‘(main()::<lambda(int, int)>) (__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)’
update(v[i]);
^
prog.cpp:23:31: note: candidate: ‘main()::<lambda(int, int)>’
auto update = [&](int x,int r) {
^
prog.cpp:23:31: note: candidate expects 2 arguments, 1 provided