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>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 400007
using namespace std;
struct event{
int par;
long long s;
int minw;
};
int n,parent[MAXN],to[MAXN],last,comp[MAXN],x;
int win[MAXN],lose[MAXN],k;
vector<int> v[MAXN];
long long curr,dist[MAXN];
pair<int,int> dp[MAXN][20];
bool li[MAXN][20];
pair<int,int> ff(int x,int p){
if(x==0)return {n,0};
if(p==0)return {parent[x],win[parent[x]]};
if(li[x][p])return dp[x][p];
li[x][p]=true;
dp[x][p].first=ff(ff(x,p-1).first,p-1).first;
dp[x][p].second=max( ff(x,p-1).second , ff(ff(x,p-1).first,p-1).second );
return dp[x][p];
}
event dp2[MAXN][30];
bool li2[MAXN][30];
event ff2(int x,int p){
if(p==0)return {to[x],lose[x],win[x]};
if(li2[x][p])return dp2[x][p];
li2[x][p]=true;
dp2[x][p].par=ff2(ff2(x,p-1).par,p-1).par;
dp2[x][p].s=ff2(x,p-1).s + ff2(ff2(x,p-1).par,p-1).s;
dp2[x][p].minw=min( ff2(x,p-1).minw , ff2(ff2(x,p-1).par,p-1).minw );
return dp2[x][p];
}
void dfs(int x,long long len){
len+=win[x]; dist[x]=len;
for(int i=0;i<v[x].size();i++){
dfs(v[x][i],len);
}
}
void init(int N,vector<int> s,vector<int> p,vector<int> w,vector<int> l){
n=N;
for(int i=0;i<n;i++){
parent[i]=w[i];
v[parent[i]].push_back(i);
to[i]=l[i];
win[i]=s[i]; lose[i]=p[i];
}
parent[n]=n; to[n]=n;
dfs(n,0);
}
long long simulate(int x, int z){
curr=z;
while(x!=n){
if(curr<win[x]){
for(int i=29;i>=0;i--){
if(curr+ff2(x,i).s<ff2(x,i).minw){
curr+=ff2(x,i).s;
x=ff2(x,i).par;
}
}
while(curr<win[x]){
curr+=lose[x]; x=to[x];
}
}
last=x;
for(int i=19;i>=0;i--){
if(ff(x,i).first!=n and ff(x,i).second<=curr){
x=ff(x,i).first;
}
}
curr+=dist[last]-dist[parent[x]];
x=parent[x];
}
return curr;
}
/*
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";
}
*/
Compilation message (stderr)
dungeons.cpp: In function 'void dfs(int, long long int)':
dungeons.cpp:52:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# | 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... |