#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N = 2e5 + 123;
const int inf = 1e18;
const int mod = 1e9 + 7;
const double eps = 1e-13;
int a[N], b[N], c[N], n, m, q, k, x, y, w, l, r, up[N][20], di[N][20];
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
void solve()
{
cin >> n >> m;
FOR( i, 1, n, 1 ) cin >> a[i] >> b[i];
stack <int> st;
FORR( i, n, 1, 1 )
{
while( !st.empty() && a[st.top()] <= a[i] ) st.pop();
c[i] = ( sz(st) ? st.top() : 0 ), st.push(i);
up[i][0] = c[i], di[i][0] = b[i];
}
FOR( j, 1, 18, 1 )
{
FOR( i, 1, n, 1 )
{
up[i][j] = up[up[i][j - 1]][j - 1];
di[i][j] = di[up[i][j - 1]][j - 1] + di[i][j - 1];
}
}
FOR( bobek, 1, m, 1 )
{
cin >> x >> y;
swap(x, y);
int l = 0, r = n, res = -1;
while( l <= r )
{
int md = l + r >> 1, v = y, sum = 0;
FORR( i, 18, 0, 1 )
{
if( (md >> i) & 1 ) sum += di[v][i], v = up[v][i];
}
if( !v )
{
r = md - 1;
continue;
}
if( sum + b[v] < x ) l = md + 1;
else r = md - 1, res = md;
}
if( res == -1 )
{
cout << "0\n";
continue;
}
int ans = y;
FORR( i, 18, 0, 1 ) if( (res >> i) & 1 ) ans = up[ans][i];
cout << ans << '\n';
}
}
signed main()
{
// freopen("connect.in", "r", stdin);
// freopen("connect.out", "w", stdout);
respagold
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |