#include <bits/stdc++.h>
#define ll long long
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ull unsigned long long
#define ld long double
#define lv v+v
#define rv v+v+1
#define files freopen("expert.in", "r", stdin), freopen("expert.out", "w", stdout)
using namespace std;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
const ll P = 337ll;
const ld EPS = 1e-9;
const ll block = 450;
ll n,qq,a[N],ans[N], t[200000 + 10], MAXNUM=0, MINNUM = LLONG_MAX,cur = 0,l=0,r=0;
struct ask {
ll L, R, idx;
};
ask q[N];
bool cmp(ask x, ask y) {
if(x.L / block == y.L / block){return x.R < y.R;}
else {return x.L < y.L;}
}
ll sum(ll k) {
ll s = 0;
while (k >= MINNUM) {
s += t[k];
k -= k&-k;
}
return s;
}
void add(ll k, ll val) {
if(k<1) {
return;
}
while (k <= MAXNUM) {
t[k] += val;
k += k&-k;
}
}
bool check(ll t) {
ll num = sum(MAXNUM) - sum(t-1);
return num>=t;
}
void del(ll i) {
add(a[i],-1);
}
void inc(ll i) {
add(a[i],1);
}
ll answer() {
ll l = 1, r = N;
while(l + 1 < r) {
ll mid = l + r >> 1LL;
if(check(mid)) {
l = mid;
}
else {
r = mid;
}
}
return l;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>qq;
for(ll i = 1; i<=n; i++) {
cin>>a[i];
MAXNUM = max(MAXNUM, a[i]);
MINNUM = min(MINNUM, a[i]);
}
for(ll i = 1; i<=qq; i++) {
ll l,r;
cin>>l>>r;
q[i].L = l;
q[i].R = r;
q[i].idx = i;
}
sort(q+1, q+1+qq, cmp);
for(ll i = 1; i<=qq;i++) {
while(l < q[i].L){
del(l++);
}
while(l > q[i].L){
inc(--l);
}
while(r < q[i].R){
inc(++r);
}
while(r > q[i].R){
del(r--);
}
ans[q[i].idx] = answer();
}
for(ll i = 1; i<=qq; i++) {
cout<<ans[i]<<"\n";
}
return 0;
}
// equal, min, max, 1, random, build
/*
*/
Compilation message
index.cpp: In function 'long long int answer()':
index.cpp:58:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | ll mid = l + r >> 1LL;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
124 ms |
5460 KB |
Output is correct |
12 |
Correct |
125 ms |
5436 KB |
Output is correct |
13 |
Correct |
125 ms |
5444 KB |
Output is correct |
14 |
Correct |
123 ms |
5456 KB |
Output is correct |
15 |
Correct |
130 ms |
5244 KB |
Output is correct |
16 |
Correct |
127 ms |
5720 KB |
Output is correct |
17 |
Correct |
119 ms |
5456 KB |
Output is correct |
18 |
Correct |
121 ms |
5452 KB |
Output is correct |
19 |
Correct |
120 ms |
5456 KB |
Output is correct |
20 |
Correct |
126 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
124 ms |
5460 KB |
Output is correct |
12 |
Correct |
125 ms |
5436 KB |
Output is correct |
13 |
Correct |
125 ms |
5444 KB |
Output is correct |
14 |
Correct |
123 ms |
5456 KB |
Output is correct |
15 |
Correct |
130 ms |
5244 KB |
Output is correct |
16 |
Correct |
127 ms |
5720 KB |
Output is correct |
17 |
Correct |
119 ms |
5456 KB |
Output is correct |
18 |
Correct |
121 ms |
5452 KB |
Output is correct |
19 |
Correct |
120 ms |
5456 KB |
Output is correct |
20 |
Correct |
126 ms |
5456 KB |
Output is correct |
21 |
Correct |
871 ms |
10848 KB |
Output is correct |
22 |
Correct |
891 ms |
10856 KB |
Output is correct |
23 |
Correct |
905 ms |
11016 KB |
Output is correct |
24 |
Correct |
897 ms |
10848 KB |
Output is correct |
25 |
Correct |
856 ms |
10868 KB |
Output is correct |
26 |
Correct |
883 ms |
11088 KB |
Output is correct |
27 |
Correct |
858 ms |
10848 KB |
Output is correct |
28 |
Correct |
908 ms |
11020 KB |
Output is correct |
29 |
Correct |
878 ms |
11088 KB |
Output is correct |
30 |
Correct |
847 ms |
10876 KB |
Output is correct |