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 "dungeons.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> s, p, w, l, dis;
vector<vector<int>> anc;
vector<vector<long long>> sum;
int n;
void dfs (int u) {
if (u == n || dis[u]) {
return;
}
dfs(w[u]);
dis[u] = dis[w[u]] + 1;
}
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;
dis.assign(n + 1, 0);
anc.assign(20, vector<int>(n + 1, -1));
sum.assign(20, vector<long long>(n + 1, 0));
for (int i = 0; i < n; ++i) {
dfs(i);
anc[0][i] = l[i];
sum[0][i] = p[i];
}
for (int i = 1; i < 20; ++i) {
for (int j = 0; j <= n; ++j) {
if (~anc[i - 1][j]) {
anc[i][j] = anc[i - 1][anc[i - 1][j]];
sum[i][j] = sum[i - 1][j] + sum[i - 1][anc[i - 1][j]];
}
}
}
return;
}
long long ans (int x, long long z) {
if (x == n) {
return z;
}
for (int i = 19; ~i; --i) {
if (z + sum[i][x] < s[x]) {
return ans(anc[i][x], z + sum[i][x]);
}
}
if (z < s[x]) return ans(l[x], z+p[x]);
return z + (long long)dis[x] * s[0];
}
long long simulate(int x, int z) {
return ans(x, z);
}
# | 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... |