#include "Anna.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
int N, L, R;
int cnt;
std::string str;
int width;
int iters;
bool is_l;
int ans = -1;
void check() {
reverse(str.begin(), str.end());
int idx = stoi(str, nullptr, 2);
// cout<< "[anna] checking idx " << idx << endl;
if (L <= idx && idx <= R && ans == -1) {
ans = idx;
}
}
} // namespace
void InitA(int N, int L, int R) {
::N = N;
::L = L;
::R = R;
width = std::bit_width((unsigned)N);
cnt = 0;
iters = 0;
auto send = [&](int x) {
// cout<< "!! [anna] sending " << x << endl;
for (int bt = 0; bt < width; ++bt) {
// cout<< "[anna] sending " << !!(x & 1 << bt) << endl;
SendA(x & 1 << bt);
}
};
is_l = N - R < L + 1 && false;
if (N - R < L + 1 && false) { // send L
// cout<< "sending l\n";
SendA(0);
send(L);
} else { // send R
// cout<< "sending r\n";
SendA(1);
send(R);
}
SendA(true);
}
void ReceiveA(bool x) {
// cout<< "[anna] recv " << int(x) << endl;
str.push_back(x ? '1' : '0');
cnt++;
if (cnt == width) {
iters++;
if (iters <= 3) {
reverse(str.begin(), str.end());
int m = stoi(str, nullptr, 2);
if (is_l) {
SendA(R <= m);
} else {
SendA(m >= L);
}
} else {
check();
}
str.clear();
cnt = 0;
}
}
int Answer() {
return ans;
}
#include "Bruno.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
int N;
int cnt;
int width;
bool is_l;
bool found_l;
int l, r;
bool recvd_l;
int st, en;
int iters;
std::vector<int> a;
std::string str;
bool FunctionExample(bool P) {
return !P;
}
void send(int x) {
// cout << "[bruno] sending index " << x << endl;
for (int bt = 0; bt < width; ++bt) {
SendB(x & 1 << bt);
}
};
void do_work() {
if (is_l) {
r = en;
} else {
l = st;
}
// cout << "[bruno] RANGE = [" << l << ", " << r << "]\n";
vector<int> ord(N);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) { return a[i] < a[j]; });
for (int &i : ord) {
if (i < l || i > r) {
continue;
}
send(i);
}
}
} // namespace
void InitB(int N, std::vector<int> P) {
::N = N;
a = P;
width = std::bit_width((unsigned)N);
cnt = 0;
found_l = false;
recvd_l = false;
iters = 0;
}
void ReceiveB(bool y) {
// cout << "[bruno] recv " << int(y) << '\n';
cnt++;
if (cnt == 1 && !found_l) {
found_l = 1;
cnt = 0;
is_l = !y;
// cout << "[bruno] is_l = " << is_l << '\n';
return;
}
str.push_back(y ? '1' : '0');
if (cnt == width && !recvd_l) {
std::reverse(str.begin(), str.end());
// cout << "str is " << str << " when cnt is width\n";
if (is_l) {
l = stoi(str, nullptr, 2);
} else {
r = stoi(str, nullptr, 2);
}
if (is_l) {
int st = l, en = N - 1;
} else {
st = 0, en = r;
}
send((st + en) / 2);
recvd_l = true;
return;
}
if (recvd_l) {
// cout << "[BRUNO] st=" << st << ", en=" << en << '\n';
iters++;
if (iters <= 3) {
int m = (st + en) / 2;
if (is_l) {
// make them return whether r <= m
if (y) {
en = m;
} else {
st = m + 1;
}
} else {
// make them return whether m >= l
if (y) {
en = m;
} else {
st = m + 1;
}
}
}
if (iters == 3) {
do_work();
} else {
send((st + en) / 2);
}
}
}