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 "cave.h"
#include<set>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iostream>
using namespace std;
// 0 base index
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
// index,value
void update(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
// prefix sum till x
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
T get_range(int l, int r) {
return get(r) - (l > 0 ? get(l - 1) : 0);
}
};
void exploreCave(int N) {
fenwick<int> size(N);
for (int i = 0; i < N; i++) {
size.update(i, 1);
}
int to_ask[N];
for (int i = 0; i < N; i++)
to_ask[i] = 0;
vector<bool> done(N);
auto ask = [&](int L, int R)->bool{
for (int i = L; i <= R; i++)
if (!done[i])
to_ask[i] ^= 1;
int answer = tryCombination(to_ask);
for (int i = L; i <= R; i++)
if (!done[i])
to_ask[i] ^= 1;
return answer != tryCombination(to_ask);
};
int S[N];
for (int i = 0; i < N; i++) {
int L = 0;
while (size.get(L) != 1)
L++;
int R = N - 1;
while (R - 1 >= 0 && size.get(R - 1) == N - i)
R--;
while (L < R) {
int mid = (L + R) / 2;
if (ask(L, mid)) {
R = mid;
} else {
L = mid + 1;
}
}
int answer = tryCombination(to_ask);
if (answer < (i + 1))
to_ask[R] ^= 1;
S[i] = R;
}
answer(S, to_ask);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |