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 "grader.cpp"
#include "dungeons.h"
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("-Ofast")
using namespace std;
typedef long long ll;
const int N = 4e5 + 500;
ll sum_lose[N][25];
ll sum_win[N][25];
ll need_lose[N][25];
ll need_win[N][25];
int up_lose[N][25], up_win[N][25];
int n;
map <int, int> cnt;
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++) {
need_lose[i][0] = s[i]; need_win[i][0] = s[i];
sum_lose[i][0] = p[i]; sum_win[i][0] = s[i];
up_lose[i][0] = l[i]; up_win[i][0] = w[i];
}
for (int st = 1; st < 25; st++) {
for (int i = 0; i < n; i++) {
if (up_lose[i][st - 1] != n) {
int to = up_lose[i][st - 1];
need_lose[i][st] = min(need_lose[i][st - 1], need_lose[to][st - 1] - sum_lose[i][st - 1]);
sum_lose[i][st] = sum_lose[i][st - 1] + sum_lose[to][st - 1];
up_lose[i][st] = up_lose[to][st - 1];
} else {
up_lose[i][st] = n;
sum_lose[i][st] = sum_lose[i][st - 1];
need_lose[i][st] = need_lose[i][st - 1];
}
if (up_win[i][st - 1] != n) {
int to = up_win[i][st - 1];
need_win[i][st] = max(need_win[i][st - 1], need_win[to][st - 1] - sum_win[i][st - 1]);
sum_win[i][st] = sum_win[i][st - 1] + sum_win[to][st - 1];
up_win[i][st] = up_win[to][st - 1];
} else {
up_win[i][st] = n;
sum_win[i][st] = sum_win[i][st - 1];
need_win[i][st] = need_win[i][st - 1];
}
}
}
}
ll simulate(int x, int start) {
ll z = start, sum = 0, mn = 1e18;
cnt.clear();
while (x != n) {
mn = 1e18;
for (int st = 24; st >= 0 && x != n; st--) {
if (z < need_lose[x][st]) {
z += sum_lose[x][st];
mn = min(mn, need_lose[x][st]);
sum += sum_lose[x][st];
x = up_lose[x][st];
}
}
cnt[x]++;
if (cnt[x] > 30) {
ll c = (mn - z) / sum;
z += sum * c;
cnt[x] = 0;
}
sum = 0;
for (int st = 24; st >= 0 && x != n; st--) {
if (z >= need_win[x][st]) {
z += sum_win[x][st];
sum += sum_win[x][st];
x = up_win[x][st];
}
}
}
return 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... |