#include <bits/stdc++.h>
#define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define itn int
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define count1 __builtin_popcount
#define all(v) v.begin(), v.end()
using namespace std;
struct point{
};
int ctoi(char x) {
return (int)x - '0';
}
int sumab(int a, int b) {
return (a + b) * (b - a + 1) / 2;
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
void print(vector <int> &v) {
for (const int &i : v) cout << i << ' ';
}
constexpr int MAX = 1e+5 + 3, INF = 5e+15, MOD = 16714589, K = 18;
int temp, temp1, temp2, temp3;
vector <int> g[MAX];
pair<int, int> up[K][MAX];
void _() {
int n, q;
cin >> n >> q;
vector <int> v(n+1), dia(n+1);
v[0] = dia[0] = INF;
for (int i=1; i <= n; i++) {
cin >> dia[i] >> v[i];
}
// build graph O(n * log(n))
multiset <pair<int, int>> ms;
for (int i=1; i <= n; i++) {
vector <pair<int, int>> rem;
for (auto &j : ms) {
if (j.ff >= dia[i]) {
break;
}
g[j.ss].pb(i);
rem.pb(j);
}
for (const auto &j : rem) ms.erase(ms.find(j));
ms.ins({dia[i], i});
}
for (int i=0; i <= n; i++) {
if (g[i].empty()) g[i].pb(0);
}
// build up O(n * log(n))
for (int i=0; i <= n; i++) {
up[0][i] = {g[i][0], v[i]};
}
for (int i=1; i <= K; i++) {
for (int j=0; j <= n; j++) {
up[i][j].ff = up[i-1][up[i-1][j].ff].ff;
up[i][j].ss = up[i-1][j].ss + up[i-1][up[i-1][j].ff].ss;
}
}
// cout << up[1][5].ss << endl;
// queries O(q * log(n))
while (q--) {
int id, w;
cin >> id >> w;
vector <int> t = {id};
for (int i=K; i >= 0; i--) {
if (up[i][id].ss <= w) {
w -= up[i][id].ss;
id = up[i][id].ff;
t.pb(id);
}
}
// if (w != 0) {
// cout << id << endl;
// }
// else {
// cout << "0 " << w << ' ' << id << endl;
// }
if (w != 0) cout << id;
else {
cout << t[t.size() - 2];
}
cout << endl;
}
}
signed main() {
GOOD_LUCK
int tests=1;
// cin >> tests;
while (tests--) {
_();
// cout << endl;
}
return 0;
}
// Problem X
// by Ekber_Ekber
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |