#include <bits/stdc++.h>
using namespace std;
// #define int long long
// int check(int pos, int sum) {
// }
void solve(){
int n, q; cin >> n >> q;
vector<pair<int, int>> vp(n+1); // diametr & capacity
vector<pair<int, int>> gr(n+1, {0, 1e9}); // child & cost
for (int i = 1; i <= n; i++) {
cin >> vp[i].first >> vp[i].second;
gr[i].second = vp[i].second;
for (int j = 1; j < i; j++){
if (gr[j].first == 0 && vp[j].first < vp[i].first) {
gr[j] = {i, vp[j].second};
}
}
}
auto check = [&](int pos, int sum) -> int {
while (pos != 0){
sum-=gr[pos].second;
if (sum <= 0) break;
pos = gr[pos].first;
}
return pos;
};
for (int i = 0; i < q; i++){
int ri, vi; cin >> ri >> vi;
cout << check(ri, vi) << endl;
// auto j = lower_bound(prefs[ri].begin(), prefs[ri].end(), vi);
// int x = distance(prefs[ri].begin(), j);
// cout << indx[ri][x] << endl;
}
// vector<int> v;
// v = {10, 18, 27};
// auto i = upper_bound(v.begin(), v.end(), 25);
// int x = distance(v.begin(), i-1);
// cout << gr[6].first << endl;
// for (int i = 1; i <= n; i++) cout << i << ": " << gr[i].first << ' ' << gr[i].second << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t = 1;
// cin >> t;
while (t--){solve();}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |