#include "dungeons.h"
#include <vector>
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
typedef long long ll;
ll s[400005],p[400005],w[400005],l[400005],cost[400005],to[24][400005];
ll n;
int dp[24][400005][24];
int suma[24][400005][12];
int who[24][400005][12];
ll smax=0;
ll plsgo(int x,int dist,int k)
{
for(int i=11;i>=0;i--)
while((1<<2*i)<=dist)
{
dist-=(1<<2*i);
x=who[k][x][i];
}
return x;
}
ll plssum(int x,int dist,int k)
{
ll rez=0;
for(int i=11;i>=0;i--)
while((1<<2*i)<=dist)
{
dist-=(1<<2*i);
rez+=suma[k][x][i];
x=who[k][x][i];
}
return rez;
}
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];
smax=max(smax,s[i]*1LL);
p[i]=P[i];
w[i]=W[i];
l[i]=L[i];
}
w[n]=n;
for(int k=0;k<24;k++)
{
for(int i=0;i<n;i++)
{
if(s[i]<(1<<k))
{
to[k][i]=w[i];
cost[i]=s[i];
}
else
{
to[k][i]=l[i];
cost[i]=p[i];
}
suma[k][i][0]=cost[i];
who[k][i][0]=to[k][i];
}
cost[n]=0;
to[k][n]=n;
who[k][n][0]=n;
suma[k][n][0]=0;
for(int j=1;j<8;j++)
{
for(int i=0;i<=n;i++)
{
ll sum=0;
ll x=i;
for(int c=1;c<=4;c++)
{
sum+=suma[k][x][j-1];
x=who[k][x][j-1];
}
who[k][i][j]=x;
suma[k][i][j]=sum;
}
}
for(int i=0;i<=n;i++)
{
if(s[i]>=(1<<k))
dp[k][i][0]=0-s[i];
else
dp[k][i][0]=-1e9;
}
for(int j=1;j<24;j++)
{
for(int i=0;i<=n;i++)
{
ll x=i;
x=plsgo(x,(1LL<<(j-1)),k);
ll sum=plssum(i,(1LL<<(j-1)),k);
dp[k][i][j]=max(dp[k][i][j-1]*1LL,min(0LL,dp[k][x][j-1]+sum));
}
}
}
}
long long simulate(int x, int z)
{
ll rez=z;
int cnt=0;
while(x!=n)
{
ll k=0;
for(int i=0;i<24;i++)
if((1<<i)<rez)
k=i;
ll add=0;
for(int i=23;i>=0;i--)
{
if(rez+dp[k][x][i]<0)
{
rez+=plssum(x,(1<<i),k);
x=plsgo(x,(1<<i),k);
}
}
rez+=s[x];
x=w[x];
}
return rez;
}
Compilation message
dungeons.cpp: In function 'long long int simulate(int, int)':
dungeons.cpp:113:12: warning: unused variable 'add' [-Wunused-variable]
113 | ll add=0;
| ^~~
dungeons.cpp:106:9: warning: unused variable 'cnt' [-Wunused-variable]
106 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
205396 KB |
Output is correct |
2 |
Incorrect |
21 ms |
205416 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
209744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
209748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
209748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
209748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
209744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |