#include "dungeons.h"
#include <bits/stdc++.h>
#define ar array
using namespace std;
typedef long long ll;
const int MXN = 4e5 + 45, MXA = 1e7 + 7;
int w[MXN], hlg[MXA], s[MXN], n;
ar <ll, 3> st[24][MXN], g[24][MXN];
void init(int N, vector<int> S, vector<int> p, vector<int> W, vector<int> l) {
n = N;
for (int i = 0; i < n; i++)
s[i] = S[i], w[i] = W[i];
for (int i = 2; i < MXA; i++)
hlg[i] = hlg[i >> 1] + 1;
for (int i = 0; i < n; i++) {
st[0][i][0] = l[i];
st[0][i][1] = s[i] + p[i] - s[l[i]];
st[0][i][2] = max(0ll, st[0][i][1]);
g[0][i][0] = w[i];
g[0][i][1] = s[i];
g[0][i][2] = max(0ll, 0ll + s[w[i]] - s[i]);
}
for (int i = 0; i < 24; i++)
g[i][n][0] = n;
for (int b = 1; b < 24; b++) {
for (int i = 0; i < n; i++) {
int h = st[b - 1][i][0];
st[b][i][0] = st[b - 1][h][0];
st[b][i][1] = st[b - 1][i][1] + st[b - 1][h][1];
st[b][i][2] = max(st[b - 1][i][2], st[b - 1][i][1] + st[b - 1][h][2]);
h = g[b - 1][i][0];
g[b][i][0] = g[b - 1][h][0];
g[b][i][1] = g[b - 1][i][1] + g[b - 1][h][1];
g[b][i][2] = max(g[b - 1][i][2], max(0ll, g[b - 1][h][2] - g[b - 1][i][1]));
}
}
}
ll simulate(int x, int z) {
ll cur = z;
while (x < n) {
if (s[x] > cur) {
ll now = cur - s[x];
int b = min(23, hlg[s[x] - cur]);
while (b >= 0) {
auto &tmp = st[b][x];
if (tmp[2] + now < 0) {
now += tmp[1];
x = tmp[0];
}
b--;
}
now += st[0][x][1];
x = st[0][x][0];
cur = s[x] + now;
} else {
int b = min(23, hlg[n - x + 1]), sv = s[x];
while (b >= 0 && x < n) {
if (g[b][x][2] <= cur && g[b][x][0] < n) {
cur += g[b][x][1];
x = g[b][x][0];
}
b--;
}
if (s[x] <= cur) {
cur += s[x];
x = w[x];
}
}
}
return cur;
}
# | 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... |