답안 #838816

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838816 2023-08-27T19:20:01 Z VMaksimoski008 Fountain (eJOI20_fountain) C++14
0 / 100
196 ms 42572 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int LOG = 20;

vector<vector<int> > up, graph;
vector<vector<ll> > sum;
vector<pii> v;

void dfs(int u, int p) {
    for(int i=1; i<LOG; i++) {
        up[u][i] = up[ up[u][i-1] ][i-1];
        sum[u][i] = 1ll * (sum[u][i-1] + sum[ up[u][i-1] ][i-1]);
    }

    for(int &next : graph[u]) {
        if(next == p) continue;
         up[next][0] = u;
         sum[next][0] = v[u].second;
         dfs(next, u);
    }
}

int jmp(int a, int b) {
    int currNode = a;
    int currLeft = b;

    currLeft -= v[a].second;

    for(int i=0; i<LOG; i++) {
        if(sum[currNode][i] <= currLeft) {
            currLeft -= sum[currNode][i];
            currNode = up[currNode][i];
        }
    }

    return up[currNode][0];
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q;
    cin >> n >> q;
    graph.resize(n+1);
    up.resize(n+1, vector<int>(LOG, 0));
    sum.resize(n+1, vector<ll>(LOG, 0));

    v.resize(n+1);
    v[0] = {1500000000, 1500000000};
    for(int i=1; i<=n; i++) {
        cin >> v[i].first >> v[i].second;
    }

    stack<pii> curr;
    for(int i=1; i<=n; i++) {
        while(!curr.empty() && curr.top().second < v[i].first) {
            graph[i].push_back(curr.top().first);
            graph[curr.top().first].push_back(i);
            curr.pop();
        }
        curr.push({ i, v[i].first });
    }

    while(!curr.empty()) {
        graph[0].push_back(curr.top().first);
        graph[curr.top().first].push_back(0);
        curr.pop();
    }

    dfs(0, 0);

    while(q--) {
        int a, b;
        cin >> a >> b;

        if(v[a].second >= b) {
            cout << a << '\n';
            continue;
        }

        cout << jmp(a, b) << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 196 ms 42572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -