/*************************************
* author: marvinthang *
* created: 22.04.2024 16:32:37 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// index from 0
template <class T> struct Fenwick {
int n; vector <T> bit;
Fenwick(int _n = 0): n(_n), bit(n + 1, 0) {}
void reset(void) { fill(bit.begin(), bit.end(), 0); }
void resize(int _n) { n = _n; bit.assign(n + 1, 0); }
void update(int i, T val) { for (++i; i <= n; i += i & -i) bit[i] += val; }
T get(int i) {
if (i < 0) return 0;
T res(0);
for (i = min(i + 1, n); i > 0; i &= i - 1) res += bit[i];
return res;
}
int upper_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i)
if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) res |= MASK(i), val -= bit[res];
return res;
}
int lower_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i)
if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) res |= MASK(i), val -= bit[res];
return res;
}
};
// end of template
void process(void) {
int n, q; cin >> n >> q;
vector <int> p(n); cin >> p;
vector <int> nxt(n), left(n);
REPD(i, n) {
--p[i];
nxt[i] = i + 1;
while (nxt[i] < n && p[i] > p[nxt[i]]) nxt[i] = nxt[nxt[i]];
left[p[i]] = i;
}
priority_queue <int> pq;
vector <int> right(n);
Fenwick <int> bit(n);
for (int i = 0; i < n; i = nxt[i]) {
right[p[i]] = nxt[i];
bit.update(p[i], nxt[i] - i);
if (i < n / 2) pq.push(p[i]);
}
int sz = right[pq.top()];
vector <vector <pair <int, int>>> queriesAt(n + 1);
REP(i, q) {
int t, x; cin >> t >> x;
queriesAt[min(t, n)].emplace_back(x - 1, i);
}
vector <int> res(q);
REP(t, n + 1) {
for (auto [x, i]: queriesAt[t]) {
int k = bit.upper_bound(x);
res[i] = p[left[k] + x - bit.get(k - 1)];
}
int v = pq.top();
int r = right[v];
right[v] -= sz - n / 2;
bit.update(v, n / 2 - sz);
for (int i = right[v]; i < r; i = nxt[i]) {
pq.push(p[i]);
right[p[i]] = min(r, nxt[i]);
bit.update(p[i], right[p[i]] - i);
}
while (true) {
int v = pq.top();
int l = left[v];
int r = right[v];
if (sz - r + l >= n / 2) {
pq.pop();
sz -= r - l;
} else break;
}
}
REP(i, q) cout << res[i] + 1 << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("abracadabra");
// int t; cin >> t; while (t--)
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:126:2: note: in expansion of macro 'file'
126 | file("abracadabra");
| ^~~~
Main.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:126:2: note: in expansion of macro 'file'
126 | file("abracadabra");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
22608 KB |
Output is correct |
2 |
Correct |
228 ms |
24660 KB |
Output is correct |
3 |
Correct |
210 ms |
24084 KB |
Output is correct |
4 |
Correct |
173 ms |
21944 KB |
Output is correct |
5 |
Correct |
191 ms |
26192 KB |
Output is correct |
6 |
Correct |
186 ms |
25940 KB |
Output is correct |
7 |
Correct |
196 ms |
27196 KB |
Output is correct |
8 |
Correct |
179 ms |
24400 KB |
Output is correct |
9 |
Correct |
185 ms |
23600 KB |
Output is correct |
10 |
Correct |
183 ms |
23312 KB |
Output is correct |
11 |
Correct |
180 ms |
23764 KB |
Output is correct |
12 |
Correct |
174 ms |
21024 KB |
Output is correct |
13 |
Correct |
194 ms |
22516 KB |
Output is correct |
14 |
Correct |
197 ms |
25436 KB |
Output is correct |
15 |
Correct |
191 ms |
23176 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
208 ms |
21688 KB |
Output is correct |
18 |
Correct |
181 ms |
21204 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
275 ms |
35524 KB |
Output is correct |
2 |
Correct |
270 ms |
41008 KB |
Output is correct |
3 |
Correct |
214 ms |
34840 KB |
Output is correct |
4 |
Correct |
198 ms |
34636 KB |
Output is correct |
5 |
Correct |
215 ms |
35780 KB |
Output is correct |
6 |
Correct |
207 ms |
33904 KB |
Output is correct |
7 |
Correct |
246 ms |
41184 KB |
Output is correct |
8 |
Correct |
236 ms |
40124 KB |
Output is correct |
9 |
Correct |
200 ms |
35252 KB |
Output is correct |
10 |
Correct |
234 ms |
37844 KB |
Output is correct |
11 |
Correct |
182 ms |
32956 KB |
Output is correct |
12 |
Correct |
186 ms |
34436 KB |
Output is correct |
13 |
Correct |
218 ms |
37124 KB |
Output is correct |
14 |
Correct |
193 ms |
35220 KB |
Output is correct |
15 |
Correct |
229 ms |
38840 KB |
Output is correct |
16 |
Correct |
20 ms |
10064 KB |
Output is correct |
17 |
Correct |
236 ms |
35776 KB |
Output is correct |
18 |
Correct |
170 ms |
30404 KB |
Output is correct |
19 |
Correct |
59 ms |
15616 KB |
Output is correct |
20 |
Correct |
65 ms |
16628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
9172 KB |
Output is correct |
2 |
Correct |
47 ms |
9264 KB |
Output is correct |
3 |
Correct |
47 ms |
8928 KB |
Output is correct |
4 |
Correct |
35 ms |
8528 KB |
Output is correct |
5 |
Correct |
41 ms |
9372 KB |
Output is correct |
6 |
Correct |
36 ms |
8516 KB |
Output is correct |
7 |
Correct |
39 ms |
9188 KB |
Output is correct |
8 |
Correct |
37 ms |
8608 KB |
Output is correct |
9 |
Correct |
40 ms |
9088 KB |
Output is correct |
10 |
Correct |
34 ms |
8056 KB |
Output is correct |
11 |
Correct |
44 ms |
8564 KB |
Output is correct |
12 |
Correct |
32 ms |
8284 KB |
Output is correct |
13 |
Correct |
33 ms |
8284 KB |
Output is correct |
14 |
Correct |
33 ms |
8536 KB |
Output is correct |
15 |
Correct |
32 ms |
8272 KB |
Output is correct |
16 |
Correct |
10 ms |
5264 KB |
Output is correct |
17 |
Correct |
44 ms |
7740 KB |
Output is correct |
18 |
Correct |
33 ms |
7716 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
22608 KB |
Output is correct |
2 |
Correct |
228 ms |
24660 KB |
Output is correct |
3 |
Correct |
210 ms |
24084 KB |
Output is correct |
4 |
Correct |
173 ms |
21944 KB |
Output is correct |
5 |
Correct |
191 ms |
26192 KB |
Output is correct |
6 |
Correct |
186 ms |
25940 KB |
Output is correct |
7 |
Correct |
196 ms |
27196 KB |
Output is correct |
8 |
Correct |
179 ms |
24400 KB |
Output is correct |
9 |
Correct |
185 ms |
23600 KB |
Output is correct |
10 |
Correct |
183 ms |
23312 KB |
Output is correct |
11 |
Correct |
180 ms |
23764 KB |
Output is correct |
12 |
Correct |
174 ms |
21024 KB |
Output is correct |
13 |
Correct |
194 ms |
22516 KB |
Output is correct |
14 |
Correct |
197 ms |
25436 KB |
Output is correct |
15 |
Correct |
191 ms |
23176 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
208 ms |
21688 KB |
Output is correct |
18 |
Correct |
181 ms |
21204 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
275 ms |
35524 KB |
Output is correct |
22 |
Correct |
270 ms |
41008 KB |
Output is correct |
23 |
Correct |
214 ms |
34840 KB |
Output is correct |
24 |
Correct |
198 ms |
34636 KB |
Output is correct |
25 |
Correct |
215 ms |
35780 KB |
Output is correct |
26 |
Correct |
207 ms |
33904 KB |
Output is correct |
27 |
Correct |
246 ms |
41184 KB |
Output is correct |
28 |
Correct |
236 ms |
40124 KB |
Output is correct |
29 |
Correct |
200 ms |
35252 KB |
Output is correct |
30 |
Correct |
234 ms |
37844 KB |
Output is correct |
31 |
Correct |
182 ms |
32956 KB |
Output is correct |
32 |
Correct |
186 ms |
34436 KB |
Output is correct |
33 |
Correct |
218 ms |
37124 KB |
Output is correct |
34 |
Correct |
193 ms |
35220 KB |
Output is correct |
35 |
Correct |
229 ms |
38840 KB |
Output is correct |
36 |
Correct |
20 ms |
10064 KB |
Output is correct |
37 |
Correct |
236 ms |
35776 KB |
Output is correct |
38 |
Correct |
170 ms |
30404 KB |
Output is correct |
39 |
Correct |
59 ms |
15616 KB |
Output is correct |
40 |
Correct |
65 ms |
16628 KB |
Output is correct |
41 |
Correct |
46 ms |
9172 KB |
Output is correct |
42 |
Correct |
47 ms |
9264 KB |
Output is correct |
43 |
Correct |
47 ms |
8928 KB |
Output is correct |
44 |
Correct |
35 ms |
8528 KB |
Output is correct |
45 |
Correct |
41 ms |
9372 KB |
Output is correct |
46 |
Correct |
36 ms |
8516 KB |
Output is correct |
47 |
Correct |
39 ms |
9188 KB |
Output is correct |
48 |
Correct |
37 ms |
8608 KB |
Output is correct |
49 |
Correct |
40 ms |
9088 KB |
Output is correct |
50 |
Correct |
34 ms |
8056 KB |
Output is correct |
51 |
Correct |
44 ms |
8564 KB |
Output is correct |
52 |
Correct |
32 ms |
8284 KB |
Output is correct |
53 |
Correct |
33 ms |
8284 KB |
Output is correct |
54 |
Correct |
33 ms |
8536 KB |
Output is correct |
55 |
Correct |
32 ms |
8272 KB |
Output is correct |
56 |
Correct |
10 ms |
5264 KB |
Output is correct |
57 |
Correct |
44 ms |
7740 KB |
Output is correct |
58 |
Correct |
33 ms |
7716 KB |
Output is correct |
59 |
Correct |
0 ms |
356 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
508 ms |
48596 KB |
Output is correct |
62 |
Correct |
446 ms |
46568 KB |
Output is correct |
63 |
Correct |
422 ms |
45160 KB |
Output is correct |
64 |
Correct |
318 ms |
44764 KB |
Output is correct |
65 |
Correct |
349 ms |
46804 KB |
Output is correct |
66 |
Correct |
332 ms |
44720 KB |
Output is correct |
67 |
Correct |
312 ms |
44344 KB |
Output is correct |
68 |
Correct |
307 ms |
42516 KB |
Output is correct |
69 |
Correct |
335 ms |
45028 KB |
Output is correct |
70 |
Correct |
288 ms |
40720 KB |
Output is correct |
71 |
Correct |
272 ms |
42320 KB |
Output is correct |
72 |
Correct |
283 ms |
41208 KB |
Output is correct |
73 |
Correct |
262 ms |
41552 KB |
Output is correct |
74 |
Correct |
284 ms |
43696 KB |
Output is correct |
75 |
Correct |
284 ms |
42424 KB |
Output is correct |
76 |
Correct |
20 ms |
10072 KB |
Output is correct |
77 |
Correct |
334 ms |
36088 KB |
Output is correct |
78 |
Correct |
223 ms |
35492 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |