# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1050813 | Blagoj | Dungeons Game (IOI21_dungeons) | 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 <bits/stdc++.h>
#include "dungeons.h"
using namespace std;
#define ll long long
#define inf 1e12
const int mxn = 4e5 + 10, LOG1 = 19, LOG2 = 25;
// steps, pwr
int n;
vector<int> s, p, w, l;
int go[mxn][LOG1][LOG2];
ll gain[mxn][LOG1][LOG2], lim[mxn][LOG1][LOG2];
void init(int _n, vector<int> _s, vector<int> _p, vector<int> _w, vector<int> _l) {
n = _n, s = _s, p = _p, w = _w, l = _l;
for (int pwr = 0; pwr < LOG2; pwr++) {
for (int i = 0; i < n; i++) {
if ((1LL << pwr) >= s[i]) {
if (w[i] == n) go[i][0][pwr] = -1;
else {
go[i][0][pwr] = w[i];
gain[i][0][pwr] = s[i];
lim[i][0][pwr] = inf;
}
}
else {
if (l[i] == n) go[i][0][pwr] = -1;
else {
go[i][0][pwr] = l[i];
gain[i][0][pwr] = p[i];
lim[i][0][pwr] = s[i];
}
}
}
}
for (int steps = 1; steps < LOG1; steps++) {
for (int pwr = 0; pwr < LOG2; pwr++) {
for (int i = 0; i < n; i++) {
int nxt = go[i][steps - 1][pwr];
if (go[i][steps - 1][pwr] == -1 || go[nxt][steps - 1][pwr] == -1) go[i][steps][pwr] = -1;
else {
go[i][steps][pwr] = go[nxt][steps - 1][pwr];
gain[i][steps][pwr] = gain[i][steps - 1][pwr] + gain[nxt][steps - 1][pwr];
lim[i][steps][pwr] = min(lim[i][steps - 1][pwr], lim[nxt][steps - 1][pwr] - gain[i][steps - 1][pwr]);
}
}
}
}
}
ll simulate(int x, int z) {
ll ans = z;
while (x != n) {
ll pwr = log2(ans);
pwr = min(pwr, (ll) LOG2 - 1);
for (int i = LOG1 - 1; i >= 0; i--) {
if (go[x][i][pwr] == -1) continue;
if (ans >= lim[x][i][pwr]) continue;
ans += gain[x][i][pwr];
x = go[x][i][pwr];
}
if (ans >= s[x]) {
ans += s[x];
x = w[x];
}
else {
ans += p[x];
x = l[x];
}
}
return ans;
}