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>
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#define ll long long
#define llinf 1000000000000000000LL
#define pb push_back
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 400005
#define lg 25
#define lg2 9
#define B 8
ll n;
int a[maxn],b[maxn],w[maxn],l[maxn];
ll bs[lg2];
int st[lg2][lg][maxn];
ll sum[lg2][lg][maxn];
ll mx[lg2][lg][maxn];
void init(int N, vector<int> s, vector<int> p, vector<int> W, vector<int> L) {
bs[0] = 1;
for(int i = 1;i<lg2;i++) bs[i] = bs[i-1]*B;
n = N;
for(int i = 1;i<=n;i++) a[i] = s[i-1],b[i] = p[i-1],w[i] = W[i-1]+1,l[i] = L[i-1]+1;
for(int j = 0;j<lg2;j++){
for(int i = 1;i<=n;i++){
st[j][0][i] = -1;
if(bs[j]>a[i]){
if(w[i]!=n+1){
st[j][0][i] = w[i];
sum[j][0][i] = a[i];
mx[j][0][i] = llinf;
}
}else{
if(l[i]!=n+1){
st[j][0][i] = l[i];
sum[j][0][i] = b[i];
mx[j][0][i] = a[i];
}
}
}
}
for(int j = 0;j<lg2;j++){
for(int k = 1;k<lg;k++){
for(int i = 1;i<=n;i++){
if(st[j][k-1][i]==-1||st[j][k-1][st[j][k-1][i]]==-1){
st[j][k][i] = -1;
continue;
}
st[j][k][i] = st[j][k-1][st[j][k-1][i]];
sum[j][k][i] = sum[j][k-1][i] + sum[j][k-1][st[j][k-1][i]];
mx[j][k][i] = min(mx[j][k-1][i],mx[j][k-1][st[j][k-1][i]]-sum[j][k-1][i]);
}
}
}
return;
}
long long simulate(int x, int Z) {
ll z = Z;
x++;
int j = 0;
while(x!=n+1){
while(j+1<lg2&&bs[j+1]<=z) j++;
for(int k = lg-1;k>=0;k--){
if(st[j][k][x]==-1) continue;
if(z>=mx[j][k][x]) continue;
z+=sum[j][k][x];
x = st[j][k][x];
}
if(z>=a[x]){
z+=a[x];
x = w[x];
}else{
z+=b[x];
x = l[x];
}
}
return z;
}
/**
3 2
2 6 9
3 1 2
2 2 3
1 0 1
0 1
2 3
3 2
9 9 9
3 1 2
2 2 3
1 0 1
0 1
2 3
**/
# | 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... |