This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
for(int i = 0; i < n; i++){
for(int k = i+1; k < n; k++){
if(d[k] > d[i]){
u[i][0] = k;
break;
}
}
}
for(int i = 0; i < n; i++){
for(int k = 1; k < 20; k++){
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;
}
/*
2 1
1 1
1 1
2 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |