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;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=400007,K=24;
int g[13][K][N];
int mx[13][K][N];
ll sum[13][K][N];
vector<int>S,P,W,L;
int n;
void init(int nn,vector<int>s,vector<int>p,vector<int>w,vector<int>l)
{
n=nn;
s.pb(0);
p.pb(0);
w.pb(n);
l.pb(n);
S=s,P=p,W=w;L=l;
for(int j=0;j<13;j++)
{
for(int i=0;i<=n;i++)
{
if(s[i]<(1<<(2*j)))
{
g[j][0][i]=w[i];
sum[j][0][i]=s[i];
mx[j][0][i]=-inf;
}
else
{
g[j][0][i]=l[i];
sum[j][0][i]=p[i];
mx[j][0][i]=-s[i];
}
}
for(int l=1;l<K;l++)
{
for(int i=0;i<=n;i++)
{
int x=g[j][l-1][i];
g[j][l][i]=g[j][l-1][x];
sum[j][l][i]=sum[j][l-1][i]+sum[j][l-1][x];
mx[j][l][i]=mx[j][l-1][i];
if(mx[j][l-1][x]!=-inf) mx[j][l][i]=max(mx[j][l][i],(int)min((ll)inf,sum[j][l-1][i]+mx[j][l-1][x]));
}
}
}
}
int cnt=0;
ll simulate(int x,int z)
{
ll a=z;
int j=0;
while(x!=n)
{
while(j<12&&a>=(1LL<<(2*j+2))) j++;
cnt++;
for(int l=K-1;l>=0;l--)
{
if(g[j][l][x]==n||(mx[j][l][x]!=-inf&&a+mx[j][l][x]>=0)) continue;
a+=sum[j][l][x];
x=g[j][l][x];
}
if(a>=S[x])
{
a+=S[x];
x=W[x];
}
else
{
a+=P[x];
x=L[x];
}
while(j<12&&a>=(1LL<<(2*j+2))) j++;
}
return a;
}
/*int main()
{
// freopen("inn.txt","r",stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
init(3, {2, 6, 9}, {3, 1, 2}, {2, 2, 3}, {1, 0, 1});
cout<<simulate(0,1)<<endl;
cout<<simulate(2,3)<<endl;
/* int n,q;
cin>>n>>q;
vector<int>sa(n),pa(n),wa(n),la(n);
for(int i=0;i<n;i++) cin>>sa[i];
for(int i=0;i<n;i++) cin>>pa[i];
for(int i=0;i<n;i++) cin>>wa[i];
for(int i=0;i<n;i++) cin>>la[i];
init(n,sa,pa,wa,la);
while(q--)
{
int a,b;
cin>>a>>b;
simulate(a,b);
}
return 0;
}*/
Compilation message (stderr)
dungeons.cpp:101:3: warning: "/*" within comment [-Wcomment]
101 | /* int n,q;
|
# | 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... |