#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=2e5;
int tab[N+10];
int srt[N+10];
int l[N+10],r[N+10];
int bg[N+10],en[N+10];
int fen[N+10];
int lsb(int x) { return x&(-x); }
void add(int x,int n)
{
for(;x<=n;x+=lsb(x))
fen[x]++;
return;
}
int read(int x)
{
int ans=0;
for(;x>0;x-=lsb(x))
ans+=fen[x];
return ans;
}
int read(int ll,int rr)
{
return read(rr)-read(ll-1);
}
void bs(int n,int q)
{
int active=q;
vector<pair<int,int>> mid;
while(active>0)
{
for(int i=1;i<=n;i++)
{
if(bg[i]<en[i])
mid.emplace_back((bg[i]+en[i]+1)/2,i);
}
sort(mid.begin(),mid.end());
for(int i=0;i<=n;i++)
fen[i]=0;
for(int i=1;i<=n;i++)
{
while(!mid.empty() && mid.back().fi>tab[srt[i]])
{
auto [m,x]=mid.back();
mid.pop_back();
if(read(l[x],r[x])>=m)
bg[x]=m;
else
en[x]=m-1;
if(bg[x]>=en[x])
active--;
}
add(srt[i],n);
}
while(!mid.empty())
{
auto [m,x]=mid.back();
mid.pop_back();
bg[x]=m;
if(bg[x]>=en[x])
active--;
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>tab[i];
srt[i]=i;
}
sort(srt+1,srt+n+1,[](int a,int b){ return tab[a]>tab[b]; });
for(int i=1;i<=q;i++)
{
cin>>l[i]>>r[i];
bg[i]=0;
en[i]=n;
}
bs(n,q);
for(int i=1;i<=q;i++)
cout<<bg[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
147 ms |
3004 KB |
Output is correct |
12 |
Correct |
140 ms |
2936 KB |
Output is correct |
13 |
Correct |
152 ms |
3020 KB |
Output is correct |
14 |
Correct |
141 ms |
3036 KB |
Output is correct |
15 |
Correct |
140 ms |
3020 KB |
Output is correct |
16 |
Correct |
151 ms |
3000 KB |
Output is correct |
17 |
Correct |
142 ms |
3016 KB |
Output is correct |
18 |
Correct |
142 ms |
3052 KB |
Output is correct |
19 |
Correct |
149 ms |
2980 KB |
Output is correct |
20 |
Correct |
148 ms |
2980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
147 ms |
3004 KB |
Output is correct |
12 |
Correct |
140 ms |
2936 KB |
Output is correct |
13 |
Correct |
152 ms |
3020 KB |
Output is correct |
14 |
Correct |
141 ms |
3036 KB |
Output is correct |
15 |
Correct |
140 ms |
3020 KB |
Output is correct |
16 |
Correct |
151 ms |
3000 KB |
Output is correct |
17 |
Correct |
142 ms |
3016 KB |
Output is correct |
18 |
Correct |
142 ms |
3052 KB |
Output is correct |
19 |
Correct |
149 ms |
2980 KB |
Output is correct |
20 |
Correct |
148 ms |
2980 KB |
Output is correct |
21 |
Correct |
712 ms |
11128 KB |
Output is correct |
22 |
Correct |
708 ms |
11068 KB |
Output is correct |
23 |
Correct |
773 ms |
11152 KB |
Output is correct |
24 |
Correct |
715 ms |
11120 KB |
Output is correct |
25 |
Correct |
810 ms |
11068 KB |
Output is correct |
26 |
Correct |
697 ms |
11136 KB |
Output is correct |
27 |
Correct |
814 ms |
11064 KB |
Output is correct |
28 |
Correct |
734 ms |
11068 KB |
Output is correct |
29 |
Correct |
709 ms |
11060 KB |
Output is correct |
30 |
Correct |
769 ms |
11204 KB |
Output is correct |