# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
838879 | pavement | Minerals (JOI19_minerals) | C++17 | 0 ms | 0 KiB |
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 "minerals.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
void Solve(int N) {
vector<int> uniq, others;
for (int i = 0, prv = 0; i < 2 * N; i++) {
if (Query(i + 1) > prv) {
uniq.pb(i);
prv++;
} else {
others.pb(i);
}
}
int cur_l = 0, cur_r = N - 1, prv = N;
vector<int> lo(2 * N, 0), hi(2 * N, N - 1), mid(2 * N), ans(2 * N, -1);
auto shift = [&](int new_l, int new_r) {
int tmp_new_l = new_l, tmp_new_r = new_r;
assert(cur_l <= cur_r);
assert(new_l <= new_r);
if (new_l < cur_l) {
swap(cur_l, new_l);
swap(cur_r, new_r);
}
// case 1: one range compeletely covers the other
if (new_r <= cur_r) {
for (int i = cur_l; i < new_l; i++) {
prv = Query(uniq[i] + 1);
}
for (int i = new_r + 1; i <= cur_r; i++) {
prv = Query(uniq[i] + 1);
}
} else if (cur_r < new_l) {
// case 2: completely disjoint
for (int i = cur_l; i <= cur_r; i++) {
prv = Query(uniq[i] + 1);
}
for (int i = new_l; i <= new_r; i++) {
prv = Query(uniq[i] + 1);
}
} else {
// intersection
for (int i = cur_l; i < new_l; i++) {
prv = Query(uniq[i] + 1);
}
for (int i = cur_r + 1; i <= new_r; i++) {
prv = Query(uniq[i] + 1);
}
}
cur_l = tmp_new_l;
cur_r = tmp_new_r;
};
function<void(int, int, vector<int>&)> go = [&](int l, int r, vector<int> &idxs) {
if (l > r || idxs.empty()) {
return;q
}
int m = (l + r) / 2;
vector<int> left_half, right_half;
shift(0, m);
for (auto i : idxs) {
int ret = Query(i + 1);
if (ret == prv) {
ans[i] = m;
left_half.pb(i);
} else {
right_half.pb(i);
}
prv = ret;
}
go(l, m - 1, left_half);
go(m + 1, r, right_half);
};
go(0, N - 1, others);
for (int i : others) {
Answer(i + 1, uniq[ans[i]] + 1);
}
}