#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct Tree {
int sz = 21;
vector<vector<int>> table;
vector<vector<ll>> sum;
Tree (){}
Tree (int n) {
// while ((1 << sz) <= n) sz++;
table = vector<vector<int>> (sz, vector<int> ((1 << sz)));
sum = vector<vector<ll>> (sz, vector<ll> ((1 << sz)));
}
void add_cost(int u, int x) {
sum[0][u] = x;
}
void add_edge(int u, int v) {
table[0][u] = v;
}
void build() {
for (int bit = 1; bit < sz; ++bit) {
for (int Node = 1; Node < sz; ++Node) {
int to = table[bit - 1][Node];
table[bit][Node] = table[bit - 1][to];
sum[bit][Node] = sum[bit - 1][Node] + sum[bit - 1][to];
}
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, idx = 1;
cin >> n >> q;
Tree tree(n);
vector<pair<int, int>> v(n);
for (auto &[a, b] : v) {
cin >> a >> b;
tree.add_cost(idx++, b);
}
vector<int> track;
for (int i = n - 1; i >= 0; --i) {
while (track.size() && v[track.back()].first <= v[i].first) {
track.pop_back();
}
if (track.size()) {
tree.add_edge(i + 1, track.back() + 1);
}
track.push_back(i);
}
tree.build();
for (int i = 0, Node, x; i < q; ++i) {
cin >> Node >> x;
for (int bit = tree.sz - 1; bit >= 0; --bit) {
// cout << bit << " " << Node << " " << tree.sum[bit][Node] << " " << x << endl;
if (tree.sum[bit][Node] < x) {
x -= tree.sum[bit][Node];
Node = tree.table[bit][Node];
}
}
cout << Node << '\n';
}
return 0;
}
/*
6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
*/
Compilation message
fountain.cpp: In function 'int main()':
fountain.cpp:37:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for (auto &[a, b] : v) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
121 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
105 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
121 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |