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 + 2;
const int log2A = 25;
const int log8N = 8;
int up[N][log2A][log8N];
ll sum[N][log2A][log8N];
ll dp[N][log2A][log8N];
vector <int> s, w;
int n;
void init(int _n, vector <int> _s, vector <int> p, vector <int> _w, vector <int> l) {
n = _n;
s = _s; w = _w;
for (int k = 0; k < log2A; k++) {
for (int i = 0; i < n; i++) {
if (s[i] <= (1 << k)) {
up[i][k][0] = w[i];
sum[i][k][0] = s[i];
dp[i][k][0] = -4e18;
} else {
up[i][k][0] = l[i];
sum[i][k][0] = p[i];
dp[i][k][0] = -s[i];
}
}
for (int st = 1; st < log8N; st++) {
for (int i = 0; i < n; i++) {
up[i][k][st] = up[i][k][st - 1];
sum[i][k][st] = sum[i][k][st - 1];
dp[i][k][st] = dp[i][k][st - 1];
for (int j = 1; j < 8; j++) {
int to = up[i][k][st];
dp[i][k][st] = max(dp[i][k][st], dp[to][k][st - 1] + sum[i][k][st]);
sum[i][k][st] += sum[to][k][st - 1];
up[i][k][st] = up[to][k][st - 1];
}
}
}
}
}
ll simulate(int x, int start) {
ll z = start;
for (int k = 0; k < log2A && x != n; k++) {
if (z >= (1 << (k + 1)) && k + 1 < log2A) {
continue;
}
for (int j = log8N - 1; j >= 0 && x != n; j--) {
for (int p = 0; p < 8 && x != n; p++) {
if (z + dp[x][k][j] < 0) {
z += sum[x][k][j];
x = up[x][k][j];
} else {
break;
}
}
}
if (x == n) {
break;
}
z += s[x];
x = w[x];
}
// assert(x == n);
return z;
}
/*
3 2
2 6 9
3 1 2
2 2 3
1 0 1
0 1
2 3
*/
# | 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... |