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>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n;
vector<pair<ll, ll>> G, W;
vl dp;
const int LOG = 25;
vector<vvi> up;
vector<vvl> sum;
vi diffS;
ll getDP(int i) {
if (dp[i] != -1) return dp[i];
if (i == n) {
dp[i] = 0;
return 0;
}
dp[i] = W[i].first+getDP(G[i].first);
return dp[i];
}
pll dist(int x, int d, int j) {
ll strength = 0;
for (int i = LOG-1; i >= 0; i--) {
if ((d>>i)&1) {
strength += sum[j][x][i];
x = up[j][x][i];
}
}
return {x, strength};
}
void init(int nx, vi s, vi p, vi w, vi l) {
n = nx;
G = vector<pll>(n);
W = vector<pll>(n);
for (int i = 0; i < n; i++) {
G[i] = {w[i], l[i]};
W[i] = {s[i], p[i]};
}
set<int> tmp;
for (int i = 0; i < n; i++) tmp.insert(s[i]);
for (auto it = tmp.begin(); it != tmp.end(); it++) {
diffS.push_back(*it);
}
if (diffS.size() > 5) return;
dp = vl(n+1, -1);
for (int i = 0; i <= n; i++) {
dp[i] = getDP(i);
}
up = vector<vvi>(diffS.size(), vvi(n+1, vi(LOG)));
sum = vector<vvl>(diffS.size(), vvl(n+1, vl(LOG)));
ll curr = 0;
for (int j = 0; j < diffS.size(); j++) {
up[j][n][0] = n;
sum[j][n][0] = 0;
for (int i = 0; i < n; i++) {
if (W[i].first <= curr) {
up[j][i][0] = G[i].first;
sum[j][i][0] = W[i].first;
} else {
up[j][i][0] = G[i].second;
sum[j][i][0] = W[i].second;
}
}
for (int i = 1; i < LOG; i++) {
for (int v = 0; v <= n; v++) {
sum[j][v][i] = sum[j][v][i-1] + sum[j][up[j][v][i-1]][i-1];
up[j][v][i] = up[j][up[j][v][i-1]][i-1];
}
}
curr = diffS[j];
}
}
ll simulate(int x, int z) {
if (diffS.size() > 5) {
int n = G.size();
ll strength = z;
for (int i = x; i != n; ) {
if (strength >= W[i].first) {
strength += W[i].first;
i = G[i].first;
} else {
strength += W[i].second;
i = G[i].second;
}
}
return strength;
}
if (z >= diffS[diffS.size()-1]) {
return z + dp[x];
}
int start = 0;
for (int i = 0; i < diffS.size(); i++) {
if (z >= diffS[i]) start = i+1;
}
ll strength = z;
int res;
for (int i = start; i < diffS.size(); i++) {
int lo = 0, hi = 2e7, mid;
res = 2e7;
while (lo <= hi) {
mid = (lo+hi)/2;
if (dist(x, mid, i).second+strength >= diffS[i]) {
res = mid;
hi = mid -1;
} else
lo = mid + 1;
}
strength += dist(x, res, i).second;
x = dist(x, res, i).first;
}
return strength+dp[x];
}
Compilation message (stderr)
dungeons.cpp: In function 'void init(int, vi, vi, vi, vi)':
dungeons.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j = 0; j < diffS.size(); j++) {
| ~~^~~~~~~~~~~~~~
dungeons.cpp: In function 'll simulate(int, int)':
dungeons.cpp:118:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (int i = 0; i < diffS.size(); i++) {
| ~~^~~~~~~~~~~~~~
dungeons.cpp:124:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = start; i < diffS.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... |