#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int inf=1e18;
#ifdef LOCAL
#include "algo/debug.h"
#else
template <typename... Args>
void dummy(Args&&... args){}
#define ps dummy
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define len(x) (int)((x).size())
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ai2 array<int,2>
#define ai3 array<int,3>
#define ai4 array<int,4>
#define ai5 array<int,5>
template<class T> int lwb(const V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(const V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define pct __builtin_popcount
#define ctz __builtin_ctz
#define clz __builtin_clz
constexpr int p2(int x) { return (int)1 << x; }
constexpr int bits(int x) { return x == 0 ? 0 : 31-clz(x); } // floor(log2(x))
template<class T>void UNIQUE(V<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }
template<class T,class U>void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }
const int LOG = 16;
void solve() {
int n, q; cin >> n >> q;
vi D(n), C(n);
for (int i = 0; i < n; i++) {
cin >> D[i] >> C[i];
}
vi stk;
vector lift(LOG + 1, V<ai2>(n, { -1, 0 }));
for (int i = 0; i < n; i++) {
while (len(stk) && D[i] > D[stk.bk]) {
lift[0][stk.bk] = { i, C[i] };
stk.pop_back();
}
stk.pb(i);
}
for (int pw = 1; pw <= LOG; pw++) {
for (int i = 0; i < n; i++) {
auto [j, c] = lift[pw - 1][i];
if (j == -1) {
lift[pw][i] = lift[pw - 1][i];
} else {
lift[pw][i] = lift[pw - 1][j];
lift[pw][i][1] += lift[pw - 1][i][1];
}
}
}
while (q--) {
int i, water; cin >> i >> water; i--;
water -= C[i];
if (water <= 0) {
cout << i + 1 << '\n';
continue;
}
for (int pw = LOG; pw >= 0; pw--) {
auto [j, c] = lift[pw][i];
if (c < water && j != -1) {
water -= c;
i = j;
}
}
int ans = lift[0][i][0] == -1 ? 0 : lift[0][i][0] + 1;
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |