Submission #442364

#TimeUsernameProblemLanguageResultExecution timeMemory
442364SorahISADungeons Game (IOI21_dungeons)C++17
13 / 100
3627 ms1287128 KiB
#include "dungeons.h"
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int long long
// #define double long double
using pii = pair<int, int>;
template<typename T>
using Prior = std::priority_queue<T>;
template<typename T>
using prior = std::priority_queue<T, vector<T>, greater<T>>;

#define X first
#define Y second
#define eb emplace_back
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)

namespace {

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

// const int maxn = 4E5 + 5;
const int maxn = 5E4 + 5;
const int lgmx = 24 + 1;
const int INF = 1E13;

int N;
vector<int> S, P, W, L;

int anc[lgmx][lgmx][maxn], sum[lgmx][lgmx][maxn], val[lgmx][lgmx][maxn];

} /// end of namespace

void init(int32_t _N, vector<int32_t> _S, vector<int32_t> _P, vector<int32_t> _W, vector<int32_t> _L) {
    N = _N;
    for (auto &x : _S) S.eb(x); S.eb(0);
    for (auto &x : _P) P.eb(x); P.eb(0);
    for (auto &x : _W) W.eb(x); W.eb(N);
    for (auto &x : _L) L.eb(x); L.eb(N);
    
    memset(anc, 0x00, sizeof(anc));
    memset(sum, 0x00, sizeof(sum));
    memset(val, 0x00, sizeof(val));
    
    for (int lvl = 0; lvl < lgmx; ++lvl) {
        /// Assume you have strength \ge 2^{lvl} ///
        for (int i = 0; i < N; ++i) {
            anc[lvl][0][i] = S[i] <= (1LL << lvl) ? W[i] : L[i];
            sum[lvl][0][i] = S[i] <= (1LL << lvl) ? S[i] : P[i];
            val[lvl][0][i] = S[i] <= (1LL << lvl) ? INF  : S[i];
        }
        anc[lvl][0][N] = N;
    }
    
    for (int lvl = 0; lvl < lgmx; ++lvl) {
        for (int lay = 1; lay < lgmx; ++lay) {
            for (int i = 0; i <= N; ++i) {
                int par = anc[lvl][lay-1][i];
                anc[lvl][lay][i] = anc[lvl][lay-1][par];
                sum[lvl][lay][i] = sum[lvl][lay-1][i] + sum[lvl][lay-1][par];
                val[lvl][lay][i] = min(val[lvl][lay-1][i], val[lvl][lay-1][par]);
            }
        }
    }
}

int simulate(int32_t now, int32_t _Z) {
    int Z = _Z;
    for (int lvl = 0; lvl < lgmx and now != N; ++lvl) {
        if (Z >= (1LL << lvl+1)) continue;
        for (int lay = lgmx-1; lay >= 0 and now != N; --lay) {
            if (Z + sum[lvl][lay][now] < min(val[lvl][lay][now], 1LL << lvl+1)) {
                Z += sum[lvl][lay][now];
                now = anc[lvl][lay][now];
            }
            // cerr << "(lvl, Z, now): " << lvl << "," << Z << "," << now << "\n";
        }
        if (Z >= S[now]) Z += S[now], now = W[now];
        else             Z += P[now], now = L[now];
        // cerr << "(lvl, Z, now): " << lvl << "," << Z << "," << now << "\n";
    }
    return Z + sum[lgmx-1][lgmx-1][now];
}

Compilation message (stderr)

dungeons.cpp: In function 'void init(int32_t, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
dungeons.cpp:40:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   40 |     for (auto &x : _S) S.eb(x); S.eb(0);
      |     ^~~
dungeons.cpp:40:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   40 |     for (auto &x : _S) S.eb(x); S.eb(0);
      |                                 ^
dungeons.cpp:41:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   41 |     for (auto &x : _P) P.eb(x); P.eb(0);
      |     ^~~
dungeons.cpp:41:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   41 |     for (auto &x : _P) P.eb(x); P.eb(0);
      |                                 ^
dungeons.cpp:42:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   42 |     for (auto &x : _W) W.eb(x); W.eb(N);
      |     ^~~
dungeons.cpp:42:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   42 |     for (auto &x : _W) W.eb(x); W.eb(N);
      |                                 ^
dungeons.cpp:43:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   43 |     for (auto &x : _L) L.eb(x); L.eb(N);
      |     ^~~
dungeons.cpp:43:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   43 |     for (auto &x : _L) L.eb(x); L.eb(N);
      |                                 ^
dungeons.cpp: In function 'long long int simulate(int32_t, int32_t)':
dungeons.cpp:74:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   74 |         if (Z >= (1LL << lvl+1)) continue;
      |                          ~~~^~
dungeons.cpp:76:76: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   76 |             if (Z + sum[lvl][lay][now] < min(val[lvl][lay][now], 1LL << lvl+1)) {
      |                                                                         ~~~^~
#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...