#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) {}
};
int n, q, i, j, dep, high;
long long res, 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;
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);
continue;
}
}
}
if (c > 0) {
dep = nxt[dep];
}
printf("%d\n", dep);
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
fountain.cpp: In function 'int main()':
fountain.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
fountain.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%lld %lld", &n1, &n2);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d %lld", &dep, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |