Submission #467187

# Submission time Handle Problem Language Result Execution time Memory
467187 2021-08-21T23:23:22 Z Mamedov Fountain (eJOI20_fountain) C++17
100 / 100
331 ms 20432 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ui unsigned int
#define pii pair<int, int>
#define pis pair<int, string>
#define piii pair<int, pii>
#define pb push_back
#define f first
#define s second
#define oo 2000000002

using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int up = 1e5 + 5;
const int lg = 18;
int D[up], C[up];
int sum[up][lg], par[up][lg];

void buildST(int n) {
    stack<int>st;
    D[0] = oo;
    st.push(0);
    for(int i = n; i >= 1; --i) {
        while(D[st.top()] <= D[i]) {
            st.pop();
        }
        par[i][0] = st.top();
        st.push(i);
    }
    for(int i = 0; i <= n; ++i) {
        sum[i][0] = C[i];
    }
    for(int j = 1; j < lg; ++j) {
        for(int i = 1; i <= n; ++i) {
            par[i][j] = par[par[i][j - 1]][j - 1];
            sum[i][j] = sum[i][j - 1] + sum[par[i][j - 1]][j - 1];
        }
    }
}

int query(int id, int vol) {
    for(int i = lg - 1; i >= 0 && id; --i) {
        if(sum[id][i] < vol) {
            vol -= sum[id][i];
            id = par[id][i];
        }
    }
    return id;
}
void solve() {
    int n, q;
    scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; ++i) {
        scanf("%d%d", D + i, C + i);
    }
    buildST(n);
    int id, vol;
    while(q--) {
        scanf("%d%d", &id, &vol);
        printf("%d\n", query(id, vol));
    }
}
int main() {
    solve();
}

Compilation message

fountain.cpp: In function 'void solve()':
fountain.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
fountain.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         scanf("%d%d", D + i, C + i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d%d", &id, &vol);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 15332 KB Output is correct
2 Correct 196 ms 14204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 192 ms 15332 KB Output is correct
9 Correct 196 ms 14204 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 80 ms 10820 KB Output is correct
12 Correct 331 ms 20432 KB Output is correct
13 Correct 206 ms 20024 KB Output is correct
14 Correct 188 ms 19200 KB Output is correct
15 Correct 138 ms 19520 KB Output is correct