제출 #950653

#제출 시각아이디문제언어결과실행 시간메모리
950653Nhoksocqt1Dungeons Game (IOI21_dungeons)C++17
37 / 100
7060 ms149072 KiB
#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 Psum[MAXN][19];
int P[MAXN][19], Ps[MAXN][19], depth[MAXN], nArr;
bool check_sub2;

void init(int _N, vector<int> _S, vector<int> _P, vector<int> _W, vector<int> _L) {
    nArr = _N;
    check_sub2 = 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);
    }

    depth[nArr] = 0;
    P[nArr][0] = -1;
    for (int i = nArr - 1; i >= 0; --i) {
        P[i][0] = dungeons[i].w;
        depth[i] = depth[P[i][0]] + 1;
        Ps[i][0] = Psum[i][0] = dungeons[i].s;
    }

    for (int j = 1; (1 << j) <= nArr + 1; ++j) {
        for (int i = 0; i <= nArr; ++i) {
            if(P[i][j - 1] == -1) {
                P[i][j] = -1;
            } else {
                P[i][j] = P[P[i][j - 1]][j - 1];
                Ps[i][j] = max(Ps[i][j - 1], Ps[P[i][j - 1]][j - 1]);
                Psum[i][j] = Psum[i][j - 1] + Psum[P[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(P[x][i] != -1 && Ps[x][i] <= z) {
                z += Psum[x][i];
                x = P[x][i];
            }
        }

        if(x < nArr && z < dungeons[x].s) {
            z += dungeons[x].p;
            x = dungeons[x].l;
        }
    }

    return z;
}

ll simulate(int x, int z) {
    if(check_sub2)
        return sub2(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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...