#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<vector<pair<int, int>>> par;
void dfs(int v, int parent=-1) {
//cerr << v << " -- v start\n";
for (auto [u, ci] : adj[v]) {
if (u == parent) { continue; }
par[u][0] = {v, ci};
dfs(u, v);
}
//cerr << v << " -- v end\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
const int INF = 1e18;
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, INF}));
dfs(0);
//cerr << "run2\n";
for (int i=1; i<=n; i++) {
for (int k=1; k<log_n; k++) {
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 (vol_sum+par[lvl][pow-1].second < vi) {
vol_sum += par[lvl][pow-1].second;
pow++;
}
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";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
263 ms |
43440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |