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 "dungeons.h"
#include <bits/stdc++.h>
using namespace std;
int nnn;
vector<int>sss,ppp,www,lll;
vector<long long>winning_path;
vector<int>adj[400001];
void gen(void);
void init(int nn, vector<int> ss, vector<int> pp, vector<int> ww, vector<int> ll) {
nnn=nn;
sss=ss,ppp=pp,www=ww,lll=ll;
for(int i = 0 ; i < nnn ; i++) {
adj[www[i]].push_back(i);
}
gen();
}
void gen(void) {
vector<bool>vis(nnn+1,false);
winning_path.resize(nnn+1);
winning_path[nnn]=0;
queue<int>q;
q.push(nnn);
long long cnt=0;
while(!q.empty()) {
int sz = q.size();
while(sz--) {
int node = q.front();
q.pop();
vis[node]=true;
winning_path[node]=cnt;
for(auto z : adj[node]) {
if(!vis[z]) {
q.push(z);
}
}
}
cnt++;
}
}
long long simulate(int x, int zz) {
vector<pair<bool,long long>>v(nnn+1,{false,0});
vector<long long>steps(nnn+1,0),seq;
long long curr=0, len=0;
long long z=zz;
while(true) {
if(v[x].first) {
long long ans = (sss[x]-z)/(curr-v[x].second);
z+=ans*(curr-v[x].second);
int left=0, right=seq.size()-1, use=x;
while(left <= right) {
int mid = (left+right)/2;
if(z+(v[seq[mid]].second-v[x].second) >= sss[x]) {
use=mid;
right=mid-1;
} else {
left=mid+1;
}
}
if(use<0)use=0;
if(use>seq.size())use=seq.size()-1;
if(use>0&&use<seq.size()){
z+=v[seq[use]].second - v[x].second;
x=seq[use];}
}
if(x==nnn) return z;
if(z >= sss[x]) {
long long mul = sss[x]*winning_path[x];
return z+mul;
}
v[x].first=true,v[x].second=curr;
z+=ppp[x], curr+=ppp[x];
steps[x]=len;
seq.push_back(x);
x=lll[x];
len++;
}
}
Compilation message (stderr)
dungeons.cpp: In function 'long long int simulate(int, int)':
dungeons.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if(use>seq.size())use=seq.size()-1;
| ~~~^~~~~~~~~~~
dungeons.cpp:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if(use>0&&use<seq.size()){
| ~~~^~~~~~~~~~~
# | 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... |