#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
vector<vector<pair<int, int>>> adj;
vector<vector<pair<int, int>>> par;
void dfs(int v) {
//cerr << v << " -- v start\n";
for (auto [u, ci] : adj[v]) {
par[u][0] = {v, ci};
dfs(u);
}
//cerr << v << " -- v end\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
int log_n = __lg(n-1) + 1;
//cerr << log_n;
vector<pair<int, int>> resv; // di, ci
for (int i=0; i<n; i++) {
int di, ci;
cin >> di >> ci;
resv.push_back({di, ci});
}
resv.push_back({INF, INF}); // Add the waterway
reverse(resv.begin(), resv.end());
adj.assign(n+1, {});
vector<int> st;
st.push_back(0);
for (int i=1; i<=n; i++) {
while (resv[st.back()].first <= resv[i].first) {
//cerr << st.back() << "\n";
st.pop_back();
}
adj[st.back()].push_back({i, resv[i].second});
//adj[i].push_back({st.back(), resv[i].second});
st.push_back(i);
}
//cerr << "run1\n";
// Compute binary lift
par.assign(n+1, vector<pair<int, int>>(log_n+1, {-1, INF}));
dfs(0);
//cerr << "run2\n";
for (int k=1; k<=log_n; k++) {
for (int i=1; i<=n; i++) {
if (par[i][k-1].first < 0) { continue; }
par[i][k] = {par[par[i][k-1].first][k-1].first, par[i][k-1].second + par[par[i][k-1].first][k-1].second};
//cerr << par[i][k].first << " ";
}
//cerr << "\n";
}
//cerr << "run3\n";
//return 0;
for (int i=0; i<q; i++) {
int ri, vi;
cin >> ri >> vi;
//cerr << "run0" << "\n";
int lvl = n - ri + 1;
int vol_sum = 0;
while (vol_sum+par[lvl][0].second < vi) {
//cerr << vol_sum+par[lvl][0].second << " -- dbg2\n";
int pow = 1;
while (pow-1 <= log_n && vol_sum+par[lvl][pow-1].second < vi) {
pow++;
}
vol_sum += par[lvl][pow-2].second;
lvl = par[lvl][pow-2].first;
//cerr << vol_sum+par[lvl][0].second << " -- dbg\n";
}
//cerr << vol_sum << "\n";
//cerr << "calc end\n";
if (lvl == 0) {
cout << 0 << "\n";
} else {
cout << n - lvl + 1 << "\n";
}
}
}
Compilation message
fountain.cpp: In function 'void dfs(long long int)':
fountain.cpp:13:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for (auto [u, ci] : adj[v]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
43728 KB |
Output is correct |
2 |
Correct |
353 ms |
43248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
333 ms |
43728 KB |
Output is correct |
9 |
Correct |
353 ms |
43248 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
122 ms |
23756 KB |
Output is correct |
12 |
Correct |
509 ms |
50196 KB |
Output is correct |
13 |
Correct |
320 ms |
44740 KB |
Output is correct |
14 |
Correct |
196 ms |
43008 KB |
Output is correct |
15 |
Correct |
177 ms |
42412 KB |
Output is correct |