제출 #444641

#제출 시각아이디문제언어결과실행 시간메모리
444641shrimbFountain (eJOI20_fountain)C++17
0 / 100
262 ms49516 KiB
#include"bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
int N, Left, Right, Value;
int seg[2000001];

int Query (int l = 1, int r = N, int ind = 1) {
    if (l > Right || r < Left) return INT_MAX;
    if (l >= Left and r <= Right) return seg[ind];
    int mid = (l + r) / 2;
    return min(Query(l, mid, ind * 2), Query(mid + 1, r, ind * 2 + 1));
}

void Update () {
    int ind = Left + N - 2;
    seg[ind] = Value;
    ind /= 2;
    while (ind) {
        seg[ind] = min(seg[ind * 2], seg[ind * 2 + 1]);
        ind /= 2;
    }
}

pair<int,int> sparse[100001][20]; // {node, capacity}

signed main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    N = exp2(ceil(log2(n + 1)));
    for (int& i : seg) i = INT_MAX;
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 0 ; j < 20 ; j++) sparse[i][j] = {-1, INT_MAX};
    }
    pair<int,int> arr[n + 1];
    vector<pair<int,int>> v; // {radius, index}
    vector<int> E;
    set<int> s;
    for (int i = 1 ; i <= n ; i++) {
        cin >> arr[i].first >> arr[i].second;
        v.push_back({arr[i].first, i});
    }
    sort(v.begin(), v.end());
    int ind = 1;

    for (int i = 0 ; i < n ; i++) {
        if (i and v[i].first != v[i-1].first) ind++;
        arr[v[i].second].first = ind;
    }
    Right = N;
    for (int i = n ; i > 0 ; i--) {
        Left = arr[i].first + 1;
        sparse[i][0] = {Query(), arr[i].second};
        if (sparse[i][0].first == INT_MAX) sparse[i][0].first = -1;
        Value = i;
        Update();
    }

    for (int i = n ; i >= 1 ; i--) {
        for (int j = 1 ; sparse[i][j-1].first != -1 ; j++) {
            sparse[i][j] = {sparse[sparse[i][j-1].first][j-1].first, sparse[i][j-1].second + sparse[sparse[i][j-1].first][j-1].second};
        }
    }

    while (q--) {
        int s, x;
        cin >> s >> x;
        for (int i = 19 ; i >= 0 ; i--) {
            if (sparse[s][i].second < x) {
                s = sparse[s][i].first;
                x -= sparse[s][i].second;
            }
        }
        cout << (s == -1 ? 0 : s) << endl;
    }
}
//    5   4
// 1 -> 2 -> 3
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...