제출 #819503

#제출 시각아이디문제언어결과실행 시간메모리
819503becaido던전 (IOI21_dungeons)C++17
100 / 100
5823 ms1083516 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "dungeons.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const ll INF = 1e18;
const int SIZE = 4e5 + 5;
const int K = 16;
const int H1 = log(1e7) / log(K) + 1;
const int H2 = __lg((int) 1e7);

int n;
int s[SIZE], p[SIZE], w[SIZE], l[SIZE];
int to[H1 + 1][H2 + 1][SIZE];
int mn[H1 + 1][H2 + 1][SIZE];
ll sum[H1 + 1][H2 + 1][SIZE];

int power(int d, int up) {
    int re = 1;
    while (up) {
        if (up & 1) re *= d;
        d *= d;
        up >>= 1;
    }
    return re;
}

void init(int _n, vector<int> _s, vector<int> _p, vector<int> _w, vector<int> _l) {
    n = _n;
    FOR (i, 0, n - 1) s[i] = _s[i], p[i] = _p[i], w[i] = _w[i], l[i] = _l[i];
    w[n] = l[n] = n;
    FOR (h, 0, H1) {
        int L = power(K, h), R = L * K;
        FOR (i, 0, n - 1) {
            if (s[i] < L) {
                to[h][0][i] = w[i];
                sum[h][0][i] = s[i];
                mn[h][0][i] = R;
            } else {
                to[h][0][i] = l[i];
                sum[h][0][i] = p[i];
                mn[h][0][i] = s[i];
            }
        }
        to[h][0][n] = n;
        FOR (j, 1, H2) FOR (i, 1, n) {
            int np = to[h][j - 1][i];
            to[h][j][i] = to[h][j - 1][np];
            sum[h][j][i] = min(INF, sum[h][j - 1][i] + sum[h][j - 1][np]);
            mn[h][j][i] = min(mn[h][j - 1][i], mn[h][j - 1][np]);
        }
    }
}

ll simulate(int pos, int z) {
    ll ans = z;
    int h = 0, pro = 1;
    while (pos != n) {
        while (h < H1 && pro * K <= ans) h++, pro *= K;
        if (h >= H1) {
            h = H1;
            for (int i = H2; i >= 0; i--) if (to[h][i][pos] != n) {
                ans += sum[h][i][pos];
                pos = to[h][i][pos];
            }
            ans += sum[h][0][pos];
            pos = to[h][0][pos];
            continue;
        }
        int L = power(K, h), R = L * K;
        for (int i = H2; i >= 0; i--) if (ans + sum[h][i][pos] < min(R, mn[h][i][pos])) {
            ans += sum[h][i][pos];
            pos = to[h][i][pos];
        }
        if (ans >= s[pos]) {
            ans += s[pos];
            pos = w[pos];
        } else {
            ans += p[pos];
            pos = l[pos];
        }
    }
    return ans;
}

/*
in1
3 2
2 6 9
3 1 2
2 2 3
1 0 1
0 1
2 3
out1
24
25
*/

#ifdef WAIMAI
int main() {
    int n, q;
    vector<int> s, p, z;
    vector<int> w, l, x;
    vector<ll> answer;
    assert(scanf("%d %d", &n, &q) == 2);
    s.resize(n);
    p.resize(n);
    w.resize(n);
    l.resize(n);
    x.resize(q);
    z.resize(q);
    answer.resize(q);

    for (int i = 0; i < n; i++) {
        assert(scanf("%d", &s[i]) == 1);
    }
    for (int i = 0; i < n; i++) {
        assert(scanf("%d", &p[i]) == 1);
    }
    for (int i = 0; i < n; i++) {
        assert(scanf("%d", &w[i]) == 1);
    }
    for (int i = 0; i < n; i++) {
        assert(scanf("%d", &l[i]) == 1);
    }


    init(n, s, p, w, l);

    for (int i = 0; i < q; i++) {
        assert(scanf("%d %d", &x[i], &z[i]) == 2);
        answer[i] = simulate(x[i], z[i]);
    }
    fclose(stdin);

    for (int i = 0; i < q; i++) {
        printf("%lld\n", answer[i]);
    }
    fclose(stdout);
    return 0;
}
#endif
#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...