#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 1e9 + 7;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
int d[n + 1], c[n + 1];
for (int i = 1; i <= n; ++i) {
cin >> d[i] >> c[i];
}
vector<vector<int>> up(n + 1, vector<int> (17, 0));
vector<vector<int>> sum(n + 1, vector<int> (17, 0));
for (int i = 0; i < 17; ++i) {
sum[0][i] = inf;
}
c[0] = d[0] = inf;
stack<pair<int, int>> stk;
stk.push({d[0], 0});
for (int i = n; i >= 1; --i) {
while (stk.top().first <= d[i]) {
stk.pop();
}
up[i][0] = stk.top().second;
sum[i][0] = c[stk.top().second];
for (int j = 1; j < 17; ++j) {
up[i][j] = up[up[i][j - 1]][j - 1];
sum[i][j] = sum[up[i][j - 1]][j - 1] + sum[i][j - 1];
}
stk.push({d[i], i});
}
while (q--) {
int r, v;
cin >> r >> v;
if (c[r] >= v) {
cout << r << '\n';
continue;
}
v -= c[r];
for (int i = 16; i >= 0; --i) {
if (sum[r][i] < v) {
v -= sum[r][i];
r = up[r][i];
}
}
cout << up[r][0] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
796 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
35372 KB |
Output is correct |
2 |
Correct |
284 ms |
32968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
796 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
243 ms |
35372 KB |
Output is correct |
9 |
Correct |
284 ms |
32968 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
73 ms |
20092 KB |
Output is correct |
12 |
Correct |
345 ms |
37480 KB |
Output is correct |
13 |
Correct |
194 ms |
36340 KB |
Output is correct |
14 |
Correct |
128 ms |
35900 KB |
Output is correct |
15 |
Correct |
98 ms |
35944 KB |
Output is correct |