#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector <pair <int, int>> nums;
vector <vector <int>> nxt;
vector <vector <ll>> sum;
ll get_sum(int st, int len) {
if (len == 0)
return nums[st].second;
ll ans = nums[st].second;
for (int i = 0; i < 20; i++) {
if ((1 << i) & len) {
ans += sum[st][i];
st = nxt[st][i];
}
}
return ans;
}
int get_node(int st, int len) {
int ans = st;
for (int i = 19; i >= 0; i--) {
if ((1 << i) & len)
ans = nxt[ans][i];
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
nums.resize(n);
nxt = vector <vector <int>> (n+1, vector <int> (20));
sum = vector <vector <ll>> (n+1, vector <ll> (20));
for (int i = 1; i <= n; i++) {
cin >> nums[i].first >> nums[i].second;
}
multiset <pair <int, int>> s;
for (int i = n; i > 0; i--) {
pair <int, int> tp = {nums[i].first+1, 0};
auto it = s.lower_bound(tp);
if (it == s.end()) {
nxt[i][0] = 0;
} else {
nxt[i][0] = it->second;
}
s.insert({nums[i].first, i});
while (s.begin()->second != i)
s.erase(s.begin());
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < 20; j++)
nxt[i][j] = nxt[nxt[i][j-1]][j-1];
}
for (int i = 1; i <= n; i++) {
sum[i][0] = nums[nxt[i][0]].second;
sum[i][1] = sum[i][0] + nums[nxt[i][1]].second;
for (int j = 2; j < 20; j++) {
sum[i][j] = sum[nxt[i][j-1]][j-1];
sum[i][j] += sum[i][i-1];
}
}
while (q--) {
int r, v;
cin >> r >> v;
int lo = 0, hi = n - r + 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
ll s = get_sum(r, mid);
if (s < v)
lo = mid + 1;
else
hi = mid;
}
int ans = get_node(r, lo);
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
228 ms |
38716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |