#gcc optimize("-O3")
#include<bits/stdc++.h>
#define eps 1e-9
#define MOD1 998244353
#define MOD2 1000000007
#define INV_2 499122177
#define INF 1000000000
#define PI 3.14159265358979323846
using namespace std;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, q;
cin >> n >> q;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
vector<pair<pair<int, int> , int> >v(q);
for(int i = 0; i < q; i++)
{
cin >> v[i].first.second >> v[i].first.first;
v[i].first.second--;
v[i].first.first--;
v[i].second=i;
}
sort(v.begin(), v.end());
int p=1;
while(p <= 200001)
p*=2;
vector<pair<int, int> >tree[2*p+1];
for(int i = 0; i < 2*p+1; i++)
tree[i].push_back(make_pair(-1, 0));
int idx=0;
int ans[q];
int k=0;
for(int i = 0; i < n; i++)
{
int x, y;
//cout << "\n--------------\n";
//cout << i << '\n';
x=p+a[i];
while(x >= 1)
{
int nb=tree[x].back().second+1;
tree[x].push_back(make_pair(i, nb));
x/=2;
}
while(idx < q && v[idx].first.first == i)
{
int l = 0, r = 200000;
while(l < r)
{
int med=(l+r)/2+1;
int cu=0;
x=p+med;
y=p+200000;
while(x <= y)
{
if(x%2==1)
{
int xx = ((lower_bound(tree[x].begin(), tree[x].end(), make_pair(v[idx].first.second, 0))-tree[x].begin()));
cu+=tree[x].back().second-tree[x][xx-1].second;
x++;
}
if(y%2==0)
{
int yy=((lower_bound(tree[y].begin(), tree[y].end(), make_pair(v[idx].first.second, 0))-tree[y].begin()));
cu+=tree[y].back().second-tree[y][yy-1].second;
y--;
}
x/=2;
y/=2;
}
if(cu >= med)
l=med;
else
r=med-1;
/*k++;
if(k > 1000)
return 0;*/
//cout << v[idx].first.second << ' ' << v[idx].first.first << ' ' << med << ' ' << cu << '\n';
}
if(l==0)
l=-1;
ans[v[idx].second]=l;
idx++;
}
}
for(int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
//size
Compilation message
index.cpp:1:2: error: invalid preprocessing directive #gcc
1 | #gcc optimize("-O3")
| ^~~
index.cpp: In function 'int main()':
index.cpp:37:6: warning: unused variable 'k' [-Wunused-variable]
37 | int k=0;
| ^