//author: erray
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> string to_string(pair<A, B>);
template<typename A, typename B, typename C> string to_string(tuple<A, B, C>);
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* c) {
return to_string(string(c));
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template<size_t T>
string to_string(bitset<T> bs) {
return bs.to_string();
}
template<typename T>
string to_string(priority_queue<T> pq) {
vector<T> a;
while (!pq.empty()) {
a.push_back(pq.top());
pq.pop();
}
return to_string(a);
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < int(v.size()); ++i) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(v[i]);
}
res += "}";
return res;
}
template<typename T>
string to_string(T v) {
string res = "{";
for (auto x : v) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(x);
}
res += "}";
return res;
}
template<typename A, typename B> string to_string(pair<A, B> p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
void debug_out() {
cerr << endl;
}
template<typename H, typename... T>
void debug_out(H head, T... tail) {
cerr << " " << to_string(head);
debug_out(tail...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
struct Fenwick {
int n;
vector<int> tree;
Fenwick(int _n) : n(_n) {
tree.resize(n + 1);
}
int get(int x) {
x += 1;
int res = 0;
while (x > 0) {
res += tree[x];
x -= x & -x;
}
return res;
}
void modify(int x, int d) {
x += 1;
while (x <= n) {
tree[x] += d;
x += x & -x;
}
}
int lower_bound(int& s) {
int res = 0;
for (int i = 1 << __lg(n); i > 0; i >>= 1) {
if (res + i <= n && s > tree[res + i]) {
res += i;
s -= tree[res];
}
}
return res;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
vector<int> P(N);
for (int i = 0; i < N; ++i) {
cin >> P[i];
--P[i];
}
vector<vector<array<int, 2>>> qs(N + 1);
vector<int> ans(Q);
for (int i = 0; i < Q; ++i) {
int T, I;
cin >> T >> I;
--I;
qs[min(N, T)].push_back({I, i});
}
Fenwick fenw(N);
int mx = -1;
vector<int> sizes(N);
for (int i = 0; i < N; ++i) {
if (mx == -1 || P[mx] < P[i]) {
mx = i;
}
fenw.modify(P[mx], +1);
sizes[P[mx]] += 1;
}
vector<int> next_big(N);
vector<int> st;
for (int i = N - 1; i >= 0; --i) {
while (!st.empty() && P[st.back()] < P[i]) {
st.pop_back();
}
next_big[i] = (st.empty() ? N : st.back());
st.push_back(i);
}
vector<int> inv(N);
for (int i = 0; i < N; ++i) {
inv[P[i]] = i;
}
debug(inv);
for (int i = 0; i <= N; ++i) {
debug(i);
if (i > 0) {
int left = N / 2;
int f = fenw.lower_bound(left);
int s = sizes[f];
debug(f, s, left);
if (left != s) {
int next = inv[f] + left;
int cd = s - left;
fenw.modify(f, -cd);
sizes[f] -= cd;
while (next < inv[f] + s) {
int del = min(inv[f] + s, next_big[next]) - next;
fenw.modify(P[next], del);
sizes[P[next]] += del;
next = next_big[next];
}
}
}
for (auto[q, id] : qs[i]) {
q += 1;
int block = fenw.lower_bound(q);
ans[id] = P[inv[block] + q - 1];
}
}
for (int i = 0; i < Q; ++i) {
cout << ans[i] + 1 << " \n"[i == Q - 1];
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:172:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
172 | for (auto[q, id] : qs[i]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
19292 KB |
Output is correct |
2 |
Correct |
251 ms |
18124 KB |
Output is correct |
3 |
Correct |
243 ms |
17632 KB |
Output is correct |
4 |
Correct |
197 ms |
17336 KB |
Output is correct |
5 |
Correct |
222 ms |
19796 KB |
Output is correct |
6 |
Correct |
197 ms |
20408 KB |
Output is correct |
7 |
Correct |
237 ms |
20844 KB |
Output is correct |
8 |
Correct |
202 ms |
24480 KB |
Output is correct |
9 |
Correct |
207 ms |
23564 KB |
Output is correct |
10 |
Correct |
210 ms |
23228 KB |
Output is correct |
11 |
Correct |
232 ms |
23500 KB |
Output is correct |
12 |
Correct |
213 ms |
20956 KB |
Output is correct |
13 |
Correct |
211 ms |
22356 KB |
Output is correct |
14 |
Correct |
217 ms |
25516 KB |
Output is correct |
15 |
Correct |
217 ms |
23200 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
247 ms |
21164 KB |
Output is correct |
18 |
Correct |
203 ms |
21060 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
27948 KB |
Output is correct |
2 |
Correct |
333 ms |
28964 KB |
Output is correct |
3 |
Correct |
277 ms |
24664 KB |
Output is correct |
4 |
Correct |
257 ms |
23856 KB |
Output is correct |
5 |
Correct |
249 ms |
24316 KB |
Output is correct |
6 |
Correct |
235 ms |
23384 KB |
Output is correct |
7 |
Correct |
367 ms |
27868 KB |
Output is correct |
8 |
Correct |
337 ms |
38644 KB |
Output is correct |
9 |
Correct |
282 ms |
34324 KB |
Output is correct |
10 |
Correct |
336 ms |
37496 KB |
Output is correct |
11 |
Correct |
262 ms |
32552 KB |
Output is correct |
12 |
Correct |
273 ms |
32236 KB |
Output is correct |
13 |
Correct |
320 ms |
36960 KB |
Output is correct |
14 |
Correct |
248 ms |
33312 KB |
Output is correct |
15 |
Correct |
428 ms |
38576 KB |
Output is correct |
16 |
Correct |
29 ms |
10060 KB |
Output is correct |
17 |
Correct |
276 ms |
36144 KB |
Output is correct |
18 |
Correct |
219 ms |
30240 KB |
Output is correct |
19 |
Correct |
75 ms |
15172 KB |
Output is correct |
20 |
Correct |
91 ms |
16484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
8764 KB |
Output is correct |
2 |
Correct |
72 ms |
8692 KB |
Output is correct |
3 |
Correct |
59 ms |
8576 KB |
Output is correct |
4 |
Correct |
48 ms |
7932 KB |
Output is correct |
5 |
Correct |
51 ms |
8408 KB |
Output is correct |
6 |
Correct |
49 ms |
7908 KB |
Output is correct |
7 |
Correct |
56 ms |
8332 KB |
Output is correct |
8 |
Correct |
72 ms |
8308 KB |
Output is correct |
9 |
Correct |
49 ms |
8640 KB |
Output is correct |
10 |
Correct |
40 ms |
8184 KB |
Output is correct |
11 |
Correct |
43 ms |
8396 KB |
Output is correct |
12 |
Correct |
40 ms |
8140 KB |
Output is correct |
13 |
Correct |
45 ms |
7984 KB |
Output is correct |
14 |
Correct |
43 ms |
8332 KB |
Output is correct |
15 |
Correct |
42 ms |
8136 KB |
Output is correct |
16 |
Correct |
15 ms |
5076 KB |
Output is correct |
17 |
Correct |
49 ms |
8024 KB |
Output is correct |
18 |
Correct |
37 ms |
7556 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
19292 KB |
Output is correct |
2 |
Correct |
251 ms |
18124 KB |
Output is correct |
3 |
Correct |
243 ms |
17632 KB |
Output is correct |
4 |
Correct |
197 ms |
17336 KB |
Output is correct |
5 |
Correct |
222 ms |
19796 KB |
Output is correct |
6 |
Correct |
197 ms |
20408 KB |
Output is correct |
7 |
Correct |
237 ms |
20844 KB |
Output is correct |
8 |
Correct |
202 ms |
24480 KB |
Output is correct |
9 |
Correct |
207 ms |
23564 KB |
Output is correct |
10 |
Correct |
210 ms |
23228 KB |
Output is correct |
11 |
Correct |
232 ms |
23500 KB |
Output is correct |
12 |
Correct |
213 ms |
20956 KB |
Output is correct |
13 |
Correct |
211 ms |
22356 KB |
Output is correct |
14 |
Correct |
217 ms |
25516 KB |
Output is correct |
15 |
Correct |
217 ms |
23200 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
247 ms |
21164 KB |
Output is correct |
18 |
Correct |
203 ms |
21060 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
330 ms |
27948 KB |
Output is correct |
22 |
Correct |
333 ms |
28964 KB |
Output is correct |
23 |
Correct |
277 ms |
24664 KB |
Output is correct |
24 |
Correct |
257 ms |
23856 KB |
Output is correct |
25 |
Correct |
249 ms |
24316 KB |
Output is correct |
26 |
Correct |
235 ms |
23384 KB |
Output is correct |
27 |
Correct |
367 ms |
27868 KB |
Output is correct |
28 |
Correct |
337 ms |
38644 KB |
Output is correct |
29 |
Correct |
282 ms |
34324 KB |
Output is correct |
30 |
Correct |
336 ms |
37496 KB |
Output is correct |
31 |
Correct |
262 ms |
32552 KB |
Output is correct |
32 |
Correct |
273 ms |
32236 KB |
Output is correct |
33 |
Correct |
320 ms |
36960 KB |
Output is correct |
34 |
Correct |
248 ms |
33312 KB |
Output is correct |
35 |
Correct |
428 ms |
38576 KB |
Output is correct |
36 |
Correct |
29 ms |
10060 KB |
Output is correct |
37 |
Correct |
276 ms |
36144 KB |
Output is correct |
38 |
Correct |
219 ms |
30240 KB |
Output is correct |
39 |
Correct |
75 ms |
15172 KB |
Output is correct |
40 |
Correct |
91 ms |
16484 KB |
Output is correct |
41 |
Correct |
67 ms |
8764 KB |
Output is correct |
42 |
Correct |
72 ms |
8692 KB |
Output is correct |
43 |
Correct |
59 ms |
8576 KB |
Output is correct |
44 |
Correct |
48 ms |
7932 KB |
Output is correct |
45 |
Correct |
51 ms |
8408 KB |
Output is correct |
46 |
Correct |
49 ms |
7908 KB |
Output is correct |
47 |
Correct |
56 ms |
8332 KB |
Output is correct |
48 |
Correct |
72 ms |
8308 KB |
Output is correct |
49 |
Correct |
49 ms |
8640 KB |
Output is correct |
50 |
Correct |
40 ms |
8184 KB |
Output is correct |
51 |
Correct |
43 ms |
8396 KB |
Output is correct |
52 |
Correct |
40 ms |
8140 KB |
Output is correct |
53 |
Correct |
45 ms |
7984 KB |
Output is correct |
54 |
Correct |
43 ms |
8332 KB |
Output is correct |
55 |
Correct |
42 ms |
8136 KB |
Output is correct |
56 |
Correct |
15 ms |
5076 KB |
Output is correct |
57 |
Correct |
49 ms |
8024 KB |
Output is correct |
58 |
Correct |
37 ms |
7556 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
320 KB |
Output is correct |
61 |
Correct |
682 ms |
48064 KB |
Output is correct |
62 |
Correct |
565 ms |
46748 KB |
Output is correct |
63 |
Correct |
534 ms |
45292 KB |
Output is correct |
64 |
Correct |
397 ms |
44328 KB |
Output is correct |
65 |
Correct |
455 ms |
46368 KB |
Output is correct |
66 |
Correct |
456 ms |
44232 KB |
Output is correct |
67 |
Correct |
366 ms |
44036 KB |
Output is correct |
68 |
Correct |
369 ms |
42112 KB |
Output is correct |
69 |
Correct |
410 ms |
44572 KB |
Output is correct |
70 |
Correct |
364 ms |
40548 KB |
Output is correct |
71 |
Correct |
293 ms |
42060 KB |
Output is correct |
72 |
Correct |
350 ms |
40988 KB |
Output is correct |
73 |
Correct |
328 ms |
41552 KB |
Output is correct |
74 |
Correct |
355 ms |
43544 KB |
Output is correct |
75 |
Correct |
347 ms |
42308 KB |
Output is correct |
76 |
Correct |
30 ms |
10036 KB |
Output is correct |
77 |
Correct |
399 ms |
36424 KB |
Output is correct |
78 |
Correct |
268 ms |
35408 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |