#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 <= 201)
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 = 200;
while(l < r)
{
int med=(l+r)/2+1;
int cu=0;
x=p+med;
y=p+200;
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: In function 'int main()':
index.cpp:36:6: warning: unused variable 'k' [-Wunused-variable]
36 | int k=0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |