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 "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
const int INF = 1e9+10;
struct S {
int n;
vector<pair<int, int>> t;
vector<int> lzy;
void build(int v, int tl, int tr, vector<int>& a) {
if (tl == tr) {
t[v] = make_pair(a[tl], tl);
} else {
int tm = (tl + tr) / 2;
build(2*v, tl, tm, a), build(2*v+1, tm+1, tr, a);
t[v] = min(t[2*v], t[2*v+1]);
}
}
S() {}
S(vector<int> a) {
n = sz(a);
t.resize(4 * n);
lzy.resize(4 * n);
build(1, 0, n-1, a);
}
void push(int v, int tl, int tr, int x) {
if (!x) return;
t[v].first += x;
if (tl != tr) {
lzy[2*v] += x;
lzy[2*v+1] += x;
}
}
void app(int v, int tl, int tr) {
push(v, tl, tr, lzy[v]);
lzy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int x) {
app(v, tl, tr);
if (l > r || l > tr || r < tl) return;
if (l <= tl && tr <= r) {
push(v, tl, tr, x);
return;
}
int tm = (tl + tr) / 2;
upd(2*v, tl, tm, l, r, x), upd(2*v+1, tm+1, tr, l, r, x);
t[v] = min(t[2*v], t[2*v+1]);
}
void upd(int l, int r, int x) {
if (l > r) {
upd(1, 0, n-1, 0, r, +x);
upd(1, 0, n-1, l, n-1, +x);
} else {
upd(1, 0, n-1, l, r, +x);
}
}
pair<int, int> qry(int v, int tl, int tr, int l, int r) {
app(v, tl, tr);
if (l > r || l > tr || r < tl) return {INF, INF};
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2;
return min(qry(2*v, tl, tm, l, r), qry(2*v+1, tm+1, tr, l, r));
}
pair<int, int> qry(int l, int r) {
if (l > r) {
return min(qry(1, 0, n-1, 0, r), qry(1, 0, n-1, l, n-1));
} else {
return qry(1, 0, n-1, l, r);
}
}
};
int n, k;
vector<int> a, b;
vector<int> nxt, prv;
void init(int K, vector<int> r) {
n = sz(r), k = K;
vector<bool> done(n);
a.resize(n);
nxt.assign(2*n, -1), prv.assign(2*n, -1);
set<int> cand;
set<int> bad_cand;
auto prv_cand = [&](int x, bool which) {
auto& use = (which ? cand : bad_cand);
if (!sz(use)) return INF;
auto it = use.lower_bound(x);
if (it != use.begin()) return *prev(it);
return *use.rbegin();
};
auto nxt_cand = [&](int x, bool which) {
auto& use = (which ? cand : bad_cand);
if (!sz(use)) return INF;
auto it = use.upper_bound(x);
if (it == use.end()) return *use.begin();
return *it;
};
auto forward_dist = [&](int a, int b) {
if (a == INF || b == INF) return INF;
if (a < b) return b - a;
return n - a + b;
};
S seg(r);
// for (int i = 0; i < n; i++) cerr << seg.qry(i, i).first << ' '; cerr << endl;
vector<bool> has_add(n);
for (int i = 0; i < n; i++) if (!r[i]) {
cand.insert(i), has_add[i] = 1;
seg.upd(i, i, INF);
}
vector<vector<int>> stop(n);
for (int i = 0; i < n; i++) if (!r[i]) {
int p = prv_cand(i, true);
if (forward_dist(p, i) < k) {
stop[p].push_back(i);
bad_cand.insert(i);
}
}
for (int x : bad_cand) cand.erase(x);
auto prv_cand_both = [&](int x) {
int one = prv_cand(x, true), two = prv_cand(x, false);
if (forward_dist(one, x) < forward_dist(two, x)) return one;
return two;
};
auto nxt_cand_both = [&](int x) {
int one = nxt_cand(x, true), two = nxt_cand(x, false);
if (forward_dist(x, one) < forward_dist(x, two)) return one;
return two;
};
auto add_cand = [&](int x) {
// cerr << "add_cand(" << x << ")\n";
int p = prv_cand_both(x);
if (p != INF && forward_dist(p, x) < k) {
stop[p].push_back(x);
cand.erase(x);
bad_cand.insert(x);
} else {
bad_cand.erase(x);
cand.insert(x);
}
int q = nxt_cand_both(x);
if (q != INF && forward_dist(x, q) < k) {
stop[x].push_back(q);
cand.erase(q);
bad_cand.insert(q);
}
};
for (int rep = 0; rep < n; rep++) {
int me = *cand.begin();
// cerr << "> " << me << '\n';
a[me] = n - 1 - rep;
done[me] = 1;
cand.erase(me);
for (int x : stop[me]) if (!done[x]) {
add_cand(x);
}
int L = ((me - k + 1) + n) % n, R = (me - 1 + n) % n;
seg.upd(L, R, -1);
// for (int i = 0; i < n; i++) cerr << seg.qry(i, i).first << ' ';
// cerr << endl;
for (auto C = seg.qry(L, R); C.first == 0; C = seg.qry(L, R)) {
int c = C.second;
add_cand(c);
seg.upd(c, c, +INF);
}
/*
for (int i = me - k + 1; i < me; i++) { // range add, range min
int c = (i + n) % n;
if (!has_add[c]) {
r[c]--;
if (r[c] == 0) {
add_cand(c);
has_add[c] = 1;
}
}
}
*/
// for (int i = 0; i < n; i++) cerr << seg.qry(i, i).first << ' ';
// cerr << endl;
}
for (int rep : {0, 1}) for (int x : a) b.push_back(x);
assert(sz(b) == 2 * n);
/*
for (int i = 0; i < 2*n; i++) {
for (int j = i+1; j < min(2*n, i+k); j++) { // range min
if (b[j] > b[i] && (nxt[i] == -1 || b[nxt[i]] > b[j]))
nxt[i] = j;
}
for (int j = max(0, i-k+1); j < i; j++) { // range min
if (b[j] > b[i] && (prv[i] == -1 || b[prv[i]] > b[j]))
prv[i] = j;
}
}
*/
}
bool can_nxt(int x, int y) {
int c = x;
int need = y < x ? y + n : y;
while (nxt[c] != -1 && nxt[c] <= need) {
assert(b[nxt[c]] > b[c]);
c = nxt[c];
}
assert(b[need] == a[y]);
return need - c < k && b[need] >= b[c];
}
bool can_prv(int x, int y) {
int c = x > y ? x : x + n;
int need = y;
while (prv[c] != -1 && prv[c] >= need) {
assert(b[prv[c]] > b[c]);
c = prv[c];
}
assert(b[need] == a[y]);
return c - need < k && b[need] >= b[c];
}
bool can_reach(int x, int y) {
return can_nxt(x, y) || can_prv(x, y);
}
int compare_plants(int x, int y) {
if (a[y] > a[x]) return -1;
else return +1;
if (can_reach(x, y)) { // y is greater than x
assert(a[y] > a[x]);
return -1;
}
if (can_reach(y, x)) { // x is greater than y
assert(a[x] > a[y]);
return 1;
}
return 0;
}
Compilation message (stderr)
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:195:14: warning: unused variable 'rep' [-Wunused-variable]
195 | for (int rep : {0, 1}) for (int x : a) b.push_back(x);
| ^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |