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;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 400005;
struct Dungeon {
int s, p, w, l;
} dungeons[MAXN];
ll Psums[MAXN][24], Psump[MAXN][24];
int Pl[MAXN][24], Psi[MAXN][24];
int Pw[MAXN][24], Psa[MAXN][24], depth[MAXN], nArr;
bool check_sub2, check_sub3;
void init(int _N, vector<int> _S, vector<int> _P, vector<int> _W, vector<int> _L) {
nArr = _N;
check_sub2 = check_sub3 = 1;
for (int i = 0; i < nArr; ++i) {
dungeons[i] = {_S[i], _P[i], _W[i], _L[i]};
check_sub2 &= (dungeons[i].s == dungeons[i].p);
check_sub3 &= (dungeons[i].s == dungeons[0].s);
}
depth[nArr] = 0;
Pw[nArr][0] = Pl[nArr][0] = -1;
for (int i = nArr - 1; i >= 0; --i) {
Pw[i][0] = dungeons[i].w;
Pl[i][0] = dungeons[i].l;
depth[i] = depth[Pw[i][0]] + 1;
Psi[i][0] = Psa[i][0] = Psums[i][0] = dungeons[i].s;
Psump[i][0] = dungeons[i].p;
}
for (int j = 1; j < 24; ++j) {
for (int i = 0; i <= nArr; ++i) {
if(Pl[i][j - 1] == -1) {
Pl[i][j] = -1;
} else {
Pl[i][j] = Pl[Pl[i][j - 1]][j - 1];
Psi[i][j] = min(Psi[i][j - 1], Psi[Pl[i][j - 1]][j - 1]);
Psump[i][j] = Psump[i][j - 1] + Psump[Pl[i][j - 1]][j - 1];
}
if(Pw[i][j - 1] == -1) {
Pw[i][j] = -1;
} else {
Pw[i][j] = Pw[Pw[i][j - 1]][j - 1];
Psa[i][j] = max(Psa[i][j - 1], Psa[Pw[i][j - 1]][j - 1]);
Psums[i][j] = Psums[i][j - 1] + Psums[Pw[i][j - 1]][j - 1];
}
}
}
}
ll sub1(int x, ll z) {
while(x < nArr) {
if(z >= dungeons[x].s) {
z += dungeons[x].s;
x = dungeons[x].w;
} else {
z += dungeons[x].p;
x = dungeons[x].l;
}
}
return z;
}
// z < s[i] do not exceed log2(max(s[i])) time
ll sub2(int x, ll z) {
while(x < nArr) {
for (int i = 31 - __builtin_clz(depth[x]); i >= 0; --i) {
if(Pw[x][i] != -1 && Psa[x][i] <= z) {
z += Psums[x][i];
x = Pw[x][i];
}
}
if(x < nArr && z < dungeons[x].s) {
z += dungeons[x].p;
x = dungeons[x].l;
}
}
return z;
}
// lose all until z >= s[.]
ll sub3(int x, ll z) {
for (int i = 23; i >= 0; --i) {
if(Pl[x][i] != -1 && z + Psump[x][i] < dungeons[0].s) {
z += Psump[x][i];
x = Pl[x][i];
}
}
if(x < nArr && z < dungeons[0].s) {
z += Psump[x][0];
x = Pl[x][0];
}
assert(z >= dungeons[0].s);
if(x < nArr) {
for (int i = 31 - __builtin_clz(depth[x]); i >= 0; --i) {
if(Pw[x][i] != -1) {
z += Psums[x][i];
x = Pw[x][i];
}
}
}
return z;
}
ll simulate(int x, int z) {
if(check_sub2)
return sub2(x, z);
if(check_sub3)
return sub3(x, z);
return sub1(x, z);
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "dungeons"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _S, _P, _W, _L;
int nArr, numQuery;
cin >> nArr >> numQuery;
_S.resize(nArr), _P.resize(nArr), _W.resize(nArr), _L.resize(nArr);
for (int i = 0; i < nArr; ++i)
cin >> _S[i] >> _P[i] >> _W[i] >> _L[i];
init(nArr, _S, _P, _W, _L);
for (int t = 0; t < numQuery; ++t) {
int x, z;
cin >> x >> z;
cout << "ANSWER FOR SIMULATE(" << x << "," << z << ") = " << simulate(x, z) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# | 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... |