Submission #467503

#TimeUsernameProblemLanguageResultExecution timeMemory
467503shmadFountain (eJOI20_fountain)C++17
100 / 100
392 ms39940 KiB


#include <bits/stdc++.h>
 
#define nl '\n'
#define pb push_back
#define E exit(0)
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define sz(s) (int)(s).size()
#define ll long long
#define int ll
#define oioi ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define vt vector
#define dbg(x) cerr << #x << " = " << x << nl

using namespace std;
using pii = pair<int, int>;
using vvi = vt< vt<int> >;
 
const int N = 1e6 + 5;
const int INF = 1e18 + 7;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const int B = 800;

int n, q, up[N][21], wtr[N][21], diam[N];

int get (int a, int b) {
    for (int i = 20; i >= 0; i--) {
        if (wtr[a][i] < b && wtr[a][i]) b -= wtr[a][i], a = up[a][i];
    }
    return a;
}

void solve () {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> diam[i] >> wtr[i][0];
    stack<int> s;
    s.push(0);
    diam[0] = INF;
    for (int i = n; i >= 1; i--) {
        while (diam[s.top()] <= diam[i]) s.pop();
        up[i][0] = s.top();
        s.push(i);
    }
    up[0][0] = wtr[0][0] = 0;
    for (int j = 1; j <= 20; j++) {
        for (int i = 1; i <= n; i++) {
            int jump = up[i][j - 1];
            up[i][j] = up[jump][j - 1];
            wtr[i][j] = wtr[i][j - 1] + wtr[jump][j - 1];
        }
    }
    while (q--) {
        int r, v;
        cin >> r >> v;
        cout << get(r, v) << nl;
    }
    cout << nl;
}

int test = 1;
 
signed main () {
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
    oioi
//  cin >> test; 
    while (test--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...