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 "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using ll = long long;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
struct Dungeon {
int s, p, w, l;
};
struct DS1 {
struct Lift {
long mn_esc;
int lose_d;
long add;
};
static constexpr int LOGN = 30;
int n, kv;
vector<Dungeon> arr;
vector<Lift> lift[LOGN];
DS1() {}
DS1(int kv, const vector<Dungeon>& arr) : n(sz(arr)), kv(kv), arr(arr) {
for (auto& a : lift) {
a.resize(n + 1);
}
long up_bound = kv == -1 ? 0 : long(1) << (kv + 1);
for (int i = 0; i < n; i++) {
auto& [s, p, w, l] = arr[i];
if (__lg(s) < kv || kv == -1) {
lift[0][i] = {w == n ? 0 : up_bound - s, w, s};
continue;
} else if (__lg(s) > kv) {
lift[0][i] = {l == n ? 0 : up_bound - p, l, p};
continue;
}
lift[0][i] = {l == n ? 0 : min(long(s), up_bound - p), l, p};
}
lift[0][n] = {0, n, 0};
for (int it = 1; it < LOGN; it++) {
for (int i = 0; i <= n; i++) {
auto& ql = lift[it - 1][i];
auto& qr = lift[it - 1][ql.lose_d];
lift[it][i] = {max(long(0), min(ql.mn_esc, qr.mn_esc - ql.add)),
qr.lose_d, ql.add + qr.add};
}
}
}
void advance(int& u, long& w) {
if (u == n || (kv != -1 && __lg(w) > kv)) {
return;
}
// assert(__lg(w) == kv);
for (int it = LOGN - 1; it >= 0; it--) {
auto& cq = lift[it][u];
if (kv != -1 && w >= cq.mn_esc) {
continue;
}
u = cq.lose_d;
w += cq.add;
dbg(it, u, w);
}
if (kv == -1) {
assert(u == n);
return;
}
dbg(kv, u, w);
// assert(u != n && __lg(w) == kv);
dbg(u, arr[u].s, arr[u].p);
if (w >= arr[u].s) {
w += arr[u].s;
u = arr[u].w;
} else {
w += arr[u].p;
u = arr[u].l;
}
// auto& cq = lift[0][u];
// u = cq.win_d;
// w += cq.win_add;
dbg(u, w);
// assert(u == n || __lg(w) > kv);
}
};
struct Solver2 {
static constexpr int LOGN = 60;
int n;
vector<Dungeon> arr;
DS1 lift_last;
Solver2() {}
Solver2(const vector<Dungeon>& arr)
: n(sz(arr)), arr(arr), lift_last(-1, arr) {}
long query(int u, long kv) {
while (u != n && kv < 1e9) {
if (kv >= arr[u].s) {
kv += arr[u].s;
u = arr[u].w;
} else {
kv += arr[u].p;
u = arr[u].l;
}
}
lift_last.advance(u, kv);
return kv;
}
};
struct Solver {
static constexpr int LOGN = 30;
int n;
bool is_s2 = true;
Solver2 s2;
DS1 lift[LOGN], lift_last;
Solver() {}
Solver(const vector<Dungeon>& arr) : n(sz(arr)) {
for (auto& a : arr) {
is_s2 &= a.s == a.p;
}
if (is_s2) {
s2 = Solver2(arr);
return;
}
for (int i = 0; i < LOGN; i++) {
lift[i] = DS1(i, arr);
}
lift_last = DS1(-1, arr);
}
long query(int u, long kv) {
if (is_s2) {
return s2.query(u, kv);
}
for (auto& a : lift) {
a.advance(u, kv);
}
lift_last.advance(u, kv);
assert(u == n);
return kv;
}
} solver;
void init(int n,
vector<int> arr_s,
vector<int> arr_p,
vector<int> arr_w,
vector<int> arr_l) {
vector<Dungeon> arr;
for (int i = 0; i < n; i++) {
arr.push_back({arr_s[i], arr_p[i], arr_w[i], arr_l[i]});
}
solver = Solver(arr);
}
ll simulate(int u, int kv) {
return solver.query(u, kv);
}
# | 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... |