Submission #1178700

#TimeUsernameProblemLanguageResultExecution timeMemory
1178700CrabCNHFountain (eJOI20_fountain)C++20
100 / 100
270 ms47752 KiB
#include <bits/stdc++.h>

#define task   "BriantheCrab"

#define int    long long
#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())

using namespace std;

template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}

const int maxN = 2e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
const int LOG = 20;

int n;
int d[maxN], c[maxN];
int up[maxN][LOG], sum[maxN][LOG];
vector <pii> nxt[maxN];

void dfs (int u) {
    for (auto [v, w] : nxt[u]) {
        up[v][0] = u;
        sum[v][0] = w;
        for (int j = 1; j < LOG; j ++) {
            up[v][j] = up[up[v][j - 1]][j - 1];
            sum[v][j] = sum[v][j - 1] + sum[up[v][j - 1]][j - 1]; 
        }
        dfs (v);
    }
}


void solve () {
    int q;
    cin >> n >> q;
    for (int i = 1; i <= n; i ++) {
        cin >> d[i] >> c[i];
    }
    stack <int> st;
    for (int i = n; i >= 1; i --) {
        while (!st.empty () && d[st.top ()] <= d[i]) {
            st.pop ();
        }
        nxt[st.empty () ? 0 : st.top ()].push_back ({i, c[i]});
        //cout << (st.empty () ? 0 : st.top ()) << ' ' << i << '\n';
        st.push (i);
    }
    dfs (0);
    //cout << "debug " << up[4][0] << '\n';
    while (q --) {
        int u, w;
        cin >> u >> w;
        for (int i = LOG - 1; i >= 0; i --) {
            if (w > sum[u][i]) {
                w -= sum[u][i];
                u = up[u][i];
                //cout << u << '\n';
            }
        }
        cout << u << '\n';
    }
    return;
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        solve ();
    } 
    return 0;
}
// thfdgb 

Compilation message (stderr)

fountain.cpp: In function 'int main()':
fountain.cpp:75:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:76:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...