#include<stdio.h>
#include<stdlib.h>
#define N 200004
int n,a[N],t[N],q,sz[N],at[N],*eh[N],eo[N],zz[1000005];
void upd(int p,int k) { for(;p<N;p|=p+1)t[p]+=k; }
int qry(int p) {int z=0;for(;p>0;p&=p-1)z+=t[p-1];return z;}
void add(int p,int k) { sz[p]=k; upd(p,k); }
void rem(int p) { upd(p,-sz[p]); }
int grp(int x)
{
if(x<=0)return -1;
int p=0,v=0;
for(int j=1<<19;j>0;j>>=1) if(p+j<=N&&v+t[p+j-1]<x)p+=j,v+=t[p-1];
return p;
}
void append(int i,int j)
{
int o=eo[i]++;
if(o>=2&&(o&o-1)==0)eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
eh[i][o]=j;
}
void shuffle()
{
static int done=0;
if(done)return;
int g1=grp(n/2),g2=grp(n/2+1);
if(g1!=g2){done=1;return;}
int rl=qry(g1);
int st=at[g1];
int md=n/2-rl+at[g1];
int ed=at[g1]+sz[g1];
for(int l=md,j=md;j<=ed;++j) if(j==ed||a[j]>a[l])add(a[l],j-l),l=j;
rem(g1);
add(g1,n/2-rl);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)scanf("%d",a+i),at[a[i]]=i;
for(int l=1,i=1;i<=n+1;++i) if(i==n+1||a[i]>a[l]) add(a[l],i-l),l=i;
for(int i=0;i<=n+1;++i)eh[i]=(int*)realloc(0,2*sizeof**eh);
for(int t,p,i=0;i<q;++i)
{
scanf("%d%d",&t,&p);
if(t>n+1)t=n+1;
append(t,i),append(t,p);
}
for(int i=0;i<=n+1;++i)
{
for(int j=0;j<eo[i];j+=2)
{
int gg=grp(eh[i][j+1]);
int idx=eh[i][j+1]-qry(gg);
zz[eh[i][j]]=a[at[gg]+idx-1];
}
shuffle();
}
for(int i=0;i<q;++i)printf("%d\n",zz[i]);
return 0;
}
Compilation message
Main.cpp: In function 'void append(int, int)':
Main.cpp:24:18: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
24 | if(o>=2&&(o&o-1)==0)eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
| ~^~
Main.cpp: In function 'void shuffle()':
Main.cpp:35:9: warning: unused variable 'st' [-Wunused-variable]
35 | int st=at[g1];
| ^~
Main.cpp: In function 'int main()':
Main.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:46:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | for(int i=1;i<=n;++i)scanf("%d",a+i),at[a[i]]=i;
| ~~~~~^~~~~~~~~~
Main.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d%d",&t,&p);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
24168 KB |
Output is correct |
2 |
Correct |
276 ms |
22864 KB |
Output is correct |
3 |
Correct |
260 ms |
22368 KB |
Output is correct |
4 |
Correct |
247 ms |
22224 KB |
Output is correct |
5 |
Correct |
264 ms |
24404 KB |
Output is correct |
6 |
Correct |
251 ms |
25168 KB |
Output is correct |
7 |
Correct |
271 ms |
25680 KB |
Output is correct |
8 |
Correct |
258 ms |
23784 KB |
Output is correct |
9 |
Correct |
251 ms |
22956 KB |
Output is correct |
10 |
Correct |
257 ms |
22388 KB |
Output is correct |
11 |
Correct |
257 ms |
22488 KB |
Output is correct |
12 |
Correct |
256 ms |
21204 KB |
Output is correct |
13 |
Correct |
261 ms |
21964 KB |
Output is correct |
14 |
Correct |
280 ms |
24156 KB |
Output is correct |
15 |
Correct |
272 ms |
22400 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
273 ms |
21064 KB |
Output is correct |
18 |
Correct |
242 ms |
20976 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3041 ms |
20564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
885 ms |
10960 KB |
Output is correct |
2 |
Correct |
67 ms |
11860 KB |
Output is correct |
3 |
Correct |
796 ms |
11156 KB |
Output is correct |
4 |
Correct |
48 ms |
11472 KB |
Output is correct |
5 |
Correct |
52 ms |
11348 KB |
Output is correct |
6 |
Correct |
44 ms |
11400 KB |
Output is correct |
7 |
Correct |
52 ms |
11344 KB |
Output is correct |
8 |
Correct |
45 ms |
11432 KB |
Output is correct |
9 |
Correct |
48 ms |
11344 KB |
Output is correct |
10 |
Correct |
38 ms |
11604 KB |
Output is correct |
11 |
Correct |
40 ms |
11856 KB |
Output is correct |
12 |
Correct |
40 ms |
11692 KB |
Output is correct |
13 |
Correct |
41 ms |
11628 KB |
Output is correct |
14 |
Correct |
40 ms |
11812 KB |
Output is correct |
15 |
Correct |
38 ms |
11600 KB |
Output is correct |
16 |
Correct |
10 ms |
9564 KB |
Output is correct |
17 |
Correct |
42 ms |
11176 KB |
Output is correct |
18 |
Correct |
40 ms |
11220 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
24168 KB |
Output is correct |
2 |
Correct |
276 ms |
22864 KB |
Output is correct |
3 |
Correct |
260 ms |
22368 KB |
Output is correct |
4 |
Correct |
247 ms |
22224 KB |
Output is correct |
5 |
Correct |
264 ms |
24404 KB |
Output is correct |
6 |
Correct |
251 ms |
25168 KB |
Output is correct |
7 |
Correct |
271 ms |
25680 KB |
Output is correct |
8 |
Correct |
258 ms |
23784 KB |
Output is correct |
9 |
Correct |
251 ms |
22956 KB |
Output is correct |
10 |
Correct |
257 ms |
22388 KB |
Output is correct |
11 |
Correct |
257 ms |
22488 KB |
Output is correct |
12 |
Correct |
256 ms |
21204 KB |
Output is correct |
13 |
Correct |
261 ms |
21964 KB |
Output is correct |
14 |
Correct |
280 ms |
24156 KB |
Output is correct |
15 |
Correct |
272 ms |
22400 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
273 ms |
21064 KB |
Output is correct |
18 |
Correct |
242 ms |
20976 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
21 |
Execution timed out |
3041 ms |
20564 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |