#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 2e5 + 5;
const int QSIZ = 1e6 + 5;
int n, q;
int a[SIZE], p[SIZE], nxt[SIZE], cnt[SIZE];
int ans[QSIZ];
tuple<int, int, int> ask[QSIZ];
int bit[SIZE];
void upd(int pos, int x) {
for (; pos <= n; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
int re = 0;
for (; pos; pos -= pos & -pos) re += bit[pos];
return re;
}
pair<int, int> sch(int x) {
int pos = 0, sum = 0;
for (int i = __lg(n); i >= 0; i--) if (pos + (1 << i) <= n && sum + bit[pos + (1 << i)] <= x) {
sum += bit[pos += 1 << i];
}
return {pos + 1, sum};
}
bool work() {
auto [k, sum] = sch(n / 2);
if (sum == n / 2) return 0;
int len = cnt[k] - (n / 2 - sum);
upd(k, -len);
cnt[k] -= len;
for (int x = a[p[k] + (n / 2 - sum)]; x && !cnt[x]; x = nxt[x]) {
cnt[x] = (nxt[x] && !cnt[nxt[x]] ? p[nxt[x]] - p[x] : len);
len -= cnt[x];
upd(x, cnt[x]);
}
return 1;
}
int cal(int i) {
auto [k, sum] = sch(i - 1);
return a[p[k] + (i - sum - 1)];
}
void solve() {
cin >> n >> q;
FOR (i, 1, n) cin >> a[i], p[a[i]] = i;
FOR (i, 1, q) {
auto &[t, k, id] = ask[i];
cin >> t >> k;
id = i;
}
sort(ask + 1, ask + q + 1);
vector<int> st;
for (int i = n; i >= 1; i--) {
while (st.size() && a[i] > st.back()) st.pop_back();
if (st.size()) nxt[a[i]] = st.back();
st.pb(a[i]);
}
for (int x = a[1]; x; x = nxt[x]) {
cnt[x] = (nxt[x] ? p[nxt[x]] : n + 1) - p[x];
upd(x, cnt[x]);
}
int cur = 0, f = 1;
FOR (i, 1, q) {
auto &[t, k, id] = ask[i];
while (f && cur < t) {
cur++;
f &= work();
}
ans[id] = cal(k);
}
FOR (i, 1, q) cout << ans[i] << '\n';
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
20172 KB |
Output is correct |
2 |
Correct |
343 ms |
20172 KB |
Output is correct |
3 |
Correct |
317 ms |
19276 KB |
Output is correct |
4 |
Correct |
332 ms |
19188 KB |
Output is correct |
5 |
Correct |
333 ms |
20056 KB |
Output is correct |
6 |
Correct |
313 ms |
19312 KB |
Output is correct |
7 |
Correct |
328 ms |
20096 KB |
Output is correct |
8 |
Correct |
312 ms |
19512 KB |
Output is correct |
9 |
Correct |
317 ms |
19172 KB |
Output is correct |
10 |
Correct |
315 ms |
19564 KB |
Output is correct |
11 |
Correct |
323 ms |
19472 KB |
Output is correct |
12 |
Correct |
308 ms |
19348 KB |
Output is correct |
13 |
Correct |
328 ms |
19640 KB |
Output is correct |
14 |
Correct |
326 ms |
19788 KB |
Output is correct |
15 |
Correct |
343 ms |
20304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
296 ms |
19512 KB |
Output is correct |
18 |
Correct |
322 ms |
19360 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
398 ms |
29448 KB |
Output is correct |
2 |
Correct |
380 ms |
40472 KB |
Output is correct |
3 |
Correct |
316 ms |
31720 KB |
Output is correct |
4 |
Correct |
325 ms |
32128 KB |
Output is correct |
5 |
Correct |
362 ms |
32776 KB |
Output is correct |
6 |
Correct |
318 ms |
31572 KB |
Output is correct |
7 |
Correct |
354 ms |
39504 KB |
Output is correct |
8 |
Correct |
452 ms |
37884 KB |
Output is correct |
9 |
Correct |
358 ms |
32700 KB |
Output is correct |
10 |
Correct |
405 ms |
36772 KB |
Output is correct |
11 |
Correct |
331 ms |
30972 KB |
Output is correct |
12 |
Correct |
330 ms |
30712 KB |
Output is correct |
13 |
Correct |
373 ms |
36096 KB |
Output is correct |
14 |
Correct |
388 ms |
31792 KB |
Output is correct |
15 |
Correct |
391 ms |
37824 KB |
Output is correct |
16 |
Correct |
22 ms |
4940 KB |
Output is correct |
17 |
Correct |
345 ms |
35280 KB |
Output is correct |
18 |
Correct |
293 ms |
26656 KB |
Output is correct |
19 |
Correct |
86 ms |
9864 KB |
Output is correct |
20 |
Correct |
97 ms |
12660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
4936 KB |
Output is correct |
2 |
Correct |
56 ms |
6556 KB |
Output is correct |
3 |
Correct |
59 ms |
6044 KB |
Output is correct |
4 |
Correct |
50 ms |
5848 KB |
Output is correct |
5 |
Correct |
53 ms |
6040 KB |
Output is correct |
6 |
Correct |
60 ms |
5832 KB |
Output is correct |
7 |
Correct |
52 ms |
6140 KB |
Output is correct |
8 |
Correct |
49 ms |
5880 KB |
Output is correct |
9 |
Correct |
49 ms |
5964 KB |
Output is correct |
10 |
Correct |
62 ms |
5708 KB |
Output is correct |
11 |
Correct |
62 ms |
5816 KB |
Output is correct |
12 |
Correct |
45 ms |
5704 KB |
Output is correct |
13 |
Correct |
52 ms |
5780 KB |
Output is correct |
14 |
Correct |
49 ms |
5852 KB |
Output is correct |
15 |
Correct |
45 ms |
5760 KB |
Output is correct |
16 |
Correct |
11 ms |
2772 KB |
Output is correct |
17 |
Correct |
43 ms |
6032 KB |
Output is correct |
18 |
Correct |
55 ms |
4408 KB |
Output is correct |
19 |
Correct |
0 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
20172 KB |
Output is correct |
2 |
Correct |
343 ms |
20172 KB |
Output is correct |
3 |
Correct |
317 ms |
19276 KB |
Output is correct |
4 |
Correct |
332 ms |
19188 KB |
Output is correct |
5 |
Correct |
333 ms |
20056 KB |
Output is correct |
6 |
Correct |
313 ms |
19312 KB |
Output is correct |
7 |
Correct |
328 ms |
20096 KB |
Output is correct |
8 |
Correct |
312 ms |
19512 KB |
Output is correct |
9 |
Correct |
317 ms |
19172 KB |
Output is correct |
10 |
Correct |
315 ms |
19564 KB |
Output is correct |
11 |
Correct |
323 ms |
19472 KB |
Output is correct |
12 |
Correct |
308 ms |
19348 KB |
Output is correct |
13 |
Correct |
328 ms |
19640 KB |
Output is correct |
14 |
Correct |
326 ms |
19788 KB |
Output is correct |
15 |
Correct |
343 ms |
20304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
296 ms |
19512 KB |
Output is correct |
18 |
Correct |
322 ms |
19360 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
398 ms |
29448 KB |
Output is correct |
22 |
Correct |
380 ms |
40472 KB |
Output is correct |
23 |
Correct |
316 ms |
31720 KB |
Output is correct |
24 |
Correct |
325 ms |
32128 KB |
Output is correct |
25 |
Correct |
362 ms |
32776 KB |
Output is correct |
26 |
Correct |
318 ms |
31572 KB |
Output is correct |
27 |
Correct |
354 ms |
39504 KB |
Output is correct |
28 |
Correct |
452 ms |
37884 KB |
Output is correct |
29 |
Correct |
358 ms |
32700 KB |
Output is correct |
30 |
Correct |
405 ms |
36772 KB |
Output is correct |
31 |
Correct |
331 ms |
30972 KB |
Output is correct |
32 |
Correct |
330 ms |
30712 KB |
Output is correct |
33 |
Correct |
373 ms |
36096 KB |
Output is correct |
34 |
Correct |
388 ms |
31792 KB |
Output is correct |
35 |
Correct |
391 ms |
37824 KB |
Output is correct |
36 |
Correct |
22 ms |
4940 KB |
Output is correct |
37 |
Correct |
345 ms |
35280 KB |
Output is correct |
38 |
Correct |
293 ms |
26656 KB |
Output is correct |
39 |
Correct |
86 ms |
9864 KB |
Output is correct |
40 |
Correct |
97 ms |
12660 KB |
Output is correct |
41 |
Correct |
65 ms |
4936 KB |
Output is correct |
42 |
Correct |
56 ms |
6556 KB |
Output is correct |
43 |
Correct |
59 ms |
6044 KB |
Output is correct |
44 |
Correct |
50 ms |
5848 KB |
Output is correct |
45 |
Correct |
53 ms |
6040 KB |
Output is correct |
46 |
Correct |
60 ms |
5832 KB |
Output is correct |
47 |
Correct |
52 ms |
6140 KB |
Output is correct |
48 |
Correct |
49 ms |
5880 KB |
Output is correct |
49 |
Correct |
49 ms |
5964 KB |
Output is correct |
50 |
Correct |
62 ms |
5708 KB |
Output is correct |
51 |
Correct |
62 ms |
5816 KB |
Output is correct |
52 |
Correct |
45 ms |
5704 KB |
Output is correct |
53 |
Correct |
52 ms |
5780 KB |
Output is correct |
54 |
Correct |
49 ms |
5852 KB |
Output is correct |
55 |
Correct |
45 ms |
5760 KB |
Output is correct |
56 |
Correct |
11 ms |
2772 KB |
Output is correct |
57 |
Correct |
43 ms |
6032 KB |
Output is correct |
58 |
Correct |
55 ms |
4408 KB |
Output is correct |
59 |
Correct |
0 ms |
328 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
528 ms |
39752 KB |
Output is correct |
62 |
Correct |
549 ms |
40252 KB |
Output is correct |
63 |
Correct |
528 ms |
38284 KB |
Output is correct |
64 |
Correct |
475 ms |
37860 KB |
Output is correct |
65 |
Correct |
448 ms |
39432 KB |
Output is correct |
66 |
Correct |
429 ms |
37684 KB |
Output is correct |
67 |
Correct |
442 ms |
39012 KB |
Output is correct |
68 |
Correct |
440 ms |
37612 KB |
Output is correct |
69 |
Correct |
433 ms |
37588 KB |
Output is correct |
70 |
Correct |
418 ms |
37168 KB |
Output is correct |
71 |
Correct |
405 ms |
36424 KB |
Output is correct |
72 |
Correct |
431 ms |
36872 KB |
Output is correct |
73 |
Correct |
411 ms |
36892 KB |
Output is correct |
74 |
Correct |
414 ms |
37892 KB |
Output is correct |
75 |
Correct |
412 ms |
38068 KB |
Output is correct |
76 |
Correct |
21 ms |
4900 KB |
Output is correct |
77 |
Correct |
384 ms |
35504 KB |
Output is correct |
78 |
Correct |
373 ms |
32252 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
328 KB |
Output is correct |