Submission #818742

#TimeUsernameProblemLanguageResultExecution timeMemory
818742becaido던전 (IOI21_dungeons)C++17
37 / 100
7041 ms222132 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

#define lpos pos*2
#define rpos pos*2+1

const ll INF = 1e18;
const int SIZE = 4e5 + 5;
const int H = __lg((int) 1e7) + 1;

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

void init(int _n, vector<int> _s, vector<int> _p, vector<int> _w, vector<int> _l) {
    n = _n;
    FOR (i, 0, n - 1) tie(s[i], p[i], w[i], l[i]) = make_tuple(_s[i], _p[i], _w[i], _l[i]);
    w[n] = l[n] = to[n][0] = n;
    FOR (i, 0, n - 1) {
        to[i][0] = w[i];
        mn[i][0] = -s[i];
        sum[i][0] = s[i];
    }
    FOR (j, 1, H) FOR (i, 0, n) {
        int np = to[i][j - 1];
        to[i][j] = to[np][j - 1];
        mn[i][j] = min(mn[i][j - 1], sum[i][j - 1] + mn[np][j - 1]);
        sum[i][j] = sum[i][j - 1] + sum[np][j - 1];
    }
}

ll simulate(int pos, int z) {
    ll ans = z;
    while (pos != n) {
        for (int i = H; i >= 0; i--) if (to[pos][i] != n && ans + mn[pos][i] >= 0) {
            ans += sum[pos][i];
            pos = to[pos][i];
        }
        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...