/**
* user: budnikov-69c
* fname: Mikhail
* lname: Budnikov
* task: Floppy
* score: 64.004392
* date: 2020-12-03 12:04:03.862004
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define x first
#define y second
#define mp make_pair
#define mt make_tuple
void save_to_floppy(const string &bits);
const int T = 1 << 17;
int n;
int a[T];
int t[2 * T];
string buffer;
bool mode = time(0) % 2;
int arg(int i, int j) {
return a[i] > a[j] ? i : j;
}
void build() {
iota(t + T, t + 2 * T, 0);
for (int i = T - 1; i > 0; --i) {
t[i] = arg(t[i + i], t[i + i + 1]);
}
}
int query(int l, int r) {
int res = l;
for (l += T, r += T; l <= r; l /= 2, r /= 2) {
if (l % 2 == 1) {
res = arg(res, t[l++]);
}
if (r % 2 == 0) {
res = arg(res, t[r--]);
}
}
return res;
}
void rec_read(int l, int r, bool dir=false) {
if (l < r) {
buffer += '0';
int i = query(l, r - 1);
rec_read(l, i, mode);
rec_read(i + 1, r, !mode);
buffer += '1';
} else {
if (dir) {
buffer += "01";
}
}
}
void read_array(int subtask_id, const vector<int> &v) {
(void)subtask_id;
n = (int)v.size();
copy(all(v), a);
build();
rec_read(0, n);
buffer.erase(buffer.begin());
buffer.pop_back();
// cout << buffer << " " << buffer.size() << endl, exit(0);
save_to_floppy(buffer);
}
vector<int> g[T];
int where[T];
int cur_pos;
void precalc(int v) {
if (g[v].empty()) {
return;
}
if ((int)g[v].size() >= 1 + !mode) {
precalc(g[v].front());
}
where[v] = cur_pos++;
if ((int)g[v].size() >= 1 + mode) {
precalc(g[v].back());
}
}
vector<int> solve_queries(int subtask_id, int _n, const string &bits,
const vector<int> &ql,
const vector<int> &qr) {
fill_n(a, T, 0);
(void)subtask_id;
n = _n;
int ff = 0;
vector<int> st = {ff++};
for (char c : bits) {
assert(st.size());
if (c == '0') {
g[st.back()].push_back(ff);
st.push_back(ff++);
} else {
st.pop_back();
}
}
// for (int i = 0; i < ff; ++i) {
// if (g[i].empty()) {
// continue;
// }
// cout << i << "\t";
// for (int j : g[i]) {
// cout << j << " ";
// }
// cout << endl;
// }
cur_pos = 0;
precalc(0);
cur_pos = 0;
for (int i = 0; i < ff; ++i) {
if (g[i].size()) {
a[where[i]] = n - cur_pos++;
}
}
build();
vector<int> ans;
for (int i = 0; i < (int)ql.size(); ++i) {
ans.push_back(query(ql[i], qr[i]));
}
return ans;
}
#ifdef LC
namespace grader{
#define NMAX 100000
#define MMAX 100000
#define BITSMAX 2000000
int subtask_id, N, M;
std::vector<int> v, sorted_v;
std::vector<int> a, b;
std::vector<int> correct_answers;
// Print score to stdout and exit.
void score_and_exit(const double pts, const char *verdict) {
fprintf(stderr, "%s", verdict);
fprintf(stdout, "%f", pts);
exit(0);
}
// Contestant sent too many bits.
void too_many_bits() {
score_and_exit(0, "Too many stored bits!");
}
// Contestant did not send any bits.
void misformatted_stored_bits() {
score_and_exit(0, "Misformatted stored bits or save_to_floppy not called!");
}
// Contestant did not call the answer function.
void answer_not_provided() {
score_and_exit(0, "Answer not provided!");
}
// Contestant sent a wrong answer.
void wrong_answer() {
score_and_exit(0, "Wrong answer to query!");
}
// Contestant sent a correct answer.
void correct_answer() {
score_and_exit(1, "OK!");
}
void read_test() {
assert(scanf("%d", &subtask_id) == 1);
assert(scanf("%d%d", &N, &M) == 2);
assert(1 <= N && N <= NMAX);
assert(0 <= M && M <= MMAX);
v.resize(N);
for (int i = 0; i < N; ++i) {
assert(scanf("%d", &v[i]) == 1);
}
// Check all values are distinct.
sorted_v.resize(N);
for (int i = 0; i < N; ++i) {
sorted_v[i] = v[i];
}
std::sort(sorted_v.begin(), sorted_v.end());
for (int i = 0; i + 1 < N; ++i) {
assert(sorted_v[i] < sorted_v[i + 1]);
}
a.resize(M);
b.resize(M);
correct_answers.resize(M);
for (int i = 0; i < M; ++i) {
assert(scanf("%d%d%d", &a[i], &b[i], &correct_answers[i]) == 3);
assert(0 <= a[i] && a[i] <= correct_answers[i] &&
correct_answers[i] <= b[i] && b[i] < N);
}
}
void save_to_floppy(const std::string &bits) {
// cout << bits.size() << endl;
std::vector<int> contestant_answers = solve_queries(subtask_id, N, bits, a, b);
for (int i = 0; i < M; ++i) {
if (contestant_answers[i] != correct_answers[i]) {
wrong_answer();
}
}
correct_answer();
exit(0);
}
}
void save_to_floppy(const string &bs) {
grader::save_to_floppy(bs);
}
int main() {
assert(freopen("input.txt", "r", stdin));
// Read input data.
grader::read_test();
// Send subtask_id, v.
read_array(grader::subtask_id, grader::v);
grader::answer_not_provided();
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9488 KB |
Output is correct |
2 |
Correct |
8 ms |
9380 KB |
Output is correct |
3 |
Correct |
8 ms |
9480 KB |
Output is correct |
4 |
Correct |
8 ms |
9480 KB |
Output is correct |
5 |
Correct |
8 ms |
9480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
11684 KB |
Output is correct |
2 |
Correct |
38 ms |
11592 KB |
Output is correct |
3 |
Correct |
37 ms |
11964 KB |
Output is correct |
4 |
Correct |
37 ms |
11872 KB |
Output is correct |
5 |
Correct |
36 ms |
11652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
144 ms |
18524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |