#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,len,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+2;++i){
x=get(n/2+1);
if(T.ask(x-1)==n/2)break;
x=v[x];
T.add(a[x],-sz[x]);
len=sz[x];
sz[x]=n/2-T.ask(a[x]-1);
T.add(a[x],sz[x]);
len-=sz[x];
y=x+sz[x];
// printf("%d %d %d\n",x,y,sz[x]);
while(y<=n&&!p[y]){
p[y]=1;
sz[y]=min(sz[y],len);
len-=sz[y];
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);
// for(int j=1;j<=n;++j){
// sav=get(j);
// printf("%d ",a[v[sav]+(j-T.ask(sav-1))-1]);
// }
// 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);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
30920 KB |
Output is correct |
2 |
Correct |
350 ms |
38296 KB |
Output is correct |
3 |
Correct |
337 ms |
37972 KB |
Output is correct |
4 |
Correct |
319 ms |
36768 KB |
Output is correct |
5 |
Correct |
340 ms |
38032 KB |
Output is correct |
6 |
Correct |
321 ms |
37220 KB |
Output is correct |
7 |
Correct |
337 ms |
37968 KB |
Output is correct |
8 |
Correct |
318 ms |
37200 KB |
Output is correct |
9 |
Correct |
326 ms |
36920 KB |
Output is correct |
10 |
Correct |
321 ms |
37200 KB |
Output is correct |
11 |
Correct |
331 ms |
37552 KB |
Output is correct |
12 |
Correct |
310 ms |
36244 KB |
Output is correct |
13 |
Correct |
333 ms |
36796 KB |
Output is correct |
14 |
Correct |
350 ms |
37612 KB |
Output is correct |
15 |
Correct |
339 ms |
37256 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
273 ms |
36544 KB |
Output is correct |
18 |
Correct |
291 ms |
36256 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
408 ms |
35716 KB |
Output is correct |
2 |
Correct |
384 ms |
49856 KB |
Output is correct |
3 |
Correct |
336 ms |
45248 KB |
Output is correct |
4 |
Correct |
349 ms |
44868 KB |
Output is correct |
5 |
Correct |
355 ms |
45224 KB |
Output is correct |
6 |
Correct |
351 ms |
44824 KB |
Output is correct |
7 |
Correct |
376 ms |
48976 KB |
Output is correct |
8 |
Correct |
377 ms |
48132 KB |
Output is correct |
9 |
Correct |
344 ms |
45772 KB |
Output is correct |
10 |
Correct |
372 ms |
47236 KB |
Output is correct |
11 |
Correct |
320 ms |
43604 KB |
Output is correct |
12 |
Correct |
332 ms |
43952 KB |
Output is correct |
13 |
Correct |
384 ms |
46248 KB |
Output is correct |
14 |
Correct |
318 ms |
44396 KB |
Output is correct |
15 |
Correct |
385 ms |
47816 KB |
Output is correct |
16 |
Correct |
24 ms |
18256 KB |
Output is correct |
17 |
Correct |
246 ms |
45396 KB |
Output is correct |
18 |
Correct |
298 ms |
41556 KB |
Output is correct |
19 |
Correct |
59 ms |
22960 KB |
Output is correct |
20 |
Correct |
89 ms |
24660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
20560 KB |
Output is correct |
2 |
Correct |
65 ms |
21592 KB |
Output is correct |
3 |
Correct |
71 ms |
21436 KB |
Output is correct |
4 |
Correct |
58 ms |
21024 KB |
Output is correct |
5 |
Correct |
58 ms |
21332 KB |
Output is correct |
6 |
Correct |
50 ms |
21040 KB |
Output is correct |
7 |
Correct |
53 ms |
21332 KB |
Output is correct |
8 |
Correct |
53 ms |
21072 KB |
Output is correct |
9 |
Correct |
56 ms |
21040 KB |
Output is correct |
10 |
Correct |
49 ms |
21076 KB |
Output is correct |
11 |
Correct |
49 ms |
21076 KB |
Output is correct |
12 |
Correct |
49 ms |
21072 KB |
Output is correct |
13 |
Correct |
50 ms |
21076 KB |
Output is correct |
14 |
Correct |
48 ms |
21072 KB |
Output is correct |
15 |
Correct |
48 ms |
21072 KB |
Output is correct |
16 |
Correct |
10 ms |
17500 KB |
Output is correct |
17 |
Correct |
34 ms |
21332 KB |
Output is correct |
18 |
Correct |
39 ms |
20560 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
30920 KB |
Output is correct |
2 |
Correct |
350 ms |
38296 KB |
Output is correct |
3 |
Correct |
337 ms |
37972 KB |
Output is correct |
4 |
Correct |
319 ms |
36768 KB |
Output is correct |
5 |
Correct |
340 ms |
38032 KB |
Output is correct |
6 |
Correct |
321 ms |
37220 KB |
Output is correct |
7 |
Correct |
337 ms |
37968 KB |
Output is correct |
8 |
Correct |
318 ms |
37200 KB |
Output is correct |
9 |
Correct |
326 ms |
36920 KB |
Output is correct |
10 |
Correct |
321 ms |
37200 KB |
Output is correct |
11 |
Correct |
331 ms |
37552 KB |
Output is correct |
12 |
Correct |
310 ms |
36244 KB |
Output is correct |
13 |
Correct |
333 ms |
36796 KB |
Output is correct |
14 |
Correct |
350 ms |
37612 KB |
Output is correct |
15 |
Correct |
339 ms |
37256 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
273 ms |
36544 KB |
Output is correct |
18 |
Correct |
291 ms |
36256 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
408 ms |
35716 KB |
Output is correct |
22 |
Correct |
384 ms |
49856 KB |
Output is correct |
23 |
Correct |
336 ms |
45248 KB |
Output is correct |
24 |
Correct |
349 ms |
44868 KB |
Output is correct |
25 |
Correct |
355 ms |
45224 KB |
Output is correct |
26 |
Correct |
351 ms |
44824 KB |
Output is correct |
27 |
Correct |
376 ms |
48976 KB |
Output is correct |
28 |
Correct |
377 ms |
48132 KB |
Output is correct |
29 |
Correct |
344 ms |
45772 KB |
Output is correct |
30 |
Correct |
372 ms |
47236 KB |
Output is correct |
31 |
Correct |
320 ms |
43604 KB |
Output is correct |
32 |
Correct |
332 ms |
43952 KB |
Output is correct |
33 |
Correct |
384 ms |
46248 KB |
Output is correct |
34 |
Correct |
318 ms |
44396 KB |
Output is correct |
35 |
Correct |
385 ms |
47816 KB |
Output is correct |
36 |
Correct |
24 ms |
18256 KB |
Output is correct |
37 |
Correct |
246 ms |
45396 KB |
Output is correct |
38 |
Correct |
298 ms |
41556 KB |
Output is correct |
39 |
Correct |
59 ms |
22960 KB |
Output is correct |
40 |
Correct |
89 ms |
24660 KB |
Output is correct |
41 |
Correct |
70 ms |
20560 KB |
Output is correct |
42 |
Correct |
65 ms |
21592 KB |
Output is correct |
43 |
Correct |
71 ms |
21436 KB |
Output is correct |
44 |
Correct |
58 ms |
21024 KB |
Output is correct |
45 |
Correct |
58 ms |
21332 KB |
Output is correct |
46 |
Correct |
50 ms |
21040 KB |
Output is correct |
47 |
Correct |
53 ms |
21332 KB |
Output is correct |
48 |
Correct |
53 ms |
21072 KB |
Output is correct |
49 |
Correct |
56 ms |
21040 KB |
Output is correct |
50 |
Correct |
49 ms |
21076 KB |
Output is correct |
51 |
Correct |
49 ms |
21076 KB |
Output is correct |
52 |
Correct |
49 ms |
21072 KB |
Output is correct |
53 |
Correct |
50 ms |
21076 KB |
Output is correct |
54 |
Correct |
48 ms |
21072 KB |
Output is correct |
55 |
Correct |
48 ms |
21072 KB |
Output is correct |
56 |
Correct |
10 ms |
17500 KB |
Output is correct |
57 |
Correct |
34 ms |
21332 KB |
Output is correct |
58 |
Correct |
39 ms |
20560 KB |
Output is correct |
59 |
Correct |
2 ms |
14684 KB |
Output is correct |
60 |
Correct |
2 ms |
14684 KB |
Output is correct |
61 |
Correct |
559 ms |
49580 KB |
Output is correct |
62 |
Correct |
597 ms |
49584 KB |
Output is correct |
63 |
Correct |
571 ms |
48668 KB |
Output is correct |
64 |
Correct |
492 ms |
48116 KB |
Output is correct |
65 |
Correct |
486 ms |
48732 KB |
Output is correct |
66 |
Correct |
483 ms |
48052 KB |
Output is correct |
67 |
Correct |
455 ms |
48468 KB |
Output is correct |
68 |
Correct |
476 ms |
47652 KB |
Output is correct |
69 |
Correct |
466 ms |
47764 KB |
Output is correct |
70 |
Correct |
476 ms |
47284 KB |
Output is correct |
71 |
Correct |
435 ms |
46420 KB |
Output is correct |
72 |
Correct |
461 ms |
46992 KB |
Output is correct |
73 |
Correct |
464 ms |
46644 KB |
Output is correct |
74 |
Correct |
445 ms |
47584 KB |
Output is correct |
75 |
Correct |
471 ms |
47440 KB |
Output is correct |
76 |
Correct |
20 ms |
18220 KB |
Output is correct |
77 |
Correct |
304 ms |
45392 KB |
Output is correct |
78 |
Correct |
363 ms |
44444 KB |
Output is correct |
79 |
Correct |
2 ms |
14680 KB |
Output is correct |
80 |
Correct |
2 ms |
14784 KB |
Output is correct |