이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ll long long
#define ar array
#define f first
#define s second
#define mpr make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(auto i = a; i < b; i++)
#define FORD(i, a, b) for(auto i = a - 1; i >= b; i--)
#define trav(x, v) for(auto x : v)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// --------------- SOLUTION --------------- //
const int mxN = 1e5+5;
int n, q;
pair <int, int> in[mxN];
pair <int, int> ch[mxN][25];
vector <pair<int, int>> st;
void solve() {
cin >> n >> q;
FOR(i, 1, n + 1) {
cin >> in[i].f >> in[i].s;
}
FORD(i, n + 1, 1) {
while (!st.empty() && st.back().f <= in[i].f) st.pop_back();
if (st.empty()) ch[i][0] = {0, in[i].s};
else ch[i][0] = {st.back().s, in[i].s};
st.pb({in[i].f, i});
}
FOR(j, 1, 20) {
FOR(i, 1, n + 1) {
ch[i][j] = {ch[ch[i][j - 1].f][j - 1].f, ch[i][j - 1].s + ch[ch[i][j - 1].f][j - 1].s};
}
}
while (q--) {
int r, v;
cin >> r >> v;
FORD(i, 20, 0) {
if (v > ch[r][i].s) {
v -= ch[r][i].s;
r = ch[r][i].f;
}
}
cout << r << "\n";
}
}
int main() {
fastio;
// fileio;
int tests = 1;
// cin >> tests;
while (tests--) {
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... |