#include<bits/stdc++.h>
#define ll long long
#define fs first
#define sn second
#define N 1001000
using namespace std;
int n,m,x,y,z,now,sav,v[N],a[N],nx[N],sz[N],ans[N],p[N];
tuple<int,int,int>q[N];
stack<int>st;
struct Tree
{
int c[N];
void add(int x,int y)
{
for(;x<=n;x+=x&-x)
c[x]+=y;
return;
}
int ask(int x)
{
int sum=0;
for(;x;x-=x&-x)
sum+=c[x];
return sum;
}
}T;
int get(int x)
{
int l,r;
l=1;
r=n;
while(l<r){
int mid=l+r>>1;
if(T.ask(mid)>=x)r=mid;
else l=mid+1;
}
return l;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
v[a[i]]=i;
}
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
q[i]=tie(x,y,i);
}
sort(q+1,q+1+m);
now=1;
while(now<=m&&get<0>(q[now])==0){
tie(x,y,z)=q[now];
ans[z]=a[y];
now++;
}
st.push(n+1);
a[n+1]=2000000000;
for(int i=n;i>0;--i){
while(a[st.top()]<a[i])st.pop();
nx[i]=st.top();
sz[i]=nx[i]-i;
st.push(i);
}
while(st.top()!=n+1){
x=st.top();
st.pop();
p[x]=1;
T.add(a[x],sz[x]);
}
for(int i=1;i<=n;++i){
x=get(n/2+1);
if(T.ask(x-1)==n/2)break;
x=v[x];
T.add(a[x],-sz[x]);
sz[x]=n/2-T.ask(a[x]-1);
T.add(a[x],sz[x]);
y=x+sz[x];
// printf("%d %d %d\n",x,y,sz[x]);
while(y<=n&&!p[y]){
p[y]=1;
T.add(a[y],sz[y]);
y=nx[y];
}
// for(int i=1;i<=n;++i)
// printf("%d ",sz[i]);
// putchar(10);
// for(int i=1;i<=n;++i)
// printf("%d ",T.ask(i));
// putchar(10);
while(now<=m&&get<0>(q[now])==i){
tie(x,y,z)=q[now];
sav=get(y);
ans[z]=a[v[sav]+(y-T.ask(sav-1))-1];
now++;
}
}
while(now<=m){
tie(x,y,z)=q[now];
sav=get(y);
ans[z]=a[v[sav]+(y-T.ask(sav-1))-1];
now++;
}
for(int i=1;i<=m;++i)
printf("%d\n",ans[i]);
return 0;
}
Compilation message
Main.cpp: In function 'int get(int)':
Main.cpp:33:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid=l+r>>1;
| ~^~
Main.cpp: In function 'int main()':
Main.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
Main.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
347 ms |
30684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
405 ms |
34412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
19544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
347 ms |
30684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |