이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 1e9 + 7;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
int d[n + 1], c[n + 1];
for (int i = 1; i <= n; ++i) {
cin >> d[i] >> c[i];
}
vector<vector<int>> up(n + 1, vector<int> (17, 0));
vector<vector<int>> sum(n + 1, vector<int> (17, 0));
for (int i = 0; i < 17; ++i) {
sum[0][i] = inf;
}
c[0] = d[0] = inf;
stack<pair<int, int>> stk;
stk.push({d[0], 0});
for (int i = n; i >= 1; --i) {
while (stk.top().first <= d[i]) {
stk.pop();
}
up[i][0] = stk.top().second;
sum[i][0] = c[stk.top().second];
for (int j = 1; j < 17; ++j) {
up[i][j] = up[up[i][j - 1]][j - 1];
sum[i][j] = sum[up[i][j - 1]][j - 1] + sum[i][j - 1];
}
stk.push({d[i], i});
}
auto get = [&](int v, int d) {
int res = c[v];
for (int i = 16; i >= 0; --i) {
if (d >> i & 1) {
res += sum[v][i];
v = up[v][i];
}
}
return make_pair(res, v);
};
while (q--) {
int r, v;
cin >> r >> v;
if (c[r] >= v) {
cout << r << '\n';
continue;
}
int lo = 0, hi = n;
while (lo + 1 < hi) {
int mid = (lo + hi) >> 1;
if (get(r, mid).first >= v) {
hi = mid;
} else {
lo = mid;
}
}
cout << get(r, hi).second << '\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... |