#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 p) {
for (int i : vec[nd]) {
tv.push_back(i);
}
vec[nd].clear();
if (l == r) {
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (p <= mid) {
fill(nd + 1, l, mid, p);
} else {
fill(rs, mid + 1, r, p);
}
}
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);
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 |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Execution timed out |
2093 ms |
40876 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15036 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
15084 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15196 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15036 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
15084 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15196 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
35 ms |
15508 KB |
Output is correct |
17 |
Correct |
16 ms |
15196 KB |
Output is correct |
18 |
Correct |
36 ms |
15528 KB |
Output is correct |
19 |
Correct |
38 ms |
15448 KB |
Output is correct |
20 |
Correct |
11 ms |
15196 KB |
Output is correct |
21 |
Correct |
22 ms |
15448 KB |
Output is correct |
22 |
Correct |
8 ms |
15196 KB |
Output is correct |
23 |
Correct |
24 ms |
15440 KB |
Output is correct |
24 |
Correct |
35 ms |
15456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15036 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
15084 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15196 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
35 ms |
15508 KB |
Output is correct |
17 |
Correct |
16 ms |
15196 KB |
Output is correct |
18 |
Correct |
36 ms |
15528 KB |
Output is correct |
19 |
Correct |
38 ms |
15448 KB |
Output is correct |
20 |
Correct |
11 ms |
15196 KB |
Output is correct |
21 |
Correct |
22 ms |
15448 KB |
Output is correct |
22 |
Correct |
8 ms |
15196 KB |
Output is correct |
23 |
Correct |
24 ms |
15440 KB |
Output is correct |
24 |
Correct |
35 ms |
15456 KB |
Output is correct |
25 |
Correct |
4 ms |
15192 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
38 ms |
15536 KB |
Output is correct |
28 |
Correct |
17 ms |
15452 KB |
Output is correct |
29 |
Correct |
11 ms |
15432 KB |
Output is correct |
30 |
Correct |
29 ms |
15712 KB |
Output is correct |
31 |
Correct |
34 ms |
15704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Execution timed out |
2093 ms |
40876 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |