#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);
}
}
tv.clear();
}
int q;
cin >> q;
while (q--) {
int v;
cin >> v;
--v;
cout << (ans[v] >= N ? -1 : ans[v] + 1) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
393 ms |
38236 KB |
Output is correct |
5 |
Correct |
225 ms |
30524 KB |
Output is correct |
6 |
Correct |
157 ms |
30528 KB |
Output is correct |
7 |
Correct |
97 ms |
21716 KB |
Output is correct |
8 |
Correct |
111 ms |
27584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
15040 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 |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
15040 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 |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
5 ms |
15196 KB |
Output is correct |
17 |
Correct |
5 ms |
15284 KB |
Output is correct |
18 |
Correct |
6 ms |
15192 KB |
Output is correct |
19 |
Correct |
5 ms |
15196 KB |
Output is correct |
20 |
Correct |
4 ms |
15196 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
15196 KB |
Output is correct |
23 |
Correct |
5 ms |
15168 KB |
Output is correct |
24 |
Correct |
5 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
15040 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 |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
5 ms |
15196 KB |
Output is correct |
17 |
Correct |
5 ms |
15284 KB |
Output is correct |
18 |
Correct |
6 ms |
15192 KB |
Output is correct |
19 |
Correct |
5 ms |
15196 KB |
Output is correct |
20 |
Correct |
4 ms |
15196 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
4 ms |
15196 KB |
Output is correct |
23 |
Correct |
5 ms |
15168 KB |
Output is correct |
24 |
Correct |
5 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
6 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
15332 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
15196 KB |
Output is correct |
31 |
Correct |
5 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
393 ms |
38236 KB |
Output is correct |
5 |
Correct |
225 ms |
30524 KB |
Output is correct |
6 |
Correct |
157 ms |
30528 KB |
Output is correct |
7 |
Correct |
97 ms |
21716 KB |
Output is correct |
8 |
Correct |
111 ms |
27584 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
15040 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
5 ms |
15196 KB |
Output is correct |
25 |
Correct |
5 ms |
15284 KB |
Output is correct |
26 |
Correct |
6 ms |
15192 KB |
Output is correct |
27 |
Correct |
5 ms |
15196 KB |
Output is correct |
28 |
Correct |
4 ms |
15196 KB |
Output is correct |
29 |
Correct |
5 ms |
15196 KB |
Output is correct |
30 |
Correct |
4 ms |
15196 KB |
Output is correct |
31 |
Correct |
5 ms |
15168 KB |
Output is correct |
32 |
Correct |
5 ms |
15196 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
14940 KB |
Output is correct |
35 |
Correct |
6 ms |
15196 KB |
Output is correct |
36 |
Correct |
5 ms |
15332 KB |
Output is correct |
37 |
Correct |
5 ms |
15196 KB |
Output is correct |
38 |
Correct |
5 ms |
15196 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
481 ms |
44232 KB |
Output is correct |
41 |
Correct |
250 ms |
33108 KB |
Output is correct |
42 |
Correct |
320 ms |
42948 KB |
Output is correct |
43 |
Correct |
313 ms |
42692 KB |
Output is correct |
44 |
Correct |
183 ms |
33028 KB |
Output is correct |
45 |
Correct |
224 ms |
33996 KB |
Output is correct |
46 |
Correct |
80 ms |
21444 KB |
Output is correct |
47 |
Correct |
232 ms |
34876 KB |
Output is correct |
48 |
Correct |
295 ms |
35008 KB |
Output is correct |