#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 |
Incorrect |
2 ms |
1236 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
796 ms |
33988 KB |
Output is correct |
2 |
Correct |
819 ms |
31516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1236 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |