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 <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10, maxlog = 25;
int n, w[maxn], l[maxn];
ll s[maxn], p[maxn];
vector < int > child[maxn];
struct jump
{
ll sum, max_strenght;
int ver;
};
struct leap
{
ll sum, best;
int ver;
leap()
{
sum = 0;
best = -1e18;
}
};
leap fp[maxlog][maxn];
jump dp[maxlog][maxn];
void dfs(int v)
{
dp[0][v].ver = w[v];
dp[0][v].sum = s[v];
dp[0][v].max_strenght = s[v];
for (int j = 1; j < maxlog; j ++)
{
dp[j][v].sum = dp[j - 1][v].sum + dp[j - 1][dp[j - 1][v].ver].sum;
dp[j][v].ver = dp[j - 1][dp[j - 1][v].ver].ver;
dp[j][v].max_strenght = max(dp[j - 1][v].max_strenght, dp[j - 1][dp[j - 1][v].ver].max_strenght);
}
for (int u : child[v])
{
dfs(u);
}
}
bool all_same = true;
set < ll > st;
vector < ll > values;
void init(int N, std::vector<int> S, std::vector<int> P, std::vector<int> W, std::vector<int> L)
{
n = N;
for (int i = 0; i < n; i ++)
{
s[i] = S[i];
p[i] = P[i];
w[i] = W[i];
l[i] = L[i];
st.insert(s[i]);
child[w[i]].push_back(i);
}
for (auto it : st)
values.push_back(it);
for (int i = 1; i < n; i ++)
{
if (s[i] != s[i - 1])
all_same = false;
}
for (int j = 0; j < maxlog; j ++)
dp[j][n].ver = n;
w[n] = n;
l[n] = n;
dfs(n);
for (int i = 0; i <= n; i ++)
{
fp[0][i].ver = l[i];
fp[0][i].sum = p[i];
fp[0][i].best = - s[i];
}
for (int j = 1; j < maxlog; j ++)
{
for (int i = 0; i <= n; i ++)
{
fp[j][i].ver = fp[j - 1][fp[j - 1][i].ver].ver;
fp[j][i].sum = fp[j - 1][fp[j - 1][i].ver].sum + fp[j - 1][i].sum;
fp[j][i].best = max(fp[j - 1][i].best, fp[j - 1][fp[j - 1][i].ver].best + fp[j - 1][i].sum);
}
}
return;
}
long long simulate(int x, int z)
{
ll strength = z;
while(x != n)
{
///cout << x << " : " << strength << endl;
if (strength >= s[x])
{
for (int bit = maxlog - 1; bit >= 0; bit --)
{
if (dp[bit][x].max_strenght <= strength)
{
strength += dp[bit][x].sum;
x = dp[bit][x].ver;
}
}
///cout << x << endl;
}
else
{
ll bef = strength;
for (int bit = maxlog - 1; bit >= 0; bit --)
{
if (fp[bit][x].best + strength < 0)
{
strength += fp[bit][x].sum;
x = fp[bit][x].ver;
}
}
if (strength == bef)
{
cout << 1 / 0 << endl;
exit(0);
}
}
}
return strength;
}
Compilation message (stderr)
dungeons.cpp: In function 'long long int simulate(int, int)':
dungeons.cpp:135:27: warning: division by zero [-Wdiv-by-zero]
135 | cout << 1 / 0 << endl;
| ~~^~~
# | 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... |