#include <bits/stdc++.h>
using namespace std;
int N, Q, up[100005][30];
long long D[100005], C[100005], suffix_max[100005], dp[100005][30];
int st[400005];
vector<int> adj[100005];
void build(int v=1, int start=1, int end=N){
if(start==end){
st[v] = D[start];
return;
}
int mid = (start+end)/2;
build(2*v, start, mid);
build(2*v+1, mid+1, end);
st[v] = max(st[2*v], st[2*v+1]);
}
int first_greater(int l, int r, int val, int v=1, int start=1, int end=N){
if(l==start && r==end){
if(st[v] <= val) return 0;
while(start<end){
int mid = (start+end)/2;
if(st[2*v] > val){
v = 2*v;
end = mid;
}
else{
v = 2*v+1;
start = mid+1;
}
}
return start;
}
int mid = (start+end)/2;
if(r<=mid) return first_greater(l,r,val,2*v,start,mid);
else if(l>mid) return first_greater(l,r,val,2*v+1,mid+1,end);
else{
int pos = first_greater(l,mid,val,2*v,start,mid);
if(pos!=0)
return pos;
else
return first_greater(mid+1,r,val,2*v+1,mid+1,end);
}
}
void dfs(int v, int p){
up[v][0] = p;
dp[v][0] = C[v];
for(int i=1; i<=ceil(log2(N)); i++){
up[v][i] = up[up[v][i-1]][i-1];
dp[v][i] = dp[v][i-1] + dp[up[v][i-1]][i-1];
}
for(auto u : adj[v])
dfs(u,v);
}
int main(){
scanf("%d%d", &N, &Q);
for(int i=1; i<=N; i++)
scanf("%lld%lld", &D[i], &C[i]);
build();
adj[0].push_back(N);
for(int i=1; i<N; i++){
int j = first_greater(i+1, N, D[i]);
adj[j].push_back(i);
}
C[0] = 1e9;
dfs(0,0);
for(int R, V, q=0; q<Q; q++){
scanf("%d%d", &R, &V);
for(int i=ceil(log2(N)); i>=0; i--){
if(dp[R][i] < V){
V -= dp[R][i];
R = up[R][i];
}
}
printf("%d\n", R);
}
return 0;
}
Compilation message
fountain.cpp: In function 'int main()':
fountain.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~
fountain.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%lld%lld", &D[i], &C[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d%d", &R, &V);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2900 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
3092 KB |
Output is correct |
5 |
Correct |
4 ms |
3156 KB |
Output is correct |
6 |
Correct |
3 ms |
3128 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
52660 KB |
Output is correct |
2 |
Correct |
258 ms |
49336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2900 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
3092 KB |
Output is correct |
5 |
Correct |
4 ms |
3156 KB |
Output is correct |
6 |
Correct |
3 ms |
3128 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
234 ms |
52660 KB |
Output is correct |
9 |
Correct |
258 ms |
49336 KB |
Output is correct |
10 |
Correct |
3 ms |
3028 KB |
Output is correct |
11 |
Correct |
107 ms |
27852 KB |
Output is correct |
12 |
Correct |
336 ms |
56716 KB |
Output is correct |
13 |
Correct |
241 ms |
48696 KB |
Output is correct |
14 |
Correct |
188 ms |
46488 KB |
Output is correct |
15 |
Correct |
160 ms |
45316 KB |
Output is correct |