#include "Anna.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
namespace {
const int M = 18;
int cnt, n, l, r;
int from, to, ed, ans;
stack<int> st;
vector<int> lst, bit;
int x = -1, y = -1;
int p = 0, mn = 0;
vector<pair<int, int> > arr[20];
int phase = 0;
void build(int d, int l, int r) {
arr[d].emplace_back(l, r);
if (l != r) {
int mid = l + r >> 1;
build(d + 1, l, mid);
build(d + 1, mid + 1, r);
}
}
} // namespace
void InitA(int N, int ll, int rr) {
n = N;
l = ll;
r = rr;
build(0, 0, n - 1);
for(int depth = 14; depth >= 0; depth--) {
int cover = -1;
for(int i = 0; i < arr[depth].size(); i++) {
int L, R; tie(L, R) = arr[depth][i];
if (L <= l && r <= R) {
cover = i;
x = L;
y = R;
}
}
if (cover == -1) continue;
for(int i = 0; i < 4; i++) SendA(depth >> i & 1), cnt++;
for(int i = 0; i < depth; i++) SendA(cover >> i & 1), cnt++;
assert((1 << depth) > cover);
break;
}
assert(x >= 0);
from = to = x + y >> 1;
assert(from >= l && to <= r);
while (cnt < M) {
int delta = (y - to + from - x + 1) / 2;
int tmp;
if (from - x >= delta) {
tmp = from - delta;
if (l <= tmp) {
SendA(1);
from = tmp;
}
else {
SendA(0);
x = from + 1;
}
}
else {
tmp = to + delta;
if (tmp <= r) {
SendA(1);
to = tmp;
}
else {
SendA(0);
y = to - 1;
}
}
cnt++;
}
// cout << from << " " << to << endl;
// cout << x << " " << y << endl;
// exit(0);
}
void ReceiveA(bool X) {
// cerr << X << endl;
if (phase == 0) bit.push_back(X);
if (phase == 0 && bit.size() == 20) {
for(int i = 0; i < 20; i++) if (bit[i]) mn |= (1 << i);
for(int i = x; i < from; i++) lst.push_back(i);
lst.push_back(mn);
for(int i = to + 1; i <= y; i++) lst.push_back(i);
ed = mn;
for(int i = to + 1; i <= y; i++) if (i <= r) ed = i;
// cout << lst.size() << endl;
// cout << mn << endl;
// exit(0);
phase = 1;
}
else if (phase == 1) {
// cout << ed << endl;
// cout << X << endl;
// exit(0);
if (X == 0) st.pop();
else {
st.push(lst[p]);
// cout << lst[p] << " " << ed << endl;
// exit(0);
if (lst[p] == ed) {
ans = -1;
while (!st.empty() && st.top() >= l) {
ans = st.top();
st.pop();
}
assert(ans >= 0);
// cout << ans << endl;
// exit(0);
phase = 2;
}
p++;
}
}
}
int Answer() {
return ans;
}
#include "Bruno.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
namespace {
const int N = 1e6 + 10, M = 18;
int n, cnt_anna;
int a[N];
int x, y, from, to;
int phase, depth;
vector<bool> bit;
vector<pair<int, int> > arr[20];
void build(int d, int l, int r) {
arr[d].emplace_back(l, r);
if (l != r) {
int mid = l + r >> 1;
build(d + 1, l, mid);
build(d + 1, mid + 1, r);
}
}
} // namespace
void InitB(int N, std::vector<int> P) {
n = N;
for(int i = 0; i < n; i++) a[i] = P[i];
}
void ReceiveB(bool Y) {
cnt_anna++;
bit.push_back(Y);
if (phase == 0 && bit.size() == 4) {
depth = 0;
for(int i = 0; i < 4; i++) if (bit[i]) depth |= (1 << i);
bit.clear();
phase = 1;
// cout << depth << endl;
// exit(0);
}
if (phase == 1 && bit.size() == depth) {
build(0, 0, n - 1);
int idx = 0;
for(int i = 0; i < depth; i++) if (bit[i]) idx |= (1 << i);
bit.clear();
tie(x, y) = arr[depth][idx];
from = to = x + y >> 1;
phase = 2;
}
if (cnt_anna >= M) {
for(int i = 0; i < bit.size(); i++) {
int delta = (y - to + from - x + 1) / 2;
int tmp;
if (from - x >= delta) {
tmp = from - delta;
if (bit[i]) from = tmp;
else x = from + 1;
}
else {
tmp = to + delta;
if (bit[i]) to = tmp;
else y = to - 1;
}
}
// cout << from << " " << to << " " << x << " " << y << endl;
// exit(0);
vector<int> lst;
for(int i = x; i < from; i++) lst.push_back(a[i]);
int mn = from;
for(int i = from; i <= to; i++) if (a[i] < a[mn]) mn = i;
for(int i = 0; i < 20; i++) SendB(mn >> i & 1);
lst.push_back(a[mn]);
for(int i = to + 1; i <= y; i++) lst.push_back(a[i]);
// cout << lst.size() << endl;
// exit(0);
stack<int> st;
for(int i = 0; i < lst.size(); i++) {
while (!st.empty() && lst[st.top()] >= lst[i]) st.pop(), SendB(0);
SendB(1);
st.push(i);
}
// exit(0);
}
}
Compilation message
Anna.cpp: In function 'void {anonymous}::build(int, int, int)':
Anna.cpp:29:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1;
| ~~^~~
Anna.cpp: In function 'void InitA(int, int, int)':
Anna.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0; i < arr[depth].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Anna.cpp:60:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | from = to = x + y >> 1;
| ~~^~~
Bruno.cpp: In function 'void {anonymous}::build(int, int, int)':
Bruno.cpp:24:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int mid = l + r >> 1;
| ~~^~~
Bruno.cpp: In function 'void ReceiveB(bool)':
Bruno.cpp:48:32: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | if (phase == 1 && bit.size() == depth) {
| ~~~~~~~~~~~^~~~~~~~
Bruno.cpp:54:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | from = to = x + y >> 1;
| ~~^~~
Bruno.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0; i < bit.size(); i++) {
| ~~^~~~~~~~~~~~
Bruno.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < lst.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
664 KB |
Output is correct |
2 |
Correct |
2 ms |
664 KB |
Output is correct |
3 |
Correct |
1 ms |
664 KB |
Output is correct |
4 |
Incorrect |
0 ms |
584 KB |
Wrong Answer [2] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
664 KB |
Output is correct |
2 |
Correct |
2 ms |
664 KB |
Output is correct |
3 |
Correct |
1 ms |
664 KB |
Output is correct |
4 |
Incorrect |
0 ms |
584 KB |
Wrong Answer [2] |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
31 ms |
28140 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |