#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005];
int n, q;
int dijametar[100005], kapacitet[100005];
int parent[100005][30], sobira[100005][30];
void dfs(int teme, int preth) {
parent[teme][0]=preth;
sobira[teme][0]=kapacitet[preth];
for (auto next:G[teme]) {
if (next!=preth)
dfs(next, teme);
}
}
void precomputeSparseMatrix() {
for (int i=1;i<20;i++) {
for (int teme=1;teme<=n+1;teme++) {
if (parent[teme][i-1]!=-1) {
parent[teme][i]=parent[parent[teme][i-1]][i-1];
sobira[teme][i]=sobira[parent[teme][i-1]][i-1]+sobira[teme][i-1];
}
}
}
}
int binary_lifting(int rezervoar, int voda) {
voda-=kapacitet[rezervoar];
for (int i=20;i>=0;i--) {
if (parent[rezervoar][i]==-1) continue;
if (sobira[rezervoar][i]<voda) {
voda-=sobira[rezervoar][i];
rezervoar=parent[rezervoar][i];
}
}
return parent[rezervoar][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i=1;i<=n;i++)
cin >> dijametar[i] >> kapacitet[i];
kapacitet[n+1]=2000000000;
stack<pair<int,int> > s;
s.push({INT_MAX, n+1});
for (int i=n;i>=1;i--) {
while (s.top().first<=dijametar[i])
s.pop();
G[i].push_back(s.top().second);
G[s.top().second].push_back(i);
s.push({dijametar[i], i});
}
memset(parent, -1, sizeof(parent));
dfs(n+1, 0);
precomputeSparseMatrix();
while (q--) {
int r, v;
cin >> r >> v;
if (kapacitet[r]>=v) cout << r << '\n';
else {
int rez=binary_lifting(r, v);
if (rez==n+1) cout << 0 << '\n';
else cout << rez << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
34384 KB |
Output is correct |
2 |
Correct |
81 ms |
33876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
77 ms |
34384 KB |
Output is correct |
9 |
Correct |
81 ms |
33876 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
56 ms |
29364 KB |
Output is correct |
12 |
Correct |
115 ms |
39244 KB |
Output is correct |
13 |
Correct |
101 ms |
35532 KB |
Output is correct |
14 |
Correct |
68 ms |
34128 KB |
Output is correct |
15 |
Correct |
67 ms |
35024 KB |
Output is correct |