#include <bits/stdc++.h>
using namespace std;
#define int long long
int u[100000][20];
int s[100000][20];
vector<int> c(100000);
int j(int v, int x){
bool b[20];
for(int i = 0; i < 20; i++){
if((x >> i) & 1) b[i] = 1;
else b[i] = 0;
}
int r = v;
for(int i = 0; i < 20; i++){
if(b[i]){
r = u[r][i];
}
}
return r;
}
int f(int v, int x){
bool b[20];
for(int i = 0; i < 20; i++){
if((x >> i) & 1) b[i] = 1;
else b[i] = 0;
}
int p = 0;
int r = v;
for(int i = 0; i < 20; i++){
if (r == -1) break;
if(b[i]){
p += s[r][i];
r = u[r][i];
}
}
return p;
}
int32_t main()
{
int n, q;
cin >> n >> q;
vector<int> d(n);
for(int i = 0; i < n; i++){
cin >> d[i] >> c[i];
}
for(int i = 0; i < n; i++){
for(int k = 0; k < 20; k++) u[i][k] = -1;
}
vector<pair<int, int>> p;
p.push_back({1e9+1, -1});
for(int i = n-1; i >= 0; i--){
while(p[p.size()-1].first <= d[i]){
p.pop_back();
}
u[i][0] = p[p.size()-1].second;
p.push_back({d[i], i});
}
for(int k = 1; k < 20; k++){
for(int i = 0; i < n; i++){
if(u[i][k-1] != -1) u[i][k] = u[u[i][k-1]][k-1];
else u[i][k] = -1;
}
}
for(int i = 0; i < n; i++){
s[i][0] = c[i];
}
for(int k = 1; k < 20; k++){
for(int i = 0; i < n; i++){
if(u[i][k-1] != -1) s[i][k] = s[i][k-1] + s[u[i][k-1]][k-1];
else s[i][k]=s[i][k-1];
}
}
for(int i = 0; i < q; i++){
int r, v;
cin >> r >> v;
if(v > f(r-1, n-1)) cout << 0 << "\n";
else{
int a = 0, b = n;
int m;
while(b > a){
m = (a+b+1)/2;
if(f(r-1, m) < v) a = m;
else b = m-1;
}
cout << j(r-1, b)+1 << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
2 ms |
1240 KB |
Output is correct |
3 |
Correct |
3 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
695 ms |
34772 KB |
Output is correct |
2 |
Correct |
862 ms |
32480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
2 ms |
1240 KB |
Output is correct |
3 |
Correct |
3 ms |
1236 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
695 ms |
34772 KB |
Output is correct |
9 |
Correct |
862 ms |
32480 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
360 ms |
20160 KB |
Output is correct |
12 |
Correct |
1143 ms |
36812 KB |
Output is correct |
13 |
Correct |
892 ms |
35552 KB |
Output is correct |
14 |
Correct |
546 ms |
34928 KB |
Output is correct |
15 |
Correct |
488 ms |
34648 KB |
Output is correct |