#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
const int NMAX=2e5+5,MM=1e6+5;
int ar[4*NMAX+505],n,q,val[NMAX+5],a[NMAX+5],inv[NMAX+5],ap[NMAX+5],urm[NMAX+5];
pair<pair<int,int>,int> t[MM];
int ras[MM];
pair<int,int> add(pair<int,int> a,pair<int,int> b)
{
return {a.first+b.first,a.second+b.second};
}
void update(int st,int dr,int nod,int poz,int val)
{
if (st==dr)
{
ar[nod]=val;
return ;
}
int mij=(st+dr)/2;
if (poz<=mij) update(st,mij,nod*2,poz,val);
else update(mij+1,dr,nod*2+1,poz,val);
ar[nod]=ar[nod*2]+ar[nod*2+1];
}
pair<int,int> query(int st,int dr,int nod,int val)
{
if (st==dr)
{
return {st,ar[nod]};
}
int mij=(st+dr)/2;
if (ar[nod*2]<val) return add(query(mij+1,dr,nod*2+1,val-ar[nod*2]),{0,ar[nod*2]});
else return query(st,mij,nod*2,val);
}
void operation()
{
int cen=0,allval=0;
{
pair<int,int> nod=query(1,n,1,n/2+1);
int x=n/2+1-(nod.second-val[nod.first]);
if (x==1) return ;
allval=val[nod.first]-(x-1);
val[nod.first]=x-1;
update(1,n,1,nod.first,val[nod.first]);
cen=a[inv[nod.first]+x-1];
}
int nod=cen;
while (ap[nod]==0)
{
ap[nod]=1;
val[nod]=min(allval,inv[urm[nod]]-inv[nod]);
update(1,n,1,nod,val[nod]);
allval-=val[nod];
nod=urm[nod];
}
}
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for (int i=1;i<=n;++i)
{
cin>>a[i];
inv[a[i]]=i;
}
inv[n+1]=n+1;
stack <int> s;
for (int i=1;i<=n;++i)
{
while (!s.empty()&&a[s.top()]<a[i]) {urm[a[s.top()]]=a[i]; s.pop();}
s.push(i);
}
while (!s.empty()) {urm[a[s.top()]]=n+1; s.pop();}
for (int i=1;i<=q;++i)
{
cin>>t[i].first.first>>t[i].first.second;
t[i].second=i;
}
sort(t+1,t+q+1);
{
ap[n+1]=1;
int nod=a[1];
while (nod!=n+1)
{
ap[nod]=1;
val[nod]=inv[urm[nod]]-inv[nod];
update(1,n,1,nod,inv[urm[nod]]-inv[nod]);
nod=urm[nod];
}
}
int cat=0;
for (int i=1;i<=q;++i)
{
while (cat<t[i].first.first&&cat<=n+5) {cat++; operation();}
{
auto nod=query(1,n,1,t[i].first.second);
int x=t[i].first.second-(nod.second-val[nod.first]);
ras[t[i].second]=a[inv[nod.first]+x-1];
}
}
for (int i=1;i<=q;++i)
cout<<ras[i]<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
29008 KB |
Output is correct |
2 |
Correct |
262 ms |
32340 KB |
Output is correct |
3 |
Correct |
283 ms |
32080 KB |
Output is correct |
4 |
Correct |
247 ms |
31184 KB |
Output is correct |
5 |
Correct |
260 ms |
32084 KB |
Output is correct |
6 |
Correct |
289 ms |
31312 KB |
Output is correct |
7 |
Correct |
263 ms |
32076 KB |
Output is correct |
8 |
Correct |
267 ms |
31312 KB |
Output is correct |
9 |
Correct |
243 ms |
30920 KB |
Output is correct |
10 |
Correct |
251 ms |
31304 KB |
Output is correct |
11 |
Correct |
262 ms |
31316 KB |
Output is correct |
12 |
Correct |
247 ms |
30292 KB |
Output is correct |
13 |
Correct |
251 ms |
31084 KB |
Output is correct |
14 |
Correct |
264 ms |
31688 KB |
Output is correct |
15 |
Correct |
266 ms |
31316 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
240 ms |
30420 KB |
Output is correct |
18 |
Correct |
295 ms |
30292 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
36020 KB |
Output is correct |
2 |
Correct |
337 ms |
42160 KB |
Output is correct |
3 |
Correct |
236 ms |
37728 KB |
Output is correct |
4 |
Correct |
242 ms |
37716 KB |
Output is correct |
5 |
Correct |
267 ms |
38228 KB |
Output is correct |
6 |
Correct |
230 ms |
37460 KB |
Output is correct |
7 |
Correct |
269 ms |
41676 KB |
Output is correct |
8 |
Correct |
254 ms |
40920 KB |
Output is correct |
9 |
Correct |
245 ms |
38232 KB |
Output is correct |
10 |
Correct |
250 ms |
39876 KB |
Output is correct |
11 |
Correct |
219 ms |
36180 KB |
Output is correct |
12 |
Correct |
223 ms |
36560 KB |
Output is correct |
13 |
Correct |
287 ms |
38996 KB |
Output is correct |
14 |
Correct |
228 ms |
37200 KB |
Output is correct |
15 |
Correct |
262 ms |
40276 KB |
Output is correct |
16 |
Correct |
14 ms |
10076 KB |
Output is correct |
17 |
Correct |
246 ms |
37180 KB |
Output is correct |
18 |
Correct |
206 ms |
34288 KB |
Output is correct |
19 |
Correct |
54 ms |
16980 KB |
Output is correct |
20 |
Correct |
70 ms |
19540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
14676 KB |
Output is correct |
2 |
Correct |
57 ms |
15188 KB |
Output is correct |
3 |
Correct |
52 ms |
15188 KB |
Output is correct |
4 |
Correct |
43 ms |
14932 KB |
Output is correct |
5 |
Correct |
65 ms |
15152 KB |
Output is correct |
6 |
Correct |
42 ms |
14932 KB |
Output is correct |
7 |
Correct |
47 ms |
15188 KB |
Output is correct |
8 |
Correct |
45 ms |
14936 KB |
Output is correct |
9 |
Correct |
49 ms |
15188 KB |
Output is correct |
10 |
Correct |
39 ms |
14928 KB |
Output is correct |
11 |
Correct |
44 ms |
14808 KB |
Output is correct |
12 |
Correct |
38 ms |
14932 KB |
Output is correct |
13 |
Correct |
54 ms |
14932 KB |
Output is correct |
14 |
Correct |
40 ms |
14932 KB |
Output is correct |
15 |
Correct |
39 ms |
14932 KB |
Output is correct |
16 |
Correct |
7 ms |
9052 KB |
Output is correct |
17 |
Correct |
40 ms |
14768 KB |
Output is correct |
18 |
Correct |
36 ms |
15192 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
29008 KB |
Output is correct |
2 |
Correct |
262 ms |
32340 KB |
Output is correct |
3 |
Correct |
283 ms |
32080 KB |
Output is correct |
4 |
Correct |
247 ms |
31184 KB |
Output is correct |
5 |
Correct |
260 ms |
32084 KB |
Output is correct |
6 |
Correct |
289 ms |
31312 KB |
Output is correct |
7 |
Correct |
263 ms |
32076 KB |
Output is correct |
8 |
Correct |
267 ms |
31312 KB |
Output is correct |
9 |
Correct |
243 ms |
30920 KB |
Output is correct |
10 |
Correct |
251 ms |
31304 KB |
Output is correct |
11 |
Correct |
262 ms |
31316 KB |
Output is correct |
12 |
Correct |
247 ms |
30292 KB |
Output is correct |
13 |
Correct |
251 ms |
31084 KB |
Output is correct |
14 |
Correct |
264 ms |
31688 KB |
Output is correct |
15 |
Correct |
266 ms |
31316 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
240 ms |
30420 KB |
Output is correct |
18 |
Correct |
295 ms |
30292 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
295 ms |
36020 KB |
Output is correct |
22 |
Correct |
337 ms |
42160 KB |
Output is correct |
23 |
Correct |
236 ms |
37728 KB |
Output is correct |
24 |
Correct |
242 ms |
37716 KB |
Output is correct |
25 |
Correct |
267 ms |
38228 KB |
Output is correct |
26 |
Correct |
230 ms |
37460 KB |
Output is correct |
27 |
Correct |
269 ms |
41676 KB |
Output is correct |
28 |
Correct |
254 ms |
40920 KB |
Output is correct |
29 |
Correct |
245 ms |
38232 KB |
Output is correct |
30 |
Correct |
250 ms |
39876 KB |
Output is correct |
31 |
Correct |
219 ms |
36180 KB |
Output is correct |
32 |
Correct |
223 ms |
36560 KB |
Output is correct |
33 |
Correct |
287 ms |
38996 KB |
Output is correct |
34 |
Correct |
228 ms |
37200 KB |
Output is correct |
35 |
Correct |
262 ms |
40276 KB |
Output is correct |
36 |
Correct |
14 ms |
10076 KB |
Output is correct |
37 |
Correct |
246 ms |
37180 KB |
Output is correct |
38 |
Correct |
206 ms |
34288 KB |
Output is correct |
39 |
Correct |
54 ms |
16980 KB |
Output is correct |
40 |
Correct |
70 ms |
19540 KB |
Output is correct |
41 |
Correct |
54 ms |
14676 KB |
Output is correct |
42 |
Correct |
57 ms |
15188 KB |
Output is correct |
43 |
Correct |
52 ms |
15188 KB |
Output is correct |
44 |
Correct |
43 ms |
14932 KB |
Output is correct |
45 |
Correct |
65 ms |
15152 KB |
Output is correct |
46 |
Correct |
42 ms |
14932 KB |
Output is correct |
47 |
Correct |
47 ms |
15188 KB |
Output is correct |
48 |
Correct |
45 ms |
14936 KB |
Output is correct |
49 |
Correct |
49 ms |
15188 KB |
Output is correct |
50 |
Correct |
39 ms |
14928 KB |
Output is correct |
51 |
Correct |
44 ms |
14808 KB |
Output is correct |
52 |
Correct |
38 ms |
14932 KB |
Output is correct |
53 |
Correct |
54 ms |
14932 KB |
Output is correct |
54 |
Correct |
40 ms |
14932 KB |
Output is correct |
55 |
Correct |
39 ms |
14932 KB |
Output is correct |
56 |
Correct |
7 ms |
9052 KB |
Output is correct |
57 |
Correct |
40 ms |
14768 KB |
Output is correct |
58 |
Correct |
36 ms |
15192 KB |
Output is correct |
59 |
Correct |
1 ms |
8540 KB |
Output is correct |
60 |
Correct |
1 ms |
8540 KB |
Output is correct |
61 |
Correct |
390 ms |
42940 KB |
Output is correct |
62 |
Correct |
408 ms |
41888 KB |
Output is correct |
63 |
Correct |
394 ms |
41300 KB |
Output is correct |
64 |
Correct |
349 ms |
40780 KB |
Output is correct |
65 |
Correct |
356 ms |
41552 KB |
Output is correct |
66 |
Correct |
398 ms |
40788 KB |
Output is correct |
67 |
Correct |
344 ms |
41300 KB |
Output is correct |
68 |
Correct |
345 ms |
40532 KB |
Output is correct |
69 |
Correct |
352 ms |
40532 KB |
Output is correct |
70 |
Correct |
371 ms |
40016 KB |
Output is correct |
71 |
Correct |
318 ms |
38996 KB |
Output is correct |
72 |
Correct |
320 ms |
39764 KB |
Output is correct |
73 |
Correct |
388 ms |
39508 KB |
Output is correct |
74 |
Correct |
331 ms |
40276 KB |
Output is correct |
75 |
Correct |
332 ms |
40276 KB |
Output is correct |
76 |
Correct |
14 ms |
10076 KB |
Output is correct |
77 |
Correct |
301 ms |
37444 KB |
Output is correct |
78 |
Correct |
297 ms |
37104 KB |
Output is correct |
79 |
Correct |
1 ms |
8540 KB |
Output is correct |
80 |
Correct |
1 ms |
8540 KB |
Output is correct |