This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "floppy.h"
void read_array(int subtask_id, const vector<int> &v) {
string save;
vector<int> st;
for (int i = 0; i < int(v.size()); i++) {
while (!st.empty() && v[st.back()] <= v[i]) {
st.pop_back();
save.push_back('0');
}
st.emplace_back(i);
save.push_back('1');
}
while (!st.empty()) {
st.pop_back();
save.push_back('0');
}
assert(save.size() == 2 * v.size());
return save_to_floppy(save);
}
vector<int> solve_queries(int subtask_id, int N,
const string &bits,
const vector<int> &a, const vector<int> &b) {
assert(bits.size() == 2 * N);
vector<int> st;
vector<int> L(N);
for (int i = 0, s = 0; s < 2 * N; s++) {
if (bits[s] == '0') {
st.pop_back();
} else if (bits[s] == '1') {
L[i] = st.empty() ? -1 : st.back();
st.emplace_back(i++);
}
}
const int Z = 1;
int root = -1;
vector<int> prv(N + 1, -1);
vector<array<int, 2>> adj(N, {-1, -1});
for (int i = N - 1; i >= 0; i--) {
if (prv[Z + L[i]] == -1) {
if (L[i] == -1) {
root = i;
} else {
adj[L[i]][1] = i;
}
} else {
adj[prv[Z + L[i]]][0] = i;
}
prv[Z + L[i]] = i;
}
assert(root != -1);
vector<pair<int, int>> tree(N * 2);
function<void(int, int)> Dfs = [&](int i, int v) -> void {
if (i == -1) return;
tree[N + i] = {v, i};
Dfs(adj[i][0], v - 1);
Dfs(adj[i][1], v - 1);
};
Dfs(root, N);
for (int i = N - 1; i > 0; i--) {
tree[i] = max(tree[i * 2], tree[i * 2 + 1]);
}
vector<int> ans;
for (int i = 0; i < int(a.size()); i++) {
int l = a[i], r = b[i];
pair<int, int> res = {0, 0};
for (l += N, r += N + 1; l < r; l /= 2, r /= 2) {
if (l & 1) res = max(res, tree[l++]);
if (r & 1) res = max(res, tree[--r]);
}
ans.emplace_back(res.second);
}
return ans;
}
Compilation message (stderr)
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from floppy.cpp:1:
floppy.cpp: In function 'std::vector<int> solve_queries(int, int, const string&, const std::vector<int>&, const std::vector<int>&)':
floppy.cpp:29:22: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | assert(bits.size() == 2 * N);
| ~~~~~~~~~~~~^~~~~~~~
stub.cpp: In function 'void run2()':
stub.cpp:101:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
101 | if (query_answers.size() != M) {
| ~~~~~~~~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |