#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));
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];
if (dm[r] >= v) {
cout << r << '\n';
continue;
}
for (int i = sm-1; -1 < i; i --) {
int p = up[i][r];
if (s - pf[p] + dm[p] < v) {
r = up[i][r];
}
}
r = up[0][r];
if (n+1 == r)
r = 0;
cout << r << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
40216 KB |
Output is correct |
2 |
Correct |
161 ms |
38596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |