# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
800425 | MohamedAliSaidane | 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;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(),(x).end()
const int nax = 4e5 + 5;
const ll INF = 1e7;
const int LG = 25;
int n;
vi S, P, W, L;
vi adj[nax];
int sp[nax][LG][LG];
ll mini[nax][LG][LG];
ll ps[nax][LG][LG];
void init(int N, vi s, vi p, vi w, vi l)
{
n = N, S = s, P = p, W= w, L = l;
S.pb(0);
W.pb(n);
for(int i = 0; i <= n; i++)
{
for(int l = 0; l < LG; l++)
{
if(S[i] <= (1 << l))
{
sp[i][l][0] = W[i];
mini[i][l][0] = INF * 1ll * INF;
ps[i][l][0] = S[i];
}
else
{
sp[i][l][0] = L[i];
mini[i][l][0] = S[i];
ps[i][l][0] = P[i];
}
}
}
for(int j = 1; j < LG; j++)
{
for(int i = 0; i <= n; i++)
{
for(int l = 0; l < LG; l++)
{
sp[i][l][j] = sp[sp[i][l][j - 1]][l][j - 1];
mini[i][l][j] = min(mini[i][l][j - 1], mini[sp[i][l][j - 1]][l][j - 1] - ps[i][l][j - 1]);
ps[i][l][j] = ps[i][l][j - 1] + ps[sp[i][l][j - 1]][l][j - 1];
}
}
}
}
pair<int,ll> check(int i, ll z)
{
ll delta = 0ll;
int l = 0;
while((1 << l) <= z && (l < LG))
l++;
l--;
if((S[i] > (1 << l)) && (S[i] <= z))
return {i, 0ll};
//cout << '\t' << i << ' ' << z << ' ' << l << '\n';
for(int j = LG- 1; j >= 0; j--)
{
bool cond = (mini[i][l][j] - delta <= z) || (sp[i][l][j] == n);
if(!(cond))
{
delta += ps[i][l][j];
i = sp[i][l][j];
}
}
if(z + delta < mini[i][l][0])
{
delta += ps[i][l][0];
i = sp[i][l][0];
}
return {i, delta};
}
ll simulate(int x, int Z)
{
ll z = Z;
while(x != n)
{
// cout << x << ' ' << z << '\n';
pair<int,ll> u = check(x, z);
x = u.ff;
z += u.ss;
z += S[x];
x = W[x];
}
return z;
}
/*
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
/*
int n, q;
cin >> n >> q;
vi s(n), p(n), w(n), l(n);
for(int i = 0; i < n; i++)
cin >> s[i];
for(int i = 0; i < n; i++)
cin >> p[i];
for(int i =0 ;i < n; i++)
cin >> w[i];
for(int i = 0;i < n; i++)
cin >> l[i];
init(n, s, p, w, l);
while(q--)
{
int x, z;
cin >> x >> z;
cout << simulate(x, z) << '\n';
}
init(3, {2, 6, 9}, {3, 1, 2}, {2, 2, 3}, {1, 0, 1});
cout << simulate(0, 1) << '\n';
cout << simulate(2, 3) << '\n';
}
*/