#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
#define int ll
using namespace std;
struct block
{
int head, l, r;
bool operator<(const block &y) const
{
return head < y.head;
}
};
struct qry
{
int t, i, idx;
};
struct segtree
{
private:
vector<int> tree;
int n;
void update(int idx, int val, int v, int tl, int tr)
{
if (tl == tr)
tree[v] = val;
else
{
int m = (tl + tr) / 2;
if (idx <= m)
update(idx, val, 2 * v, tl, m);
else
update(idx, val, 2 * v + 1, m + 1, tr);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
}
pii query(int idx, int v, int tl, int tr)
{
if (tl == tr)
return {tl, idx};
else
{
int m = (tl + tr) / 2;
if (tree[2 * v] > idx)
return query(idx, 2 * v, tl, m);
else
return query(idx - tree[2 * v], 2 * v + 1, m + 1, tr);
}
}
public:
segtree(int _n)
{
n = _n;
tree.resize(4 * n, 0);
}
void update(int idx, int val)
{
return update(idx, val, 1, 0, n - 1);
}
pii query(int idx)
{
return query(idx, 1, 0, n - 1);
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, Q;
cin >> N >> Q;
vector<int> arr(N);
vector<int> nxt(N, -1), res(Q, 0);
for (int i = 0; i < N; i++)
cin >> arr[i];
vector<qry> queries(Q);
for (int i = 0; i < Q; i++)
{
cin >> queries[i].t >> queries[i].i;
queries[i].i--;
queries[i].t = min(queries[i].t, N);
queries[i].idx = i;
}
sort(queries.begin(), queries.end(), [](qry &a, qry &b)
{ return a.t < b.t; });
int q_idx = 0;
while (q_idx < Q && queries[q_idx].t == 0)
{
res[queries[q_idx].idx] = arr[queries[q_idx].i];
q_idx++;
}
stack<int> stk;
for (int i = N - 1; i >= 0; i--)
{
while (!stk.empty() && arr[stk.top()] < arr[i])
stk.pop();
nxt[i] = stk.empty() ? N : stk.top();
stk.push(i);
}
segtree tree(N + 1);
vector<block> block_props(N + 1);
set<block> blocks;
int block_cnt = 0;
for (int i = 0; i < N; i++)
{
block_props[arr[i]] = {arr[i], i, nxt[i] - 1};
blocks.insert({arr[i], i, nxt[i] - 1});
tree.update(arr[i], nxt[i] - i);
block_cnt++;
i = nxt[i] - 1;
}
vector<block> fixed_blocks;
int idx = N - 1;
int t = 0;
while (idx != N / 2 - 1)
{
block b = *blocks.rbegin();
blocks.erase(b);
int sz = b.r - b.l + 1;
idx -= sz;
if (idx + 1 >= N / 2)
{
fixed_blocks.push_back(b);
continue;
}
// we want to make the block smaller
int k = N / 2 - 1 - idx - 1;
blocks.insert({b.head, b.l, b.l + k});
block_props[b.head] = {b.head, b.l, b.l + k};
tree.update(b.head, k + 1);
idx += k + 1;
for (int i = b.l + k + 1; i <= b.r; i = nxt[i])
{
int j = nxt[i];
blocks.insert({arr[i], i, min(j - 1, b.r)});
block_props[arr[i]] = {arr[i], i, min(j - 1, b.r)};
tree.update(arr[i], min(j - 1, b.r) - i + 1);
idx += min(j - 1, b.r) - i + 1;
}
t++;
while (q_idx < Q && queries[q_idx].t == t)
{
int fidx = queries[q_idx].i;
pii fres = tree.query(fidx);
res[queries[q_idx].idx] = arr[block_props[fres.first].l + fres.second];
q_idx++;
}
}
while (q_idx < Q)
{
int fidx = queries[q_idx].i;
pii fres = tree.query(fidx);
res[queries[q_idx].idx] = arr[block_props[fres.first].l + fres.second];
q_idx++;
}
// while (!blocks.empty())
// {
// fixed_blocks.push_back(*blocks.rbegin());
// blocks.erase(*blocks.rbegin());
// }
// reverse(fixed_blocks.begin(), fixed_blocks.end());
for (int q : res)
cout << q << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
43336 KB |
Output is correct |
2 |
Correct |
244 ms |
43080 KB |
Output is correct |
3 |
Correct |
224 ms |
41544 KB |
Output is correct |
4 |
Correct |
184 ms |
40172 KB |
Output is correct |
5 |
Correct |
215 ms |
42568 KB |
Output is correct |
6 |
Correct |
201 ms |
40376 KB |
Output is correct |
7 |
Correct |
221 ms |
42824 KB |
Output is correct |
8 |
Correct |
222 ms |
40688 KB |
Output is correct |
9 |
Correct |
201 ms |
40264 KB |
Output is correct |
10 |
Correct |
206 ms |
40892 KB |
Output is correct |
11 |
Correct |
209 ms |
40776 KB |
Output is correct |
12 |
Correct |
200 ms |
39524 KB |
Output is correct |
13 |
Correct |
201 ms |
40776 KB |
Output is correct |
14 |
Correct |
210 ms |
41808 KB |
Output is correct |
15 |
Correct |
218 ms |
41884 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
159 ms |
40264 KB |
Output is correct |
18 |
Correct |
228 ms |
40008 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
75196 KB |
Output is correct |
2 |
Correct |
342 ms |
78656 KB |
Output is correct |
3 |
Correct |
333 ms |
63276 KB |
Output is correct |
4 |
Correct |
238 ms |
57616 KB |
Output is correct |
5 |
Correct |
248 ms |
58952 KB |
Output is correct |
6 |
Correct |
236 ms |
56648 KB |
Output is correct |
7 |
Correct |
260 ms |
66888 KB |
Output is correct |
8 |
Correct |
249 ms |
65096 KB |
Output is correct |
9 |
Correct |
258 ms |
57928 KB |
Output is correct |
10 |
Correct |
250 ms |
62028 KB |
Output is correct |
11 |
Correct |
187 ms |
53464 KB |
Output is correct |
12 |
Correct |
204 ms |
53104 KB |
Output is correct |
13 |
Correct |
268 ms |
61256 KB |
Output is correct |
14 |
Correct |
212 ms |
54552 KB |
Output is correct |
15 |
Correct |
234 ms |
63304 KB |
Output is correct |
16 |
Correct |
31 ms |
15688 KB |
Output is correct |
17 |
Correct |
249 ms |
75136 KB |
Output is correct |
18 |
Correct |
234 ms |
50636 KB |
Output is correct |
19 |
Correct |
58 ms |
24648 KB |
Output is correct |
20 |
Correct |
74 ms |
25672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
17088 KB |
Output is correct |
2 |
Correct |
83 ms |
18308 KB |
Output is correct |
3 |
Correct |
99 ms |
17340 KB |
Output is correct |
4 |
Correct |
43 ms |
13128 KB |
Output is correct |
5 |
Correct |
70 ms |
15612 KB |
Output is correct |
6 |
Correct |
44 ms |
13128 KB |
Output is correct |
7 |
Correct |
56 ms |
14776 KB |
Output is correct |
8 |
Correct |
54 ms |
13752 KB |
Output is correct |
9 |
Correct |
59 ms |
14664 KB |
Output is correct |
10 |
Correct |
37 ms |
12360 KB |
Output is correct |
11 |
Correct |
41 ms |
12616 KB |
Output is correct |
12 |
Correct |
35 ms |
12104 KB |
Output is correct |
13 |
Correct |
39 ms |
12616 KB |
Output is correct |
14 |
Correct |
39 ms |
12616 KB |
Output is correct |
15 |
Correct |
35 ms |
12368 KB |
Output is correct |
16 |
Correct |
12 ms |
8016 KB |
Output is correct |
17 |
Correct |
61 ms |
19076 KB |
Output is correct |
18 |
Correct |
37 ms |
12040 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
43336 KB |
Output is correct |
2 |
Correct |
244 ms |
43080 KB |
Output is correct |
3 |
Correct |
224 ms |
41544 KB |
Output is correct |
4 |
Correct |
184 ms |
40172 KB |
Output is correct |
5 |
Correct |
215 ms |
42568 KB |
Output is correct |
6 |
Correct |
201 ms |
40376 KB |
Output is correct |
7 |
Correct |
221 ms |
42824 KB |
Output is correct |
8 |
Correct |
222 ms |
40688 KB |
Output is correct |
9 |
Correct |
201 ms |
40264 KB |
Output is correct |
10 |
Correct |
206 ms |
40892 KB |
Output is correct |
11 |
Correct |
209 ms |
40776 KB |
Output is correct |
12 |
Correct |
200 ms |
39524 KB |
Output is correct |
13 |
Correct |
201 ms |
40776 KB |
Output is correct |
14 |
Correct |
210 ms |
41808 KB |
Output is correct |
15 |
Correct |
218 ms |
41884 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
159 ms |
40264 KB |
Output is correct |
18 |
Correct |
228 ms |
40008 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
344 ms |
75196 KB |
Output is correct |
22 |
Correct |
342 ms |
78656 KB |
Output is correct |
23 |
Correct |
333 ms |
63276 KB |
Output is correct |
24 |
Correct |
238 ms |
57616 KB |
Output is correct |
25 |
Correct |
248 ms |
58952 KB |
Output is correct |
26 |
Correct |
236 ms |
56648 KB |
Output is correct |
27 |
Correct |
260 ms |
66888 KB |
Output is correct |
28 |
Correct |
249 ms |
65096 KB |
Output is correct |
29 |
Correct |
258 ms |
57928 KB |
Output is correct |
30 |
Correct |
250 ms |
62028 KB |
Output is correct |
31 |
Correct |
187 ms |
53464 KB |
Output is correct |
32 |
Correct |
204 ms |
53104 KB |
Output is correct |
33 |
Correct |
268 ms |
61256 KB |
Output is correct |
34 |
Correct |
212 ms |
54552 KB |
Output is correct |
35 |
Correct |
234 ms |
63304 KB |
Output is correct |
36 |
Correct |
31 ms |
15688 KB |
Output is correct |
37 |
Correct |
249 ms |
75136 KB |
Output is correct |
38 |
Correct |
234 ms |
50636 KB |
Output is correct |
39 |
Correct |
58 ms |
24648 KB |
Output is correct |
40 |
Correct |
74 ms |
25672 KB |
Output is correct |
41 |
Correct |
84 ms |
17088 KB |
Output is correct |
42 |
Correct |
83 ms |
18308 KB |
Output is correct |
43 |
Correct |
99 ms |
17340 KB |
Output is correct |
44 |
Correct |
43 ms |
13128 KB |
Output is correct |
45 |
Correct |
70 ms |
15612 KB |
Output is correct |
46 |
Correct |
44 ms |
13128 KB |
Output is correct |
47 |
Correct |
56 ms |
14776 KB |
Output is correct |
48 |
Correct |
54 ms |
13752 KB |
Output is correct |
49 |
Correct |
59 ms |
14664 KB |
Output is correct |
50 |
Correct |
37 ms |
12360 KB |
Output is correct |
51 |
Correct |
41 ms |
12616 KB |
Output is correct |
52 |
Correct |
35 ms |
12104 KB |
Output is correct |
53 |
Correct |
39 ms |
12616 KB |
Output is correct |
54 |
Correct |
39 ms |
12616 KB |
Output is correct |
55 |
Correct |
35 ms |
12368 KB |
Output is correct |
56 |
Correct |
12 ms |
8016 KB |
Output is correct |
57 |
Correct |
61 ms |
19076 KB |
Output is correct |
58 |
Correct |
37 ms |
12040 KB |
Output is correct |
59 |
Correct |
1 ms |
336 KB |
Output is correct |
60 |
Correct |
1 ms |
504 KB |
Output is correct |
61 |
Correct |
498 ms |
74684 KB |
Output is correct |
62 |
Correct |
468 ms |
78536 KB |
Output is correct |
63 |
Correct |
473 ms |
72372 KB |
Output is correct |
64 |
Correct |
388 ms |
65864 KB |
Output is correct |
65 |
Correct |
369 ms |
68680 KB |
Output is correct |
66 |
Correct |
350 ms |
65624 KB |
Output is correct |
67 |
Correct |
348 ms |
66448 KB |
Output is correct |
68 |
Correct |
349 ms |
64760 KB |
Output is correct |
69 |
Correct |
370 ms |
65352 KB |
Output is correct |
70 |
Correct |
366 ms |
62540 KB |
Output is correct |
71 |
Correct |
295 ms |
61840 KB |
Output is correct |
72 |
Correct |
322 ms |
62120 KB |
Output is correct |
73 |
Correct |
304 ms |
62024 KB |
Output is correct |
74 |
Correct |
316 ms |
63816 KB |
Output is correct |
75 |
Correct |
350 ms |
64008 KB |
Output is correct |
76 |
Correct |
24 ms |
15688 KB |
Output is correct |
77 |
Correct |
284 ms |
75456 KB |
Output is correct |
78 |
Correct |
252 ms |
59208 KB |
Output is correct |
79 |
Correct |
1 ms |
336 KB |
Output is correct |
80 |
Correct |
1 ms |
336 KB |
Output is correct |