#include"bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
int N, Left, Right, Value;
int seg[2000001];
int Query (int l = 1, int r = N, int ind = 1) {
if (l > Right || r < Left) return INT_MAX;
if (l >= Left and r <= Right) return seg[ind];
int mid = (l + r) / 2;
return min(Query(l, mid, ind * 2), Query(mid + 1, r, ind * 2 + 1));
}
void Update () {
int ind = Left + N - 2;
seg[ind] = Value;
ind /= 2;
while (ind) {
seg[ind] = min(seg[ind * 2], seg[ind * 2 + 1]);
ind /= 2;
}
}
pair<int,int> sparse[100001][20]; // {node, capacity}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
N = exp2(ceil(log2(n + 1)));
for (int& i : seg) i = INT_MAX;
for (int i = 1 ; i <= n ; i++) {
for (int j = 0 ; j < 20 ; j++) sparse[i][j] = {-1, INT_MAX};
}
pair<int,int> arr[n + 1];
vector<pair<int,int>> v; // {radius, index}
vector<int> E;
set<int> s;
for (int i = 1 ; i <= n ; i++) {
cin >> arr[i].first >> arr[i].second;
v.push_back({arr[i].first, i});
}
sort(v.begin(), v.end());
int ind = 1;
for (int i = 0 ; i < n ; i++) {
if (i and v[i].first != v[i-1].first) ind++;
arr[v[i].second].first = ind;
}
Right = N;
for (int i = n ; i > 0 ; i--) {
Left = arr[i].first + 1;
sparse[i][0] = {Query(), arr[i].second};
if (sparse[i][0].first == INT_MAX) sparse[i][0].first = -1;
Value = i;
Update();
}
for (int i = n ; i >= 1 ; i--) {
for (int j = 1 ; sparse[i][j-1].first != -1 ; j++) {
sparse[i][j] = {sparse[sparse[i][j-1].first][j-1].first, sparse[i][j-1].second + sparse[sparse[i][j-1].first][j-1].second};
}
}
while (q--) {
int s, x;
cin >> s >> x;
for (int i = 19 ; i >= 0 ; i--) {
if (sparse[s][i].second < x) {
// cerr << "here\n";
x -= sparse[s][i].second;
s = sparse[s][i].first;
}
}
cout << (s == -1 ? 0 : s) << endl;
}
}
// 5 4
// 1 -> 2 -> 3
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
16076 KB |
Output is correct |
3 |
Correct |
9 ms |
16204 KB |
Output is correct |
4 |
Correct |
9 ms |
16332 KB |
Output is correct |
5 |
Correct |
12 ms |
16332 KB |
Output is correct |
6 |
Correct |
9 ms |
16284 KB |
Output is correct |
7 |
Correct |
11 ms |
16260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
49632 KB |
Output is correct |
2 |
Correct |
305 ms |
50100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
8 ms |
16076 KB |
Output is correct |
3 |
Correct |
9 ms |
16204 KB |
Output is correct |
4 |
Correct |
9 ms |
16332 KB |
Output is correct |
5 |
Correct |
12 ms |
16332 KB |
Output is correct |
6 |
Correct |
9 ms |
16284 KB |
Output is correct |
7 |
Correct |
11 ms |
16260 KB |
Output is correct |
8 |
Correct |
278 ms |
49632 KB |
Output is correct |
9 |
Correct |
305 ms |
50100 KB |
Output is correct |
10 |
Correct |
9 ms |
16332 KB |
Output is correct |
11 |
Correct |
116 ms |
37412 KB |
Output is correct |
12 |
Correct |
391 ms |
55732 KB |
Output is correct |
13 |
Correct |
310 ms |
55256 KB |
Output is correct |
14 |
Correct |
198 ms |
54716 KB |
Output is correct |
15 |
Correct |
169 ms |
54992 KB |
Output is correct |