#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<array<int, 2>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
}
vector<vector<int>> at(n);
vector<array<int, 2>> qe(q);
for (int i = 0; i < q; i++) {
cin >> qe[i][0] >> qe[i][1];
--qe[i][0];
at[qe[i][0]].push_back(i);
}
vector<int> stk;
vector<int> pref;
auto get = [&](int v) {
int len = (int) pref.size();
int low = 0, high = len - 1;
if (pref.back() < v) {
return 0;
}
while (low <= high) {
int mid = low + (high - low) / 2;
if (pref.back() - (mid > 0 ? pref[mid - 1] : 0) < v) {
high = mid - 1;
} else {
low = mid + 1;
}
}
--low;
return stk[low] + 1;
};
vector<int> res(q);
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && a[stk.back()][0] <= a[i][0]) {
stk.pop_back();
pref.pop_back();
}
int x = (pref.empty() ? 0 : pref.back());
pref.push_back(x + a[i][1]);
stk.push_back(i);
for (auto query_id : at[i]) {
int v = qe[query_id][1];
res[query_id] = get(v);
}
}
for (int i = 0; i < q; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
10476 KB |
Output is correct |
2 |
Correct |
68 ms |
10948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
59 ms |
10476 KB |
Output is correct |
9 |
Correct |
68 ms |
10948 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
40 ms |
6800 KB |
Output is correct |
12 |
Correct |
94 ms |
13716 KB |
Output is correct |
13 |
Correct |
122 ms |
13412 KB |
Output is correct |
14 |
Correct |
72 ms |
12520 KB |
Output is correct |
15 |
Correct |
93 ms |
12896 KB |
Output is correct |