#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
template <class T> struct Fenwick_Tree {
vector<T> bit;
int n, LOG;
Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5) {
this->LOG = 32 - __builtin_clz(n);
}
void clear() { fill(bit.begin(), bit.end(), T(0)); }
void update(int u, T val) {
// cout << u << " " << val << '\n';
for (; u <= n; u += u & -u) bit[u] += val;
}
T get(int u) {
T ans = 0;
assert(u <= n);
for (; u; u -= u & -u) ans += bit[u];
return ans;
}
T get(int l, int r) {
return get(r) - get(l - 1);
}
int lower_bound(T x) {
int pos = 0;
for (int k = LOG - 1; k >= 0; --k) {
if(pos + (1 << k) <= n && bit[pos + (1 << k)] < x) {
pos ^= 1 << k;
x -= bit[pos];
}
}
return pos + 1;
}
};
const int MAXN = 2e5 + 5;
int N, Q, a[MAXN], p[MAXN], ans[MAXN * 5], nxt[MAXN];
vector <pair <int, int>> queries[MAXN];
void you_make_it(void) {
cin >> N >> Q;
FOR(i, 1, N) cin >> a[i], p[a[i]] = i;
FOR(i, 1, Q) {
int t, x; cin >> t >> x;
queries[min(t, N)].emplace_back(x, i);
}
stack <int> st;
st.push(N + 1), a[N + 1] = N + 1;
FORD(i, N, 1) {
while(!st.empty() and a[st.top()] <= a[i]) st.pop();
nxt[i] = st.top();
st.push(i);
}
for (auto [x, i] : queries[0]) ans[i] = a[x];
int lt = 1;
Fenwick_Tree <int> bit(N);
while(lt <= N / 2) {
bit.update(a[lt], min(N / 2 + 1, nxt[lt]) - lt);
lt = nxt[lt];
}
lt = N / 2 + 1;
while(lt <= N) {
bit.update(a[lt], min(N + 1, nxt[lt]) - lt);
lt = nxt[lt];
}
// exit(0);
FOR(q, 1, N) {
for (auto [x, i] : queries[q]) {
int id = bit.lower_bound(x);
ans[i] = a[x - bit.get(id - 1) + p[id] - 1];
}
int id = bit.lower_bound(N / 2);
if(bit.get(id) <= N / 2) continue;
int lt = N / 2 - bit.get(id - 1) + p[id];
int rt = bit.get(id, id) + p[id] - 1;
bit.update(id, N / 2 - bit.get(id));
while(lt <= rt) {
bit.update(a[lt], min(rt + 1, nxt[lt]) - lt);
lt = nxt[lt];
}
}
FOR(i, 1, Q) cout << ans[i] << '\n';
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
Main.cpp: In instantiation of 'Fenwick_Tree<T>::Fenwick_Tree(int) [with T = int]':
Main.cpp:76:29: required from here
Main.cpp:20:9: warning: 'Fenwick_Tree<int>::n' will be initialized after [-Wreorder]
20 | int n, LOG;
| ^
Main.cpp:19:15: warning: 'std::vector<int> Fenwick_Tree<int>::bit' [-Wreorder]
19 | vector<T> bit;
| ^~~
Main.cpp:21:5: warning: when initialized here [-Wreorder]
21 | Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5) {
| ^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
25096 KB |
Output is correct |
2 |
Correct |
209 ms |
23896 KB |
Output is correct |
3 |
Correct |
194 ms |
23380 KB |
Output is correct |
4 |
Correct |
154 ms |
23356 KB |
Output is correct |
5 |
Correct |
168 ms |
25404 KB |
Output is correct |
6 |
Correct |
176 ms |
26036 KB |
Output is correct |
7 |
Correct |
176 ms |
26192 KB |
Output is correct |
8 |
Correct |
160 ms |
24876 KB |
Output is correct |
9 |
Correct |
162 ms |
23624 KB |
Output is correct |
10 |
Correct |
161 ms |
23460 KB |
Output is correct |
11 |
Correct |
162 ms |
23748 KB |
Output is correct |
12 |
Correct |
151 ms |
22184 KB |
Output is correct |
13 |
Correct |
172 ms |
22828 KB |
Output is correct |
14 |
Correct |
172 ms |
24916 KB |
Output is correct |
15 |
Correct |
185 ms |
23244 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
130 ms |
22552 KB |
Output is correct |
18 |
Correct |
161 ms |
22232 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
40252 KB |
Output is correct |
2 |
Correct |
266 ms |
40372 KB |
Output is correct |
3 |
Correct |
203 ms |
34628 KB |
Output is correct |
4 |
Correct |
186 ms |
33972 KB |
Output is correct |
5 |
Correct |
197 ms |
34996 KB |
Output is correct |
6 |
Correct |
180 ms |
34588 KB |
Output is correct |
7 |
Correct |
230 ms |
39576 KB |
Output is correct |
8 |
Correct |
223 ms |
38836 KB |
Output is correct |
9 |
Correct |
195 ms |
35124 KB |
Output is correct |
10 |
Correct |
215 ms |
37240 KB |
Output is correct |
11 |
Correct |
171 ms |
32900 KB |
Output is correct |
12 |
Correct |
178 ms |
34092 KB |
Output is correct |
13 |
Correct |
216 ms |
36536 KB |
Output is correct |
14 |
Correct |
206 ms |
34088 KB |
Output is correct |
15 |
Correct |
213 ms |
38072 KB |
Output is correct |
16 |
Correct |
27 ms |
11124 KB |
Output is correct |
17 |
Correct |
175 ms |
35848 KB |
Output is correct |
18 |
Correct |
155 ms |
32440 KB |
Output is correct |
19 |
Correct |
54 ms |
15804 KB |
Output is correct |
20 |
Correct |
75 ms |
17292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
11984 KB |
Output is correct |
2 |
Correct |
46 ms |
11604 KB |
Output is correct |
3 |
Correct |
47 ms |
11604 KB |
Output is correct |
4 |
Correct |
36 ms |
11304 KB |
Output is correct |
5 |
Correct |
44 ms |
11600 KB |
Output is correct |
6 |
Correct |
36 ms |
11136 KB |
Output is correct |
7 |
Correct |
38 ms |
11496 KB |
Output is correct |
8 |
Correct |
37 ms |
11348 KB |
Output is correct |
9 |
Correct |
39 ms |
11348 KB |
Output is correct |
10 |
Correct |
32 ms |
11120 KB |
Output is correct |
11 |
Correct |
37 ms |
11132 KB |
Output is correct |
12 |
Correct |
31 ms |
11088 KB |
Output is correct |
13 |
Correct |
32 ms |
10844 KB |
Output is correct |
14 |
Correct |
32 ms |
11096 KB |
Output is correct |
15 |
Correct |
38 ms |
11104 KB |
Output is correct |
16 |
Correct |
12 ms |
9304 KB |
Output is correct |
17 |
Correct |
24 ms |
10960 KB |
Output is correct |
18 |
Correct |
25 ms |
10712 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
25096 KB |
Output is correct |
2 |
Correct |
209 ms |
23896 KB |
Output is correct |
3 |
Correct |
194 ms |
23380 KB |
Output is correct |
4 |
Correct |
154 ms |
23356 KB |
Output is correct |
5 |
Correct |
168 ms |
25404 KB |
Output is correct |
6 |
Correct |
176 ms |
26036 KB |
Output is correct |
7 |
Correct |
176 ms |
26192 KB |
Output is correct |
8 |
Correct |
160 ms |
24876 KB |
Output is correct |
9 |
Correct |
162 ms |
23624 KB |
Output is correct |
10 |
Correct |
161 ms |
23460 KB |
Output is correct |
11 |
Correct |
162 ms |
23748 KB |
Output is correct |
12 |
Correct |
151 ms |
22184 KB |
Output is correct |
13 |
Correct |
172 ms |
22828 KB |
Output is correct |
14 |
Correct |
172 ms |
24916 KB |
Output is correct |
15 |
Correct |
185 ms |
23244 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
130 ms |
22552 KB |
Output is correct |
18 |
Correct |
161 ms |
22232 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
256 ms |
40252 KB |
Output is correct |
22 |
Correct |
266 ms |
40372 KB |
Output is correct |
23 |
Correct |
203 ms |
34628 KB |
Output is correct |
24 |
Correct |
186 ms |
33972 KB |
Output is correct |
25 |
Correct |
197 ms |
34996 KB |
Output is correct |
26 |
Correct |
180 ms |
34588 KB |
Output is correct |
27 |
Correct |
230 ms |
39576 KB |
Output is correct |
28 |
Correct |
223 ms |
38836 KB |
Output is correct |
29 |
Correct |
195 ms |
35124 KB |
Output is correct |
30 |
Correct |
215 ms |
37240 KB |
Output is correct |
31 |
Correct |
171 ms |
32900 KB |
Output is correct |
32 |
Correct |
178 ms |
34092 KB |
Output is correct |
33 |
Correct |
216 ms |
36536 KB |
Output is correct |
34 |
Correct |
206 ms |
34088 KB |
Output is correct |
35 |
Correct |
213 ms |
38072 KB |
Output is correct |
36 |
Correct |
27 ms |
11124 KB |
Output is correct |
37 |
Correct |
175 ms |
35848 KB |
Output is correct |
38 |
Correct |
155 ms |
32440 KB |
Output is correct |
39 |
Correct |
54 ms |
15804 KB |
Output is correct |
40 |
Correct |
75 ms |
17292 KB |
Output is correct |
41 |
Correct |
55 ms |
11984 KB |
Output is correct |
42 |
Correct |
46 ms |
11604 KB |
Output is correct |
43 |
Correct |
47 ms |
11604 KB |
Output is correct |
44 |
Correct |
36 ms |
11304 KB |
Output is correct |
45 |
Correct |
44 ms |
11600 KB |
Output is correct |
46 |
Correct |
36 ms |
11136 KB |
Output is correct |
47 |
Correct |
38 ms |
11496 KB |
Output is correct |
48 |
Correct |
37 ms |
11348 KB |
Output is correct |
49 |
Correct |
39 ms |
11348 KB |
Output is correct |
50 |
Correct |
32 ms |
11120 KB |
Output is correct |
51 |
Correct |
37 ms |
11132 KB |
Output is correct |
52 |
Correct |
31 ms |
11088 KB |
Output is correct |
53 |
Correct |
32 ms |
10844 KB |
Output is correct |
54 |
Correct |
32 ms |
11096 KB |
Output is correct |
55 |
Correct |
38 ms |
11104 KB |
Output is correct |
56 |
Correct |
12 ms |
9304 KB |
Output is correct |
57 |
Correct |
24 ms |
10960 KB |
Output is correct |
58 |
Correct |
25 ms |
10712 KB |
Output is correct |
59 |
Correct |
1 ms |
8796 KB |
Output is correct |
60 |
Correct |
1 ms |
8796 KB |
Output is correct |
61 |
Correct |
484 ms |
47376 KB |
Output is correct |
62 |
Correct |
410 ms |
45920 KB |
Output is correct |
63 |
Correct |
439 ms |
44752 KB |
Output is correct |
64 |
Correct |
361 ms |
44104 KB |
Output is correct |
65 |
Correct |
335 ms |
45716 KB |
Output is correct |
66 |
Correct |
301 ms |
44116 KB |
Output is correct |
67 |
Correct |
291 ms |
43412 KB |
Output is correct |
68 |
Correct |
280 ms |
42068 KB |
Output is correct |
69 |
Correct |
298 ms |
44172 KB |
Output is correct |
70 |
Correct |
273 ms |
40516 KB |
Output is correct |
71 |
Correct |
244 ms |
41552 KB |
Output is correct |
72 |
Correct |
260 ms |
40632 KB |
Output is correct |
73 |
Correct |
246 ms |
41200 KB |
Output is correct |
74 |
Correct |
269 ms |
43092 KB |
Output is correct |
75 |
Correct |
267 ms |
41616 KB |
Output is correct |
76 |
Correct |
22 ms |
11356 KB |
Output is correct |
77 |
Correct |
179 ms |
36272 KB |
Output is correct |
78 |
Correct |
196 ms |
35504 KB |
Output is correct |
79 |
Correct |
1 ms |
8796 KB |
Output is correct |
80 |
Correct |
2 ms |
8796 KB |
Output is correct |