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 <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 1000;
const int M = 10000001;
const int L = 1e9 + 10;
int INF = 1e7;
long long Q = 2e6 + 1;
long long INFF = 1e7 + 1;
long long INFFF = 20000012000001;
long long ZERO = 0;
long long dp[N * 24 * 24];
bitset <L> esim;
long long NN;
vector <long long> ss, pp, ww, ll;
long long nxt[N];
int lg2[M];
long long f(long long pos, long long strength, long long mindiff) {
return pos * INFFF + strength * Q + mindiff;
}
long long pos(int i, int j, int k) {
return dp[i * 576 + j * 24 + k] / INFFF;
}
long long strength(int i, int j, int k) {
return (dp[i * 576 + j * 24 + k] / Q) % INFF;
}
long long mindiff(int i, int j, int k) {
int ind = i * 576 + j * 24 + k;
long long u = 0;
if (esim[3 * ind]) u += 4;
if (esim[3 * ind + 1]) u += 2;
if (esim[3 * ind + 2]) u += 1;
return (dp[ind] % Q) * 8 + u;
}
void init(int n, vector<int> s, vector<int> p, vector<int> w, vector<int> l) {
bool ch = true;
lg2[1] = 0;
for (int i = 2; i < M; i++) {
lg2[i] = lg2[i / 2] + 1;
}
NN = n;
s.push_back(0);
p.push_back(0);
w.push_back(n);
l.push_back(n);
nxt[n] = 0;
for (int i = n - 1; i >= 0; i--) {
nxt[i] = s[i] + nxt[w[i]];
}
for (int i = 0; i <= n; i++) {
ss.push_back(s[i]);
}
for (int i = 0; i <= n; i++) {
pp.push_back(p[i]);
}
for (int i = 0; i <= n; i++) {
ww.push_back(w[i]);
}
for (int i = 0; i <= n; i++) {
ll.push_back(l[i]);
}
for (int k = 0; k < 24; k++) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 24; j++) {
if (k == 0) {
int a = ((1 << j) >= s[i]) ? w[i] : l[i];
int b = ((1 << j) >= s[i]) ? s[i] : p[i];
int c = ((1 << j) < s[i]) ? s[i] : INF;
int ind = i * 576 + j * 24 + k;
dp[ind] = f(a, b, c / 8);
c %= 8;
esim[3 * ind] = (c / 4) % 2;
esim[3 * ind + 1] = (c / 2) % 2;
esim[3 * ind + 2] = c % 2;
}
else {
int v = pos(i, j, k - 1);
int ind = i * 576 + j * 24 + k;
int a = pos(v, j, k - 1);
int b = min(INFF - 1, strength(i, j, k - 1) + strength(v, j, k - 1));
int c = max(ZERO, min(mindiff(i, j, k - 1), mindiff(v, j, k - 1) - strength(i, j, k - 1)));
dp[ind] = f(a, b, c / 8);
c %= 8;
esim[3 * ind] = (c / 4) % 2;
esim[3 * ind + 1] = (c / 2) % 2;
esim[3 * ind + 2] = c % 2;
}
}
}
}
}
long long simulate(int xx, int zz) {
long long x = xx, z = zz;
while (1) {
int v = lg2[z];
if (z >= 10000000) {
z += nxt[x];
return z;
}
for (int k = 23; k >= 0; k--) {
if (pos(x, v, k) == NN || mindiff(x, v, k) <= z || (z + strength(x, v, k) >= 10000000) || lg2[z + strength(x, v, k)] > v) continue;
z += strength(x, v, k);
x = pos(x, v, k);
}
if (z >= ss[x]) {
z += ss[x];
x = ww[x];
}
else {
z += pp[x];
x = ll[x];
}
if (x == NN) return z;
}
}
//int main() {
// init(3, { 2, 6, 9 }, { 3, 1, 2 }, { 2, 2, 3 }, { 1, 0, 1 });
// cout << simulate(0, 1) << "\n";
// cout << simulate(2, 3) << "\n";
// cout << mindiff(1, 2, 2) << "\n";
//}
Compilation message (stderr)
dungeons.cpp: In function 'void init(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
dungeons.cpp:36:9: warning: unused variable 'ch' [-Wunused-variable]
36 | bool ch = true;
| ^~
# | 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... |