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>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "dungeons.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 4e5 + 10;
int n;
int s[N], p[N], w[N], l[N];
int nxt[N][25];
long long dmin[N][25];
long long sum[N][25];
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];
for (int i = 0; i < n; ++i) {
nxt[i][0] = w[i];
dmin[i][0] = - s[i];
sum[i][0] = s[i];
}
nxt[n][0] = n;
for (int lg = 1; lg < 25; ++lg) {
for (int i = 0; i <= n; ++i) {
nxt[i][lg] = nxt[nxt[i][lg - 1]][lg - 1];
sum[i][lg] = sum[i][lg - 1] + sum[nxt[i][lg - 1]][lg - 1];
dmin[i][lg] = min(dmin[i][lg - 1], dmin[nxt[i][lg - 1]][lg - 1] + sum[i][lg - 1]);
}
}
// assert()
}
long long simulate(int i, int z) {
long long cur = z;
while (i != n) {
for (int lg = 24; lg >= 0; --lg) {
if (nxt[i][lg] == n || dmin[i][lg] + cur < 0) continue;
cur += sum[i][lg];
i = nxt[i][lg];
}
if (dmin[i][0] + cur < 0) {
cur += s[i];
i = l[i];
} else {
cur += s[i];
i = nxt[i][0];
assert(i == n);
}
}
return cur;
}
//
//int main() {
//// freopen("test.inp", "r", stdin);
//// freopen("test.out", "w", stdout);
// int n, q;
// std::vector<int> s, p, z;
// std::vector<int> w, l, x;
// std::vector<long long> answer;
//
// assert(scanf("%d %d", &n, &q) == 2);
// s.resize(n);
// p.resize(n);
// w.resize(n);
// l.resize(n);
// x.resize(q);
// z.resize(q);
// answer.resize(q);
//
// for (int i = 0; i < n; i++) {
// assert(scanf("%d", &s[i]) == 1);
// }
// for (int i = 0; i < n; i++) {
// assert(scanf("%d", &p[i]) == 1);
// }
// for (int i = 0; i < n; i++) {
// assert(scanf("%d", &w[i]) == 1);
// }
// for (int i = 0; i < n; i++) {
// assert(scanf("%d", &l[i]) == 1);
// }
//
//
// init(n, s, p, w, l);
//
// for (int i = 0; i < q; i++) {
// assert(scanf("%d %d", &x[i], &z[i]) == 2);
// answer[i] = simulate(x[i], z[i]);
// }
// fclose(stdin);
//
// for (int i = 0; i < q; i++) {
// printf("%lld\n", answer[i]);
// }
// fclose(stdout);
// return 0;
//}
# | 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... |