#include <bits/stdc++.h>
#define ll long long
#define ar array
using namespace std;
struct fenwick {
int n, lg; vector<int> bit;
fenwick(int a) : n(a), lg(__lg(n)), bit(a+1) {}
int pref(int pos) {
pos++;
int ret = 0;
if (pos > n) pos = n;
while (pos) {
ret += bit[pos], pos -= pos&-pos;
}
return ret;
}
int point(int pos) {
return pref(pos) - pref(pos-1);
}
void update(int pos, int x) {
pos++;
while (pos <= n) {
bit[pos] += x, pos += pos&-pos;
}
}
int last_smaller(int x) {
int pos = 0;
for (int i = lg; i >= 0; i--) {
if (pos + (1 << i) <= n && x > bit[pos+(1<<i)]) {
x -= bit[pos+(1<<i)];
pos += 1<<i;
}
}
return pos-1;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int a, q;
cin >> a >> q;
vector<int> t(a);
for (int &x : t) cin >> x;
auto upd = [&](vector<int> &C) {
vector<int> A(a/2), B(a/2), ret(a);
for (int j = 0; j < a/2; j++) A[j] = C[j], B[j] = C[j+a/2];
int ii = 0, jj = 0;
for (int j = 0; j < a; j++) {
if (ii == a/2) ret[j] = B[jj++];
else if (jj == a/2) ret[j] = A[ii++];
else {
if (A[ii] < B[jj]) ret[j] = A[ii++];
else ret[j] = B[jj++];
}
}
swap(C, ret);
};
vector<int> orig_t(t);
upd(t);
vector<int> where(a+1);
fenwick fw(a+1);
for (int i = 0; i < a; i++) where[t[i]] = i;
for (int i = 0; i < a; i++) {
int r = i;
while (r + 1 < a && t[r+1] <= t[i]) r++;
fw.update(t[i], r-i+1);
i = r;
}
vector<int> nxt(a, a);
stack<ar<int, 2>> stk;
for (int i = a-1; i >= 0; i--) {
while (stk.size() && stk.top()[0] < t[i]) stk.pop();
if (stk.size()) nxt[i] = stk.top()[1];
stk.push({t[i], i});
}
vector<ar<int, 3>> queries(q);
for (int i = 0; i < q; i++) {
cin >> queries[i][0] >> queries[i][1];
queries[i][2] = i;
}
sort(queries.begin(), queries.end());
vector<int> ret(q);
int time = 1;
bool converged = 0;
for (int i = 0; i < q; i++) {
while (!converged && time < queries[i][0]) {
int ans = fw.last_smaller(a/2) + 1;
if (fw.pref(ans) == a/2) {
converged = 1; break;
}
int l1 = where[ans] + a/2 - fw.pref(ans-1), r1 = where[ans] + fw.point(ans) - 1;
int old_idx = fw.pref(ans-1), old_len = fw.point(ans);
fw.update(ans, -old_len + a/2 - old_idx);
while (l1 <= r1) {
int nxt_ = min(r1, nxt[l1] - 1);
//assert(fw.point(t[l1]) == 0);
fw.update(t[l1], nxt_-l1+1);
l1 = nxt_ + 1;
}
time++;
}
if (!queries[i][0]) ret[queries[i][2]] = orig_t[queries[i][1]-1];
else {
int idx = queries[i][1], ans = fw.last_smaller(idx) + 1;
ret[queries[i][2]] = t[where[ans] + idx - fw.pref(ans-1) - 1];
}
}
for (int i = 0; i < q; i++) cout << ret[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
20492 KB |
Output is correct |
2 |
Correct |
389 ms |
22604 KB |
Output is correct |
3 |
Correct |
344 ms |
22620 KB |
Output is correct |
4 |
Correct |
359 ms |
25104 KB |
Output is correct |
5 |
Correct |
359 ms |
25464 KB |
Output is correct |
6 |
Correct |
330 ms |
24248 KB |
Output is correct |
7 |
Correct |
359 ms |
24100 KB |
Output is correct |
8 |
Correct |
335 ms |
23552 KB |
Output is correct |
9 |
Correct |
335 ms |
23976 KB |
Output is correct |
10 |
Correct |
361 ms |
23684 KB |
Output is correct |
11 |
Correct |
341 ms |
23508 KB |
Output is correct |
12 |
Correct |
338 ms |
22700 KB |
Output is correct |
13 |
Correct |
342 ms |
23156 KB |
Output is correct |
14 |
Correct |
351 ms |
25704 KB |
Output is correct |
15 |
Correct |
351 ms |
26040 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
337 ms |
24516 KB |
Output is correct |
18 |
Correct |
327 ms |
24420 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
401 ms |
40084 KB |
Output is correct |
2 |
Correct |
380 ms |
40216 KB |
Output is correct |
3 |
Correct |
348 ms |
32444 KB |
Output is correct |
4 |
Correct |
342 ms |
28976 KB |
Output is correct |
5 |
Correct |
352 ms |
29124 KB |
Output is correct |
6 |
Correct |
333 ms |
30792 KB |
Output is correct |
7 |
Correct |
374 ms |
32904 KB |
Output is correct |
8 |
Correct |
352 ms |
35436 KB |
Output is correct |
9 |
Correct |
334 ms |
28792 KB |
Output is correct |
10 |
Correct |
353 ms |
33404 KB |
Output is correct |
11 |
Correct |
320 ms |
27120 KB |
Output is correct |
12 |
Correct |
320 ms |
25580 KB |
Output is correct |
13 |
Correct |
346 ms |
33340 KB |
Output is correct |
14 |
Correct |
344 ms |
31452 KB |
Output is correct |
15 |
Correct |
374 ms |
36996 KB |
Output is correct |
16 |
Correct |
21 ms |
5240 KB |
Output is correct |
17 |
Correct |
294 ms |
32796 KB |
Output is correct |
18 |
Correct |
293 ms |
25332 KB |
Output is correct |
19 |
Correct |
74 ms |
10188 KB |
Output is correct |
20 |
Correct |
95 ms |
10920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
6080 KB |
Output is correct |
2 |
Correct |
56 ms |
6068 KB |
Output is correct |
3 |
Correct |
59 ms |
6132 KB |
Output is correct |
4 |
Correct |
45 ms |
5484 KB |
Output is correct |
5 |
Correct |
53 ms |
4816 KB |
Output is correct |
6 |
Correct |
46 ms |
5632 KB |
Output is correct |
7 |
Correct |
50 ms |
4912 KB |
Output is correct |
8 |
Correct |
46 ms |
5212 KB |
Output is correct |
9 |
Correct |
49 ms |
5740 KB |
Output is correct |
10 |
Correct |
48 ms |
5600 KB |
Output is correct |
11 |
Correct |
46 ms |
4800 KB |
Output is correct |
12 |
Correct |
46 ms |
5460 KB |
Output is correct |
13 |
Correct |
45 ms |
5532 KB |
Output is correct |
14 |
Correct |
45 ms |
5752 KB |
Output is correct |
15 |
Correct |
49 ms |
5068 KB |
Output is correct |
16 |
Correct |
10 ms |
2420 KB |
Output is correct |
17 |
Correct |
41 ms |
5876 KB |
Output is correct |
18 |
Correct |
42 ms |
5044 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
20492 KB |
Output is correct |
2 |
Correct |
389 ms |
22604 KB |
Output is correct |
3 |
Correct |
344 ms |
22620 KB |
Output is correct |
4 |
Correct |
359 ms |
25104 KB |
Output is correct |
5 |
Correct |
359 ms |
25464 KB |
Output is correct |
6 |
Correct |
330 ms |
24248 KB |
Output is correct |
7 |
Correct |
359 ms |
24100 KB |
Output is correct |
8 |
Correct |
335 ms |
23552 KB |
Output is correct |
9 |
Correct |
335 ms |
23976 KB |
Output is correct |
10 |
Correct |
361 ms |
23684 KB |
Output is correct |
11 |
Correct |
341 ms |
23508 KB |
Output is correct |
12 |
Correct |
338 ms |
22700 KB |
Output is correct |
13 |
Correct |
342 ms |
23156 KB |
Output is correct |
14 |
Correct |
351 ms |
25704 KB |
Output is correct |
15 |
Correct |
351 ms |
26040 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
337 ms |
24516 KB |
Output is correct |
18 |
Correct |
327 ms |
24420 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
401 ms |
40084 KB |
Output is correct |
22 |
Correct |
380 ms |
40216 KB |
Output is correct |
23 |
Correct |
348 ms |
32444 KB |
Output is correct |
24 |
Correct |
342 ms |
28976 KB |
Output is correct |
25 |
Correct |
352 ms |
29124 KB |
Output is correct |
26 |
Correct |
333 ms |
30792 KB |
Output is correct |
27 |
Correct |
374 ms |
32904 KB |
Output is correct |
28 |
Correct |
352 ms |
35436 KB |
Output is correct |
29 |
Correct |
334 ms |
28792 KB |
Output is correct |
30 |
Correct |
353 ms |
33404 KB |
Output is correct |
31 |
Correct |
320 ms |
27120 KB |
Output is correct |
32 |
Correct |
320 ms |
25580 KB |
Output is correct |
33 |
Correct |
346 ms |
33340 KB |
Output is correct |
34 |
Correct |
344 ms |
31452 KB |
Output is correct |
35 |
Correct |
374 ms |
36996 KB |
Output is correct |
36 |
Correct |
21 ms |
5240 KB |
Output is correct |
37 |
Correct |
294 ms |
32796 KB |
Output is correct |
38 |
Correct |
293 ms |
25332 KB |
Output is correct |
39 |
Correct |
74 ms |
10188 KB |
Output is correct |
40 |
Correct |
95 ms |
10920 KB |
Output is correct |
41 |
Correct |
61 ms |
6080 KB |
Output is correct |
42 |
Correct |
56 ms |
6068 KB |
Output is correct |
43 |
Correct |
59 ms |
6132 KB |
Output is correct |
44 |
Correct |
45 ms |
5484 KB |
Output is correct |
45 |
Correct |
53 ms |
4816 KB |
Output is correct |
46 |
Correct |
46 ms |
5632 KB |
Output is correct |
47 |
Correct |
50 ms |
4912 KB |
Output is correct |
48 |
Correct |
46 ms |
5212 KB |
Output is correct |
49 |
Correct |
49 ms |
5740 KB |
Output is correct |
50 |
Correct |
48 ms |
5600 KB |
Output is correct |
51 |
Correct |
46 ms |
4800 KB |
Output is correct |
52 |
Correct |
46 ms |
5460 KB |
Output is correct |
53 |
Correct |
45 ms |
5532 KB |
Output is correct |
54 |
Correct |
45 ms |
5752 KB |
Output is correct |
55 |
Correct |
49 ms |
5068 KB |
Output is correct |
56 |
Correct |
10 ms |
2420 KB |
Output is correct |
57 |
Correct |
41 ms |
5876 KB |
Output is correct |
58 |
Correct |
42 ms |
5044 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
510 ms |
35292 KB |
Output is correct |
62 |
Correct |
530 ms |
37264 KB |
Output is correct |
63 |
Correct |
496 ms |
32824 KB |
Output is correct |
64 |
Correct |
424 ms |
32424 KB |
Output is correct |
65 |
Correct |
436 ms |
35444 KB |
Output is correct |
66 |
Correct |
427 ms |
33088 KB |
Output is correct |
67 |
Correct |
414 ms |
34764 KB |
Output is correct |
68 |
Correct |
412 ms |
34480 KB |
Output is correct |
69 |
Correct |
424 ms |
32728 KB |
Output is correct |
70 |
Correct |
419 ms |
33956 KB |
Output is correct |
71 |
Correct |
398 ms |
33724 KB |
Output is correct |
72 |
Correct |
420 ms |
31120 KB |
Output is correct |
73 |
Correct |
402 ms |
32204 KB |
Output is correct |
74 |
Correct |
412 ms |
33612 KB |
Output is correct |
75 |
Correct |
422 ms |
32172 KB |
Output is correct |
76 |
Correct |
21 ms |
4284 KB |
Output is correct |
77 |
Correct |
368 ms |
31480 KB |
Output is correct |
78 |
Correct |
376 ms |
31436 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |