제출 #1305771

#제출 시각아이디문제언어결과실행 시간메모리
1305771austinFountain (eJOI20_fountain)C++20
100 / 100
190 ms32768 KiB
#include <bits/stdc++.h>
using namespace std;
struct Etage {
    long long diam, cap;
    Etage() : diam(0), cap(0) {}
    Etage(long long _diam, long long _cap) : diam(_diam), cap(_cap) {}
};
int8_t ok;
int n, q, i, j, dep, high;
long long n1, n2, c;
vector<long long> nxt;
vector<Etage> fountain;
vector<vector<int>> up;
vector<vector<long long>> cap;
stack<long long> s;
int main() {
    scanf("%d %d", &n, &q);
    high = 32 - __builtin_clz(max(n-1, 1));
    nxt.assign(n+1, 0);
    fountain.resize(n+1);
    up.assign(n+1, vector<int>(high+1, 0));
    cap.assign(n+1, vector<long long>(high+1, 0ll));
    for (i=1; i<=n; i++) {
        scanf("%lld %lld", &n1, &n2);
        fountain[i] = {n1, n2};
    }
    for (i=n; i>=1; i--) {
        while (!s.empty() && fountain[s.top()].diam <= fountain[i].diam) s.pop();
        nxt[i] = s.empty() ? 0 : s.top();
        s.emplace(i);
    }
    for (i=1; i<=n; i++) {
        up[i][0] = nxt[i];
        cap[i][0] = (!nxt[i] ? LLONG_MAX : fountain[nxt[i]].cap);
    }
    for (i=1; i<=high; i++) {
        for (j=1; j<=n; j++) {
            if (!up[j][i-1]) {
                up[j][i] = 0;
                cap[j][i] = LLONG_MAX;
            } else {
                up[j][i] = up[up[j][i-1]][i-1];
                cap[j][i] = (cap[j][i-1] >= LLONG_MAX || cap[up[j][i-1]][i-1] >= LLONG_MAX ? LLONG_MAX : cap[j][i-1] + cap[up[j][i-1]][i-1]);
            }
        }
    }
    while (q--) {
        scanf("%d %lld", &dep, &c);
        c -= fountain[dep].cap;
        ok = 0;
        for (i=high; i>=0; i--) {
            if (up[dep][i] !=0 && cap[dep][i] <= c) {
                c-=cap[dep][i];
                dep = up[dep][i];
                if (c <= 0) {
                    printf("%d\n", dep);
                    ok = 1;
                    break;
                }
            }
        }
        if (ok) continue;
        if (c > 0) {
            dep = nxt[dep];
        }
        printf("%d\n", dep);
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

fountain.cpp: In function 'int main()':
fountain.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
fountain.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         scanf("%lld %lld", &n1, &n2);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%d %lld", &dep, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...