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>
using namespace std;
using ll = long long;
ll N, D[50005], A[50005][30], B[50005][30];
vector<ll> S, P, W, L;
vector<ll> rev[50005];
bool sameS = 1;
long long solve(int i, int k) {
if(i == N) return k;
if(k >= S[i]) return solve(W[i], k + S[i]);
return solve(L[i], k + P[i]);
}
void dfs(int v, int p) {
for(auto u : rev[v]) {
D[u] = D[v] + S[0];
dfs(u, v);
}
}
void init(int n, vector<int> s, vector<int> p,
vector<int> w, vector<int> l) {
N = n; S.resize(n), P.resize(n), W.resize(n), L.resize(n);
for(int i = 0; i < N; i++) {
S[i] = s[i], P[i] = p[i], W[i] = w[i], L[i] = l[i];
A[i][0] = L[i], B[i][0] = P[i];
sameS &= (S[i] == S[0]);
rev[W[i]].push_back(i);
}
A[N][0] = N; L[N] = N;
for(int i = 1; i < 30; i++)
for(int l = 0; l <= N; l++) {
A[l][i] = A[A[l][i - 1]][i - 1];
B[l][i] = B[l][i - 1] + B[A[l][i - 1]][i - 1];
}
dfs(N, N);
}
long long simulate(int x, int z) {
if(!sameS) return solve(x, z);
ll res = z;
for(int l = 25; l >= 0; l--)
if(res + B[x][l] < S[0]) {
res += B[x][l];
x = A[x][l];
}
if(res >= S[0]) return res + D[x];
res += P[x]; x = L[x]; res += D[x];
return res;
}
# | 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... |