#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] , dp[N][19] , used[N] ;
vector<int> g[N] ;
void dfs(int v , int p) {
used[v] = 1 ;
// if(v != p) {
up[v][0] = p ;
dp[v][0] = c[v] ;
if(v != p) dp[v][0] += c[p] ;
int last = p ;
rep(i , 1 , 17) {
up[v][i] = up[up[v][i - 1]][i - 1] ;
if(up[v][i] == up[v][i - 1]) {
dp[v][i] = dp[v][i - 1] ;
}else{
dp[v][i] = dp[up[v][i - 1]][i - 1] + c[v] ;
}
}
// }n
for(auto& to : g[v]) {
if(to != p) {
dfs(to , v) ;
}
}
}
void solve(){
cin >> n >> q ;
rep(i , 1 , n) {
cin >> d[i] >> c[i] ;
}
stack<pair<int,int>> s ;
per(i , n , 1){
while(sz(s) && s.top().first <= d[i]) s.pop() ;
if(sz(s)) g[s.top().second].pb(i) ;
s.push({d[i] , i}) ;
}
rep(i , 1 , n) {
if(!used[i]) {
int pr = i ;
dfs(i , pr) ;
}
}
// rep(i , 1 , n) {
// rep(j , 0 , 17) {
// cout << dp[i][j] << ' ' ;
// }
// cout << "\n" ;
// }
rep(i , 1 , q) {
int ind , k ; cin >> ind >> k ;
if(c[ind] >= k) {
cout << ind << "\n" ;
continue;
}
if(dp[ind][17] < k) {
cout << "0\n" ;
}else{
per(j , 17 , 0) {
if(dp[ind][0] >= k) {
k -= dp[ind][0] ;
ind = up[ind][0] ;
break ;
}else
if(dp[ind][j] <= k) {
k -= dp[ind][j] ;
ind = up[ind][j] ;
k += c[ind] ;
if(k == 0) break ;
}
}
if(k > 0) ind = up[ind][0] ;
cout << ind << "\n" ;
}
}
}
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 dfs(long long int, long long int)':
fountain.cpp:37:13: warning: unused variable 'last' [-Wunused-variable]
37 | int last = p ;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1554 ms |
11112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |