#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,ioi=b; i<ioi; i++)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define imp(v) {for(auto kdfhg:v)cout<<kdfhg<<" ";cout<<"\n";}
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll K=25,VK=25,MAXN=5e4+5,INF=1e18;
const ll MAXN2=4e5+5;
ll sub=1;
ll n;
ll s[MAXN2],p[MAXN2],w[MAXN2],l[MAXN2];
ii oper(ii a, ii b){
return {max(min((ll)a.fst,b.fst-a.snd),-1ll),min(a.snd+b.snd,INF)};
}
struct bloque{
ll pot;
ll a[MAXN],v[MAXN];
int F[K][MAXN];
ii V[K][MAXN];
void init(ll _pot){
pot=_pot;
fore(i,0,n){
if(pot<s[i]){
a[i]=l[i];
v[i]=p[i];
}
else {
a[i]=w[i];
v[i]=s[i];
}
}
fore(i,0,n){
F[0][i]=a[i];
V[0][i]={(pot<s[i]?s[i]:INF),v[i]};
// if(a[i]==n)V[0][i].fst=-1;
}
F[0][n]=n;
V[0][n]={-1,0};
fore(k,1,K)fore(x,0,n+1){
F[k][x]=F[k-1][F[k-1][x]];
V[k][x]=oper(V[k-1][x],V[k-1][F[k-1][x]]);
}
// cout<<"bloque "<<pot<<":\n";
// fore(i,0,n)cout<<a[i]<<" ";;cout<<"\n";
// fore(i,0,n)cout<<v[i]<<" ";;cout<<"\n";
// fore(i,0,n)cout<<V[0][i].fst<<","<<V[0][i].snd<<" ";;cout<<"\n";
}
void go(ll &x, ll &z){
// cout<<"go "<<x<<" "<<z<<endl;
assert(z<INF);
ii nod={INF,0};
for(ll k=K-1;k>=0;k--){
auto nodi=oper(nod,V[k][x]);
if(z<nodi.fst&&F[k][x]<n){
// nod=nodi;
z+=nodi.snd;
// cout<<"salto "<<k<<"\n";
// cout<<x<<" -> "<<F[k][x]<<"\n";
// cout<<nod.fst<<","<<nod.snd<<"\n";
assert(z<INF);
x=F[k][x];
}
}
// nod=oper(nod,V[0][x]);
// cout<<"salto final"<<"\n";
// cout<<x<<" -> "<<F[0][x]<<"\n";
// cout<<nod.fst<<","<<nod.snd<<"\n";
// x=F[0][x];
// z+=nod.snd;
// cout<<"ended in "<<x<<","<<z<<endl;
// assert((pot<s[x]&&z>=s[x]));
if((pot>=s[x]||z<s[x])&&a[x]==n){
z+=v[x]; x=a[x];
assert(x==n);
}
else {
assert(pot<s[x]&&z>=s[x]);
z+=s[x]; x=w[x];
}
// cout<<x<<" "<<z<<"\n\n";
}
};
bloque dat[VK];
ll F[K][MAXN2];
ii V[K][MAXN2];
ii oper2(ii a, ii b){
return {max((ll)a.fst,b.fst-a.snd),min(a.snd+b.snd,INF)};
}
void init(int N, vector<int> S, vector<int> P, vector<int> W, vector<int> L){
n=N;
fore(i,0,n){
s[i]=S[i];
p[i]=P[i];
w[i]=W[i];
l[i]=L[i];
}
fore(i,0,n)sub&=s[i]==p[i];
if(!sub){
// assert(0);
fore(i,0,VK)dat[i].init(1ll<<i);
return;
}
// D.resize(n+1);
// for(ll i=n-1;i>=0;i--){
// D[i]=D[w[i]]+s[i];
// sub&=s[i]==p[i];
// }
fore(i,0,n){
F[0][i]=w[i];
V[0][i]={s[i],s[i]};
// if(a[i]==n)V[0][i].fst=-1;
}
F[0][n]=n;
V[0][n]={INF,0};
fore(k,1,K)fore(x,0,n+1){
F[k][x]=F[k-1][F[k-1][x]];
V[k][x]=oper2(V[k-1][x],V[k-1][F[k-1][x]]);
}
// while(!sub);
}
void go(ll &x, ll &z){
for(ll k=K-1;k>=0;k--){
auto nod=V[k][x];
if(z>=nod.fst&&F[k][x]<n){
z+=nod.snd;
x=F[k][x];
}
}
if(z>=s[x]){
z+=s[x]; x=w[x];
while(w[x]!=n);
}
else {
z+=p[x]; x=l[x];
}
}
long long simulate(int X, int Z){
ll x=X,z=Z;
if(sub){
while(x<n)go(x,z);
return z;
}
while(x<n){
ll bl=min(VK-1,ll(63-__builtin_clzll(z)));
dat[bl].go(x,z);
}
return z;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
492628 KB |
Output is correct |
2 |
Correct |
145 ms |
492624 KB |
Output is correct |
3 |
Correct |
138 ms |
498516 KB |
Output is correct |
4 |
Correct |
257 ms |
635728 KB |
Output is correct |
5 |
Correct |
141 ms |
498512 KB |
Output is correct |
6 |
Correct |
278 ms |
635476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
490832 KB |
Output is correct |
2 |
Correct |
330 ms |
757476 KB |
Output is correct |
3 |
Correct |
337 ms |
757328 KB |
Output is correct |
4 |
Correct |
331 ms |
759124 KB |
Output is correct |
5 |
Correct |
329 ms |
758948 KB |
Output is correct |
6 |
Correct |
344 ms |
757332 KB |
Output is correct |
7 |
Correct |
345 ms |
755496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
495444 KB |
Output is correct |
2 |
Correct |
296 ms |
637012 KB |
Output is correct |
3 |
Correct |
319 ms |
637352 KB |
Output is correct |
4 |
Correct |
279 ms |
636496 KB |
Output is correct |
5 |
Correct |
271 ms |
636508 KB |
Output is correct |
6 |
Correct |
409 ms |
636760 KB |
Output is correct |
7 |
Correct |
312 ms |
636756 KB |
Output is correct |
8 |
Correct |
298 ms |
636416 KB |
Output is correct |
9 |
Correct |
280 ms |
636496 KB |
Output is correct |
10 |
Correct |
289 ms |
636240 KB |
Output is correct |
11 |
Correct |
321 ms |
636752 KB |
Output is correct |
12 |
Correct |
395 ms |
636756 KB |
Output is correct |
13 |
Correct |
310 ms |
636756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
495444 KB |
Output is correct |
2 |
Correct |
296 ms |
637012 KB |
Output is correct |
3 |
Correct |
319 ms |
637352 KB |
Output is correct |
4 |
Correct |
279 ms |
636496 KB |
Output is correct |
5 |
Correct |
271 ms |
636508 KB |
Output is correct |
6 |
Correct |
409 ms |
636760 KB |
Output is correct |
7 |
Correct |
312 ms |
636756 KB |
Output is correct |
8 |
Correct |
298 ms |
636416 KB |
Output is correct |
9 |
Correct |
280 ms |
636496 KB |
Output is correct |
10 |
Correct |
289 ms |
636240 KB |
Output is correct |
11 |
Correct |
321 ms |
636752 KB |
Output is correct |
12 |
Correct |
395 ms |
636756 KB |
Output is correct |
13 |
Correct |
310 ms |
636756 KB |
Output is correct |
14 |
Correct |
138 ms |
495444 KB |
Output is correct |
15 |
Correct |
316 ms |
636968 KB |
Output is correct |
16 |
Correct |
313 ms |
637140 KB |
Output is correct |
17 |
Correct |
295 ms |
636556 KB |
Output is correct |
18 |
Correct |
326 ms |
636668 KB |
Output is correct |
19 |
Correct |
339 ms |
636648 KB |
Output is correct |
20 |
Correct |
305 ms |
636540 KB |
Output is correct |
21 |
Correct |
306 ms |
636444 KB |
Output is correct |
22 |
Correct |
306 ms |
636484 KB |
Output is correct |
23 |
Correct |
322 ms |
636756 KB |
Output is correct |
24 |
Correct |
392 ms |
636676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
140 ms |
495444 KB |
Output is correct |
2 |
Correct |
296 ms |
637012 KB |
Output is correct |
3 |
Correct |
319 ms |
637352 KB |
Output is correct |
4 |
Correct |
279 ms |
636496 KB |
Output is correct |
5 |
Correct |
271 ms |
636508 KB |
Output is correct |
6 |
Correct |
409 ms |
636760 KB |
Output is correct |
7 |
Correct |
312 ms |
636756 KB |
Output is correct |
8 |
Correct |
298 ms |
636416 KB |
Output is correct |
9 |
Correct |
280 ms |
636496 KB |
Output is correct |
10 |
Correct |
289 ms |
636240 KB |
Output is correct |
11 |
Correct |
321 ms |
636752 KB |
Output is correct |
12 |
Correct |
395 ms |
636756 KB |
Output is correct |
13 |
Correct |
310 ms |
636756 KB |
Output is correct |
14 |
Correct |
138 ms |
495444 KB |
Output is correct |
15 |
Correct |
316 ms |
636968 KB |
Output is correct |
16 |
Correct |
313 ms |
637140 KB |
Output is correct |
17 |
Correct |
295 ms |
636556 KB |
Output is correct |
18 |
Correct |
326 ms |
636668 KB |
Output is correct |
19 |
Correct |
339 ms |
636648 KB |
Output is correct |
20 |
Correct |
305 ms |
636540 KB |
Output is correct |
21 |
Correct |
306 ms |
636444 KB |
Output is correct |
22 |
Correct |
306 ms |
636484 KB |
Output is correct |
23 |
Correct |
322 ms |
636756 KB |
Output is correct |
24 |
Correct |
392 ms |
636676 KB |
Output is correct |
25 |
Correct |
296 ms |
635988 KB |
Output is correct |
26 |
Correct |
326 ms |
637012 KB |
Output is correct |
27 |
Correct |
301 ms |
636588 KB |
Output is correct |
28 |
Correct |
315 ms |
636556 KB |
Output is correct |
29 |
Correct |
316 ms |
637012 KB |
Output is correct |
30 |
Correct |
314 ms |
636760 KB |
Output is correct |
31 |
Correct |
316 ms |
636492 KB |
Output is correct |
32 |
Correct |
412 ms |
636576 KB |
Output is correct |
33 |
Correct |
418 ms |
636756 KB |
Output is correct |
34 |
Correct |
1104 ms |
636500 KB |
Output is correct |
35 |
Correct |
385 ms |
636532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
490832 KB |
Output is correct |
2 |
Correct |
330 ms |
757476 KB |
Output is correct |
3 |
Correct |
337 ms |
757328 KB |
Output is correct |
4 |
Correct |
331 ms |
759124 KB |
Output is correct |
5 |
Correct |
329 ms |
758948 KB |
Output is correct |
6 |
Correct |
344 ms |
757332 KB |
Output is correct |
7 |
Correct |
345 ms |
755496 KB |
Output is correct |
8 |
Correct |
140 ms |
495444 KB |
Output is correct |
9 |
Correct |
296 ms |
637012 KB |
Output is correct |
10 |
Correct |
319 ms |
637352 KB |
Output is correct |
11 |
Correct |
279 ms |
636496 KB |
Output is correct |
12 |
Correct |
271 ms |
636508 KB |
Output is correct |
13 |
Correct |
409 ms |
636760 KB |
Output is correct |
14 |
Correct |
312 ms |
636756 KB |
Output is correct |
15 |
Correct |
298 ms |
636416 KB |
Output is correct |
16 |
Correct |
280 ms |
636496 KB |
Output is correct |
17 |
Correct |
289 ms |
636240 KB |
Output is correct |
18 |
Correct |
321 ms |
636752 KB |
Output is correct |
19 |
Correct |
395 ms |
636756 KB |
Output is correct |
20 |
Correct |
310 ms |
636756 KB |
Output is correct |
21 |
Correct |
138 ms |
495444 KB |
Output is correct |
22 |
Correct |
316 ms |
636968 KB |
Output is correct |
23 |
Correct |
313 ms |
637140 KB |
Output is correct |
24 |
Correct |
295 ms |
636556 KB |
Output is correct |
25 |
Correct |
326 ms |
636668 KB |
Output is correct |
26 |
Correct |
339 ms |
636648 KB |
Output is correct |
27 |
Correct |
305 ms |
636540 KB |
Output is correct |
28 |
Correct |
306 ms |
636444 KB |
Output is correct |
29 |
Correct |
306 ms |
636484 KB |
Output is correct |
30 |
Correct |
322 ms |
636756 KB |
Output is correct |
31 |
Correct |
392 ms |
636676 KB |
Output is correct |
32 |
Correct |
296 ms |
635988 KB |
Output is correct |
33 |
Correct |
326 ms |
637012 KB |
Output is correct |
34 |
Correct |
301 ms |
636588 KB |
Output is correct |
35 |
Correct |
315 ms |
636556 KB |
Output is correct |
36 |
Correct |
316 ms |
637012 KB |
Output is correct |
37 |
Correct |
314 ms |
636760 KB |
Output is correct |
38 |
Correct |
316 ms |
636492 KB |
Output is correct |
39 |
Correct |
412 ms |
636576 KB |
Output is correct |
40 |
Correct |
418 ms |
636756 KB |
Output is correct |
41 |
Correct |
1104 ms |
636500 KB |
Output is correct |
42 |
Correct |
385 ms |
636532 KB |
Output is correct |
43 |
Correct |
137 ms |
492884 KB |
Output is correct |
44 |
Correct |
142 ms |
495444 KB |
Output is correct |
45 |
Runtime error |
984 ms |
948644 KB |
Execution killed with signal 6 |
46 |
Halted |
0 ms |
0 KB |
- |