이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e9;
using vi = vector<int>;
using ai = array<ll, 2>;
#define sz(v) ((int)v.size())
#define pb push_back
#define pry cout << "YES\n"
#define prn cout << "NO\n"
#define endl '\n'
#define fst first
#define scn second
/* #define ONPC */
const int K = 27;
vector<array<array<ll, 3>, 27>> j;
vector<array<ai, 27>> f;
vector<ll> d;
vi s, p, w, l;
int n;
void
init(int _n, vi _s, vi _p, vi _w, vi _l)
{
n = _n;
s = _s;
p = _p;
w = _w;
l = _l;
j.assign(n+1,{{}});
f.assign(n+1,{{}});
d.assign(n+1, 0);
s.pb(0);
p.pb(0);
l.pb(n);
w.pb(n);
f[n][0] = {n,0};
j[n][0] = {n,0,0};
for (int i = n-1; i >= 0; --i) {
d[i] = d[w[i]] + s[i];
f[i][0] = {w[i], d[i]+s[i]};
j[i][0] = {l[i], p[i], s[l[i]] - p[i]};
}
for (int k = 1; k < K; ++k) {
for (int i = 0; i <= n; ++i) {
f[i][k][0] = f[f[i][k-1][0]][k-1][0];
f[i][k][1] = max(f[i][k-1][1], f[f[i][k-1][0]][k-1][1]);
j[i][k][0] = j[j[i][k-1][0]][k-1][0];
j[i][k][1] = j[i][k-1][1] + j[j[i][k-1][0]][k-1][1];
j[i][k][2] = min(j[i][k-1][2], j[j[i][k-1][0]][k-1][2]-j[i][k-1][1]);
}
}
return;
}
ll
simulate(int x, int _z)
{
ll z = _z;
while (x < n) {
/* cout << x << ' ' << z << ' ' << s[x] << endl; */
if (z >= s[x]) {
for (int k = K-1; k >= 0; --k) {
if (z + d[x] >= f[x][k][1]) {
z += d[x];
x = f[x][k][0];
z -= d[x];
}
}
/* z += s[x]; */
/* x = w[x]; */
} else {
for (int k = K-1; k >= 0; --k) {
if (z < j[x][k][2])
{
z += j[x][k][1];
x = j[x][k][0];
}
}
z += p[x];
x = l[x];
}
}
assert(x == n);
return z;
}
#ifdef ONPC
void
solve()
{
int n, q;
cin >> n >> q;
vector<int> s(n), p(n), w(n) ,l(n);
for (int i = 0; i < n; ++i) cin >> s[i];
for (int i = 0; i < n; ++i) cin >> p[i];
for (int i = 0; i < n; ++i) cin >> w[i];
for (int i = 0; i < n; ++i) cin >> l[i];
init(n,s,p,w,l);
int z, x;
while (q--) {
cin >> x >> z;
cout << simulate(x,z) << endl;
}
}
int32_t
main(int argc, char **argv)
{
if (argc >= 2) {
freopen(argv[1], "r", stdin);
} else
ios_base::sync_with_stdio(0);cin.tie(0);
int t = 1;
/* cin >> t; */
while(t--)
solve();
}
#endif
# | 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... |