//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;
}
debug(mx);
fenw.modify(P[mx], +1);
sizes[P[mx]] += 1;
}
vector<int> inv(N);
for (int i = 0; i < N; ++i) {
inv[P[i]] = i;
}
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];
if (left != s) {
int next = P[inv[f] + left];
fenw.modify(f, left - s);
fenw.modify(next, s - left);
sizes[next] += s - left;
sizes[f] += left - s;
}
}
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:157:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
157 | for (auto[q, id] : qs[i]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
26120 KB |
Output is correct |
2 |
Correct |
259 ms |
24532 KB |
Output is correct |
3 |
Correct |
232 ms |
23976 KB |
Output is correct |
4 |
Correct |
198 ms |
22552 KB |
Output is correct |
5 |
Correct |
209 ms |
26008 KB |
Output is correct |
6 |
Correct |
192 ms |
25932 KB |
Output is correct |
7 |
Incorrect |
228 ms |
27212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
40692 KB |
Output is correct |
2 |
Correct |
321 ms |
39848 KB |
Output is correct |
3 |
Correct |
241 ms |
32804 KB |
Output is correct |
4 |
Correct |
232 ms |
32928 KB |
Output is correct |
5 |
Correct |
244 ms |
33628 KB |
Output is correct |
6 |
Correct |
229 ms |
32392 KB |
Output is correct |
7 |
Incorrect |
287 ms |
39632 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
9044 KB |
Output is correct |
2 |
Correct |
60 ms |
8520 KB |
Output is correct |
3 |
Correct |
65 ms |
8392 KB |
Output is correct |
4 |
Correct |
48 ms |
7980 KB |
Output is correct |
5 |
Correct |
51 ms |
8584 KB |
Output is correct |
6 |
Correct |
46 ms |
7984 KB |
Output is correct |
7 |
Incorrect |
48 ms |
8472 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
26120 KB |
Output is correct |
2 |
Correct |
259 ms |
24532 KB |
Output is correct |
3 |
Correct |
232 ms |
23976 KB |
Output is correct |
4 |
Correct |
198 ms |
22552 KB |
Output is correct |
5 |
Correct |
209 ms |
26008 KB |
Output is correct |
6 |
Correct |
192 ms |
25932 KB |
Output is correct |
7 |
Incorrect |
228 ms |
27212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |