This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
#include <stack>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
int n, q;
ll sum[100005][17];
int par[100005][17];
int binDrop(int v, int sm) {
for (int i = 16; i >= 0; i--) {
if (par[v][i] != -1 && sum[v][i] < sm) {
sm -= sum[v][i]; v = par[v][i];
}
}
return v;
}
void solve() {
cin >> n >> q;
vector<pii> arr(n + 1);
memset(par, -1, sizeof par);
for (int i = 1; i <= n; i++)
cin >> arr[i].ff >> arr[i].sc;
stack<int> stk;
for (int i = 1; i <= n; i++) {
while (!stk.empty() && arr[stk.top()].ff < arr[i].ff) {
par[stk.top()][0] = i; sum[stk.top()][0] = arr[stk.top()].sc; stk.pop();
}
stk.push(i);
}
while (!stk.empty()) {
par[stk.top()][0] = 0;
sum[stk.top()][0] = arr[stk.top()].sc;
stk.pop();
}
for (int j = 1; j <= 16; j++) {
for (int i = 1; i <= n; i++) {
if (par[i][j - 1] != -1 && par[par[i][j - 1]][j - 1] != -1) {
par[i][j] = par[par[i][j - 1]][j - 1];
sum[i][j] = sum[i][j - 1] + sum[par[i][j - 1]][j - 1];
}
}
}
int d, v;
while (q--) {
cin >> d >> v;
cout << binDrop(d, v) << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
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... |