# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
600257 | PiejanVDC | Dungeons Game (IOI21_dungeons) | C++17 | 0 ms | 0 KiB |
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 "dungeons.h"
using namespace std;
const int mxN = (int)5e4+5;
const int lg = (int)32;
long long lim[mxN][lg][32];
long long gget[mxN][lg][32];
int jump[mxN][lg][32];
int N;
vector<int>S; vector<int>P; vector<int>W; vector<int>L;
bool _ = 1;
void init(int n, vector<int>s, vector<int>p, vector<int>w, vector<int>l) {
for(int i = 0 ; i < n ; i++)
if(s[i] != p[i])
_ = 0;
if(_) {
L = l;
S = s;
N = n;
for(int i = 0 ; i < n ; i++) {
jump[i][0] = w[i];
lim[i][0] = s[i];
gget[i][0] = s[i];
}
jump[n][0] = n;
for(int j = 1 ; j <= 30 ; j++) {
for(int i = 0 ; i <= n ; i++) {
jump[i][j] = jump[jump[i][j-1]][j-1];
lim[i][j] = max(lim[i][j-1], lim[jump[i][j-1]][j-1] - gget[i][j-1]);
gget[i][j] = gget[i][j-1] + gget[jump[i][j-1]][j-1];
}
}
return;
}
S = s, P = p, W = w, L = l;
N = n;
for(int j = 0 ; j < lg ; j++) {
for(int i = 0 ; i < n ; i++) {
if(pow(2,j) >= s[i]) {
lim[i][j][0] = LLONG_MAX;
gget[i][j][0] = s[i];
jump[i][j][0] = w[i];
} else {
lim[i][j][0] = s[i];
gget[i][j][0] = p[i];
jump[i][j][0] = l[i];
}
}
}
for(int k = 1 ; k < 30 ; k++) {
for(int j = 0 ; j < lg ; j++) {
for(int i = 0 ; i < n ; i++) {
lim[i][j][k] = (lim[i][j][k-1] == LLONG_MAX && lim[ jump[i][j][k-1] ][j][k-1] - gget[i][j][k-1] == LLONG_MAX ? LLONG_MAX : min(lim[i][j][k-1], lim[ jump[i][j][k-1] ][j][k-1] - gget[i][j][k-1]));
gget[i][j][k] = gget[i][j][k-1] + gget[ jump[i][j][k-1] ][j][k-1];
jump[i][j][k] = jump[ jump[i][j][k-1] ][j][k-1];
}
}
}
}
long long simulate(int x, int z) {
if(_) {
long long ans = z;
while(x != N) {
for(int j = 30 ; j >= 0 ; j--) {
if(jump[x][j] != N && ans >= lim[x][j]) {
ans += gget[x][j];
x = jump[x][j];
}
}
if(x != N) {
if(lim[x][0] <= ans) {
ans += S[x];
x = jump[x][0];
} else {
ans += S[x];
x = L[x];
}
}
}
return ans;
}
long long c = z;
int pw = 0;
while(x != N) {
while(pow(2,(pw+1)) <= c && pw+1 <= 30) {
pw++;
}
for(int j = 30 ; j >= 0 ; j--) {
if(lim[x][pw][j] > c && jump[x][pw][j] != N) {
c += gget[x][pw][j];
x = jump[x][pw][j];
}
}
if(x != N) {
if(c >= S[x]) {
c += S[x];
x = W[x];
} else {
c += P[x];
x = L[x];
}
}
}
return c;
}