#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
using arr = pair<int, int>;
const int N = 2e5 + 5;
int seg[N * 2];
vector<int> vec[N * 2];
void init() {
for (int i = 0; i < N * 2; ++i) {
seg[i] = N;
}
}
void upd(int nd, int l, int r, int p, int v) {
if (l == r) {
seg[nd] = v;
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (p <= mid) {
upd(nd + 1, l, mid, p, v);
} else {
upd(rs, mid + 1, r, p, v);
}
seg[nd] = min(seg[nd + 1], seg[rs]);
}
int qry(int nd, int l, int r, int s, int e) {
if (l >= s && r <= e) {
return seg[nd];
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
return qry(nd + 1, l, mid, s, e);
} else {
if (mid < s) {
return qry(rs, mid + 1, r, s, e);
} else {
return min(qry(nd + 1, l, mid, s, e), qry(rs, mid + 1, r, s, e));
}
}
}
void add(int nd, int l, int r, int s, int e, int x) {
if (l >= s && r <= e) {
vec[nd].push_back(x);
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
add(nd + 1, l, mid, s, e, x);
} else {
if (mid < s) {
add(rs, mid + 1, r, s, e, x);
} else {
add(nd + 1, l, mid, s, e, x);
add(rs, mid + 1, r, s, e, x);
}
}
}
vector<int> tv;
void fill(int nd, int l, int r, int s, int e) {
if (l >= s && r <= e) {
for (int i : vec[nd]) {
tv.push_back(i);
}
vec[nd].clear();
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
fill(nd + 1, l, mid, s, e);
} else {
if (mid < s) {
fill(rs, mid + 1, r, s, e);
} else {
fill(nd + 1, l, mid, s, e);
fill(rs, mid + 1, r, s, e);
}
}
}
int l[N], r[N];
int a[N], b[N], ans[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
--l[i], --r[i];
}
vector<int> id(n);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int i, int j) {
return l[i] < l[j];
});
init();
for (int i = 0; i < n; ++i) {
int cur = id[i];
if (l[cur] == 0) {
a[cur] = 0;
} else {
a[cur] = 1 + qry(0, 0, n - 1, l[cur], r[cur]);
}
upd(0, 0, n - 1, cur, a[cur]);
}
sort(id.begin(), id.end(), [&](int i, int j) {
return r[i] > r[j];
});
init();
priority_queue<arr, vector<arr>, greater<arr>> pq;
for (int i = 0; i < n; ++i) {
int cur = id[i];
if (r[cur] == n - 1) {
b[cur] = 0;
} else {
b[cur] = 1 + qry(0, 0, n - 1, l[cur], r[cur]);
}
upd(0, 0, n - 1, cur, b[cur]);
pq.emplace(a[cur] + b[cur], cur);
add(0, 0, n - 1, l[cur], r[cur], cur);
ans[cur] = a[cur] + b[cur];
}
while (!pq.empty()) {
auto [c, v] = pq.top();
pq.pop();
if (c > ans[v]) {
continue;
}
fill(0, 0, n - 1, v, v);
for (int i : tv) {
if (ans[v] + 1 < ans[i]) {
ans[i] = ans[v] + 1;
pq.emplace(ans[i], i);
}
}
}
int q;
cin >> q;
while (q--) {
int v;
cin >> v;
--v;
cout << (ans[v] >= N ? -1 : ans[v] + 1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Execution timed out |
2062 ms |
40768 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Incorrect |
3 ms |
14940 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Incorrect |
3 ms |
14940 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Incorrect |
3 ms |
14940 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Execution timed out |
2062 ms |
40768 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |