#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> d(n), c(n);
for (int i = 0; i < n; i++) {
cin >> d[i] >> c[i];
}
vector<int> gR(n, n), stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && d[stk.back()] < d[i]) {
gR[stk.back()] = i;
stk.pop_back();
}
stk.push_back(i);
}
vector<vector<pair<int, int>>> qry(n);
for (int i = 0; i < q; i++) {
int r, v;
cin >> r >> v;
r--;
qry[r].emplace_back(v, i);
}
vector<vector<int>> adj(n + 1);
for (int i = 0; i < n; i++) {
adj[gR[i]].push_back(i);
}
ll t;
set<pair<ll, int>> S;
vector<int> ans(q);
function<void(int)> dfs = [&](int v) {
S.insert({0 - t, v});
t += c[v];
for (auto [w, id] : qry[v]) {
auto it = S.lower_bound({w - t, -1});
if (it == S.end()) {
ans[id] = 0;
} else {
ans[id] = it->second + 1;
}
}
for (auto to : adj[v]) {
dfs(to);
}
t -= c[v];
S.erase({0 - t, v});
};
for (auto to : adj[n]) {
S.clear();
t = 0;
dfs(to);
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
31056 KB |
Output is correct |
2 |
Correct |
119 ms |
29776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
260 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
117 ms |
31056 KB |
Output is correct |
9 |
Correct |
119 ms |
29776 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
50 ms |
10792 KB |
Output is correct |
12 |
Correct |
159 ms |
35132 KB |
Output is correct |
13 |
Correct |
121 ms |
20188 KB |
Output is correct |
14 |
Correct |
91 ms |
16360 KB |
Output is correct |
15 |
Correct |
65 ms |
15820 KB |
Output is correct |