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 "bits/stdc++.h"
#define int long long
using namespace std;
const int inf = 1e18 + 18;
const int sz = 1e5 + 5;
const int sm = 25;
vector<int> aj[sz];
int pf[sz], dm[sz];
int up[sm][sz];
void dfs(int v, int s = 0) {
for (int i = 1; i < sm; i ++)
up[i][v] = up[i-1][up[i-1][v]];
pf[v] = s;
for (int u : aj[v]) {
up[0][u] = v;
dfs(u, s + dm[u]);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<pair<int,int>> a;
for (int i = 1; i <= n; i ++) {
int d, c;
cin >> d >> c;
dm[i] = c;
a.push_back(make_pair(d, i));
}
sort(begin(a),end(a),[](auto& l, auto& r){
int dl = l.first;
int dr = r.first;
if (dl != dr)
return dl < dr;
int xl = l.second;
int xr = r.second;
return xl > xr;
});
set<int> rs = {n+1};
for (auto it = rbegin(a); it != rend(a); it ++) {
int v = it->second;
int u = *rs.upper_bound(v);
aj[u].push_back(v);
rs.insert(v);
}
dfs(n+1);
while (q --) {
int r, v;
cin >> r >> v;
int s = pf[r];
for (int i = sm-1; -1 < i; i --) {
int p = up[i][r];
if (s - pf[p] + dm[p] < v) {
r = up[i][r];
}
}
if (s - pf[r] + dm[r] < v)
r = up[0][r];
cout << r % (n+1) << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |