#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,ii> i4;
const int MOD=1000000007;
const int INF=1012345678;
const ll LLINF=1012345678012345678LL;
const double PI=3.1415926536;
const double EPS=1e-14;
int n,q;
int arr[200005];
struct node{
int s,e,m;
vector<int> v;
node *l,*r;
node(int S,int E){
s=S;e=E;m=(s+e)/2;
if(s==e){
v.pb(arr[s]);
}else{
l=new node(s,m);
r=new node(m+1,e);
int p1=0,p2=0;
for(int i=s;i<=e;i++){
if(p1==SZ(l->v)){
v.pb((r->v)[p2++]);
}else if(p2==SZ(r->v)){
v.pb((l->v)[p1++]);
}else{
if((l->v)[p1]<(r->v)[p2]){
v.pb((l->v)[p1++]);
}else{
v.pb((r->v)[p2++]);
}
}
}
}
}
int qry(int x,int y,int val){
if(s>=x&&e<=y){
int b=lower_bound(ALL(v),val)-v.begin();
return SZ(v)-b;
}
if(y<=m)return l->qry(x,y,val);
if(x>m)return r->qry(x,y,val);
return l->qry(x,m,val)+r->qry(m+1,y,val);
}
}*root;
int main(){
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++)scanf("%d",&arr[i]);
root=new node(0,n-1);
int l,r;
while(q--){
scanf("%d%d",&l,&r);l--;r--;
int mini=0,maxi=r-l+1;
while(mini<maxi){
int mid=(mini+maxi+1)/2;
if(root->qry(l,r,mid)>=mid){ // number of papers from l to r with >=mid citations
mini=mid;
}else{
maxi=mid-1;
}
}
printf("%d\n",mini);
}
}
Compilation message
index.cpp: In function 'int main()':
index.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d%d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
index.cpp:62:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | for(int i=0;i<n;i++)scanf("%d",&arr[i]);
| ~~~~~^~~~~~~~~~~~~~
index.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d",&l,&r);l--;r--;
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
646 ms |
14320 KB |
Output is correct |
12 |
Correct |
611 ms |
14164 KB |
Output is correct |
13 |
Correct |
647 ms |
14312 KB |
Output is correct |
14 |
Correct |
642 ms |
14176 KB |
Output is correct |
15 |
Correct |
641 ms |
14268 KB |
Output is correct |
16 |
Correct |
643 ms |
14364 KB |
Output is correct |
17 |
Correct |
636 ms |
14256 KB |
Output is correct |
18 |
Correct |
645 ms |
14340 KB |
Output is correct |
19 |
Correct |
658 ms |
14420 KB |
Output is correct |
20 |
Correct |
624 ms |
14292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
646 ms |
14320 KB |
Output is correct |
12 |
Correct |
611 ms |
14164 KB |
Output is correct |
13 |
Correct |
647 ms |
14312 KB |
Output is correct |
14 |
Correct |
642 ms |
14176 KB |
Output is correct |
15 |
Correct |
641 ms |
14268 KB |
Output is correct |
16 |
Correct |
643 ms |
14364 KB |
Output is correct |
17 |
Correct |
636 ms |
14256 KB |
Output is correct |
18 |
Correct |
645 ms |
14340 KB |
Output is correct |
19 |
Correct |
658 ms |
14420 KB |
Output is correct |
20 |
Correct |
624 ms |
14292 KB |
Output is correct |
21 |
Execution timed out |
2569 ms |
57180 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |