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;
 
long long N, S[400005], P[400005], W[400005], L[400005];
vector<long long> vals;
pair<long long,long long> par[7][400005][25];
 
void init(int n, vector<int> s, vector<int> p, vector<int> w, vector<int> l) {
	N = n;
	for (long long i = 0; i < N; ++i)
		S[i] = s[i], P[i] = p[i], W[i] = w[i], L[i] = l[i], vals.push_back(S[i]);
	sort(vals.begin(),vals.end());
	vals.resize(unique(vals.begin(),vals.end()) - vals.begin());
	vals.push_back(4e18);
	for (long long i = 0; i < vals.size(); ++i) {
		par[i][N][0] = make_pair(-1,-1);
		for (long long j = 0; j < N; ++j) {
			if (vals[i] > S[j]) par[i][j][0] = make_pair(W[j],S[j]);
			else par[i][j][0] = make_pair(L[j],P[j]);
		}
		for (long long k = 1; k < 25; ++k) for (long long j = 0; j <= N; ++j) {
			if (par[i][j][k-1] == make_pair(-1ll,-1ll)) par[i][j][k] = make_pair(-1,-1);
			else {
				par[i][j][k].first = par[i][par[i][j][k-1].first][k-1].first;
				if (par[i][j][k].first == -1) par[i][j][k].second = -1;
				else par[i][j][k].second = par[i][j][k-1].second + par[i][par[i][j][k-1].first][k-1].second;
			}
		}
	}
}
 
long long simulate(int x, int z) {
	long long ans = z;
	for (long long i = 0; i < vals.size(); ++i) {
		for (long long k = 24; k >= 0; --k)
			if (par[i][x][k] != make_pair(-1ll,-1ll) && ans+par[i][x][k].second < vals[i])
				ans += par[i][x][k].second, x = par[i][x][k].first;
		if (x == N) return ans;
		if (ans < vals[i]) ans += par[i][x][0].second, x = par[i][x][0].first;
	}
	return ans;
}
Compilation message (stderr)
dungeons.cpp: In function 'void init(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
dungeons.cpp:16:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for (long long i = 0; i < vals.size(); ++i) {
      |                        ~~^~~~~~~~~~~~~
dungeons.cpp: In function 'long long int simulate(int, int)':
dungeons.cpp:35:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (long long i = 0; i < vals.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... |