#include <bits/stdc++.h>
using namespace std ;
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ones(x) __builtin_popcount(x)
#define remove(v) v.erase(unique(all(v)), v.end())
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#ifdef local
#include "C:\debug.h"
#else
#define dbg(x...) 42
#endif
#define int ll
const int N = 1e5 + 2 ;
int n , q , d[N] , c[N] , up[N][19] , sum[N][19] ;
void dfs(int v , int p) {
rep(i , 2 , 17) {
up[v][i] = up[up[v][i - 1]][i - 1] ;
}
}
void solve(){
cin >> n >> q ;
rep(i , 1 , n) {
cin >> d[i] >> c[i] ;
}
stack<pair<int,int>> s ;
s.push({INT_MAX , n + 1}) ;
per(i , n , 1){
up[i][0] = i , sum[i][0] = c[i] ;
while(sz(s) && s.top().first < d[i]) s.pop() ;
if(sz(s)) up[i][1] = s.top().second , sum[i][1] = c[i] + c[s.top().second] ;
else up[i][1] = i ;
s.push({d[i] , i}) ;
}
per(i , n , 1) {
rep(j , 2 , 18) {
up[i][j] = up[up[i][j - 1]][j - 1] ;
up[i][j] = max(up[i][j] , up[i][j - 1]) ;
if(up[i][j]) sum[i][j] = sum[i][j - 1] + c[up[i][j]] ;
}
}
// rep(i , 1 , n) {
// rep(j , 0 , 18) {
// cout << sum[i][j] << ' ';
// }
// cout << "\n" ;
// }
rep(i , 1 , q) {
int ind , k ; cin >> ind >> k ;
per(j , 18 , 0) {
if(k >= sum[ind][j]) {
k -= sum[ind][j] ;
if(up[ind][j]) ind = up[ind][j] ;
k += c[ind] ;
}
}
if(ind > n) {
cout << 0 << "\n" ;
}else{
rep(j , 0 , 18) {
if(sum[ind][j] >= k) {
ind = up[ind][j] ;
break ;
}
}
cout << ind << "\n" ;
}
dbg(ind , k) ;
}
}
signed main () {
ios::sync_with_stdio(false) ;
cin.tie(0) ;
int test = 1 ;
// cin >> test ;
for(int i = 1 ; i <= test ; i++) {
solve() ;
}
}
Compilation message
fountain.cpp: In function 'void solve()':
fountain.cpp:20:19: warning: statement has no effect [-Wunused-value]
20 | #define dbg(x...) 42
| ^~
fountain.cpp:82:9: note: in expansion of macro 'dbg'
82 | dbg(ind , k) ;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
85 ms |
31824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |