#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll MAXN = 2e5+100;
ll n,q;
pair <pll,ll> all[1'000'100];
ll ans[1'000'100];
ll p[MAXN],pos[MAXN];
ll nxt[MAXN];
ll tree[MAXN*4];
void update(ll id,ll l,ll r,ll i,ll v){
if (l > i || r < i)return;
if (l == r){
tree[id]+=v;
return;
}
ll mid = (l + r) >> 1;
update(id<<1,l,mid,i,v);
update(id<<1|1,mid+1,r,i,v);
tree[id] = tree[id<<1] + tree[id<<1|1];
}
pair <ll,pll> get(ll id,ll l,ll r,ll k){
if (l == r)return MP(l,MP(k,tree[id]));
ll mid = (l + r) >> 1;
if (tree[id<<1] >= k)return get(id<<1,l,mid,k);
else return get(id<<1|1,mid+1,r,k-tree[id<<1]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin>>n>>q;
for (ll i = 1;i <= n;i ++){cin>>p[i];pos[p[i]] = i;}
deque <ll> max1;
for (ll i = n;i >= 1;i --){
while (!max1.empty() && p[max1.back()] < p[i])max1.pop_back();
if (max1.empty())nxt[i] = n+1;
else nxt[i] = max1.back();
max1.push_back(i);
}
set <pll> L,R;
{
ll cur = 1;
while (cur <= n){
pll x = MP(p[cur],nxt[cur]-cur);
update(1,1,n,x.fi,x.se);
cur = nxt[cur];
}
}
for (ll i = 1;i <= q;i ++){
cin>>all[i].fi.fi>>all[i].fi.se;
all[i].fi.fi = min(all[i].fi.fi,n);
all[i].se = i;
}
sort(all+1,all+1+q);
for (ll i = 0,ptr = 1;i <= n;i ++){
while (ptr<=q&&all[ptr].fi.fi == i){
pair <ll,pll> tmp = get(1,1,n,all[ptr].fi.se);
// cout<<ptr<<' '<<tmp.fi<<' '<<tmp.se.fi<<' '<<tmp.se.se<<' '<<p[pos[tmp.fi] + tmp.se.fi - 1]<<'\n';
ans[all[ptr].se] = p[pos[tmp.fi] + tmp.se.fi - 1];
ptr++;
}
pair <ll,pll> tmp = get(1,1,n,n/2+1);
// cout<<i<<' '<<tmp.fi<<' '<<tmp.se.fi<<' '<<tmp.se.se<<'\n';
update(1,1,n,tmp.fi,-tmp.se.se+(tmp.se.fi-1));
ll cur = pos[tmp.fi] + tmp.se.fi - 1;
ll r = pos[tmp.fi] + tmp.se.se - 1;
while (cur <= r){
ll nx = min(nxt[cur],r+1);
pll x = MP(p[cur],nx-cur);
update(1,1,n,x.fi,x.se);
cur = nx;
}
// cout<<i<<' '<<ptr<<endl;
}
for (ll i = 1;i <= q;i ++)cout<<ans[i]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
42324 KB |
Output is correct |
2 |
Correct |
210 ms |
46184 KB |
Output is correct |
3 |
Correct |
213 ms |
44940 KB |
Output is correct |
4 |
Correct |
210 ms |
44368 KB |
Output is correct |
5 |
Correct |
203 ms |
45908 KB |
Output is correct |
6 |
Correct |
191 ms |
44560 KB |
Output is correct |
7 |
Correct |
203 ms |
45972 KB |
Output is correct |
8 |
Correct |
192 ms |
44884 KB |
Output is correct |
9 |
Correct |
198 ms |
44308 KB |
Output is correct |
10 |
Correct |
195 ms |
45044 KB |
Output is correct |
11 |
Correct |
228 ms |
44956 KB |
Output is correct |
12 |
Correct |
196 ms |
44084 KB |
Output is correct |
13 |
Correct |
199 ms |
44884 KB |
Output is correct |
14 |
Correct |
198 ms |
45244 KB |
Output is correct |
15 |
Correct |
202 ms |
45376 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
186 ms |
44316 KB |
Output is correct |
18 |
Correct |
196 ms |
44200 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
49180 KB |
Output is correct |
2 |
Correct |
239 ms |
55632 KB |
Output is correct |
3 |
Correct |
217 ms |
47720 KB |
Output is correct |
4 |
Correct |
227 ms |
49168 KB |
Output is correct |
5 |
Correct |
235 ms |
49076 KB |
Output is correct |
6 |
Correct |
233 ms |
46776 KB |
Output is correct |
7 |
Correct |
258 ms |
54412 KB |
Output is correct |
8 |
Correct |
242 ms |
52832 KB |
Output is correct |
9 |
Correct |
238 ms |
48980 KB |
Output is correct |
10 |
Correct |
252 ms |
52412 KB |
Output is correct |
11 |
Correct |
211 ms |
48320 KB |
Output is correct |
12 |
Correct |
207 ms |
46520 KB |
Output is correct |
13 |
Correct |
225 ms |
52236 KB |
Output is correct |
14 |
Correct |
219 ms |
48644 KB |
Output is correct |
15 |
Correct |
265 ms |
53136 KB |
Output is correct |
16 |
Correct |
51 ms |
15704 KB |
Output is correct |
17 |
Correct |
243 ms |
53244 KB |
Output is correct |
18 |
Correct |
207 ms |
43480 KB |
Output is correct |
19 |
Correct |
109 ms |
23696 KB |
Output is correct |
20 |
Correct |
83 ms |
22352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
15440 KB |
Output is correct |
2 |
Correct |
65 ms |
17148 KB |
Output is correct |
3 |
Correct |
51 ms |
16724 KB |
Output is correct |
4 |
Correct |
47 ms |
16396 KB |
Output is correct |
5 |
Correct |
51 ms |
16468 KB |
Output is correct |
6 |
Correct |
61 ms |
16576 KB |
Output is correct |
7 |
Correct |
60 ms |
16468 KB |
Output is correct |
8 |
Correct |
71 ms |
16324 KB |
Output is correct |
9 |
Correct |
50 ms |
16516 KB |
Output is correct |
10 |
Correct |
69 ms |
16464 KB |
Output is correct |
11 |
Correct |
48 ms |
16388 KB |
Output is correct |
12 |
Correct |
64 ms |
16464 KB |
Output is correct |
13 |
Correct |
57 ms |
16468 KB |
Output is correct |
14 |
Correct |
46 ms |
16468 KB |
Output is correct |
15 |
Correct |
53 ms |
16720 KB |
Output is correct |
16 |
Correct |
39 ms |
13400 KB |
Output is correct |
17 |
Correct |
45 ms |
17292 KB |
Output is correct |
18 |
Correct |
41 ms |
16208 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
42324 KB |
Output is correct |
2 |
Correct |
210 ms |
46184 KB |
Output is correct |
3 |
Correct |
213 ms |
44940 KB |
Output is correct |
4 |
Correct |
210 ms |
44368 KB |
Output is correct |
5 |
Correct |
203 ms |
45908 KB |
Output is correct |
6 |
Correct |
191 ms |
44560 KB |
Output is correct |
7 |
Correct |
203 ms |
45972 KB |
Output is correct |
8 |
Correct |
192 ms |
44884 KB |
Output is correct |
9 |
Correct |
198 ms |
44308 KB |
Output is correct |
10 |
Correct |
195 ms |
45044 KB |
Output is correct |
11 |
Correct |
228 ms |
44956 KB |
Output is correct |
12 |
Correct |
196 ms |
44084 KB |
Output is correct |
13 |
Correct |
199 ms |
44884 KB |
Output is correct |
14 |
Correct |
198 ms |
45244 KB |
Output is correct |
15 |
Correct |
202 ms |
45376 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
186 ms |
44316 KB |
Output is correct |
18 |
Correct |
196 ms |
44200 KB |
Output is correct |
19 |
Correct |
2 ms |
10584 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
231 ms |
49180 KB |
Output is correct |
22 |
Correct |
239 ms |
55632 KB |
Output is correct |
23 |
Correct |
217 ms |
47720 KB |
Output is correct |
24 |
Correct |
227 ms |
49168 KB |
Output is correct |
25 |
Correct |
235 ms |
49076 KB |
Output is correct |
26 |
Correct |
233 ms |
46776 KB |
Output is correct |
27 |
Correct |
258 ms |
54412 KB |
Output is correct |
28 |
Correct |
242 ms |
52832 KB |
Output is correct |
29 |
Correct |
238 ms |
48980 KB |
Output is correct |
30 |
Correct |
252 ms |
52412 KB |
Output is correct |
31 |
Correct |
211 ms |
48320 KB |
Output is correct |
32 |
Correct |
207 ms |
46520 KB |
Output is correct |
33 |
Correct |
225 ms |
52236 KB |
Output is correct |
34 |
Correct |
219 ms |
48644 KB |
Output is correct |
35 |
Correct |
265 ms |
53136 KB |
Output is correct |
36 |
Correct |
51 ms |
15704 KB |
Output is correct |
37 |
Correct |
243 ms |
53244 KB |
Output is correct |
38 |
Correct |
207 ms |
43480 KB |
Output is correct |
39 |
Correct |
109 ms |
23696 KB |
Output is correct |
40 |
Correct |
83 ms |
22352 KB |
Output is correct |
41 |
Correct |
48 ms |
15440 KB |
Output is correct |
42 |
Correct |
65 ms |
17148 KB |
Output is correct |
43 |
Correct |
51 ms |
16724 KB |
Output is correct |
44 |
Correct |
47 ms |
16396 KB |
Output is correct |
45 |
Correct |
51 ms |
16468 KB |
Output is correct |
46 |
Correct |
61 ms |
16576 KB |
Output is correct |
47 |
Correct |
60 ms |
16468 KB |
Output is correct |
48 |
Correct |
71 ms |
16324 KB |
Output is correct |
49 |
Correct |
50 ms |
16516 KB |
Output is correct |
50 |
Correct |
69 ms |
16464 KB |
Output is correct |
51 |
Correct |
48 ms |
16388 KB |
Output is correct |
52 |
Correct |
64 ms |
16464 KB |
Output is correct |
53 |
Correct |
57 ms |
16468 KB |
Output is correct |
54 |
Correct |
46 ms |
16468 KB |
Output is correct |
55 |
Correct |
53 ms |
16720 KB |
Output is correct |
56 |
Correct |
39 ms |
13400 KB |
Output is correct |
57 |
Correct |
45 ms |
17292 KB |
Output is correct |
58 |
Correct |
41 ms |
16208 KB |
Output is correct |
59 |
Correct |
1 ms |
10588 KB |
Output is correct |
60 |
Correct |
1 ms |
10588 KB |
Output is correct |
61 |
Correct |
323 ms |
56420 KB |
Output is correct |
62 |
Correct |
358 ms |
57172 KB |
Output is correct |
63 |
Correct |
330 ms |
55364 KB |
Output is correct |
64 |
Correct |
314 ms |
54408 KB |
Output is correct |
65 |
Correct |
355 ms |
56144 KB |
Output is correct |
66 |
Correct |
290 ms |
54448 KB |
Output is correct |
67 |
Correct |
311 ms |
55572 KB |
Output is correct |
68 |
Correct |
298 ms |
54348 KB |
Output is correct |
69 |
Correct |
299 ms |
54120 KB |
Output is correct |
70 |
Correct |
310 ms |
54356 KB |
Output is correct |
71 |
Correct |
270 ms |
53636 KB |
Output is correct |
72 |
Correct |
317 ms |
53924 KB |
Output is correct |
73 |
Correct |
300 ms |
54100 KB |
Output is correct |
74 |
Correct |
290 ms |
54792 KB |
Output is correct |
75 |
Correct |
288 ms |
55124 KB |
Output is correct |
76 |
Correct |
61 ms |
15748 KB |
Output is correct |
77 |
Correct |
290 ms |
54572 KB |
Output is correct |
78 |
Correct |
263 ms |
50512 KB |
Output is correct |
79 |
Correct |
1 ms |
10588 KB |
Output is correct |
80 |
Correct |
1 ms |
10704 KB |
Output is correct |