#include <bits/stdc++.h>
using namespace std;
const int N = 8e5+5;
vector<array<int, 3>> g[N];
bool vis[N];
int di[N], ri[N], lc[N], rc[N], n, C;
long long f[N];
void add(int u, int v, int x, int R = -1) {
g[u].push_back({v, x, R});
}
int build(int l = 1, int r = n) {
if (l == r) return l;
int m = (l+r)/2, nd = ++C;
lc[nd] = build(l, m);
rc[nd] = build(m+1, r);
add(lc[nd], nd, 0);
add(rc[nd], nd, 0);
return nd;
}
void Add(int l, int r, int u, int i = n+1, int l2 = 1, int r2 = n) {
if (l <= l2 && r2 <= r) {
add(i, u, 1, r);
return;
}
int m2 = (l2 + r2)/2;
if (l <= m2) Add(l, r, u, lc[i], l2, m2);
if (m2+1 <= r) Add(l, r, u, rc[i], m2+1, r2);
}
int L[N], R[N], ans[N][2];
void resiretarda(int x) {
for (int i = 1; i <= C; ++i) {
g[i].clear();
}
C = n;
build();
for (int i = 1; i <= n; ++i) Add(L[i], R[i], i);
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> pq;
fill(di, di+C+1, 1e9);
fill(vis, vis+C+1, 0);
fill(ri, ri+C+1, 0);
for (int i = 1; i <= n; ++i) {
if (L[i] == 1) {
ri[i] = R[i], di[i] = 1;
pq.push({1, -ri[i], i});
}
}
while (!pq.empty()) {
auto [D, RIII, s] = pq.top(); pq.pop();
if (vis[s]) continue;
vis[s] = 1;
for (auto [u, w, Ri] : g[s]) {
if (di[u] > di[s] + w) {
di[u] = di[s] + w;
}
if (di[u] == di[s] + w) {
ri[u] = max({ri[u], -RIII, Ri});
pq.push({di[u], -ri[u], u});
}
}
}
multiset<long long> ms, vs;
for (int i = 1; i <= n; ++i) ms.insert(R[i]);
fill(f, f+n+1, 1e9);
f[n] = 0;
for (int i = n; i >= 1; --i) {
if (i != n) {
f[i] = (vs.empty() ? 1e9 : *vs.begin()) +1;
}
vs.insert(f[i]);
if (i == 1) continue;
int RRR = *ms.rbegin();
ms.erase(ms.find(R[i]));
while (RRR != *ms.rbegin()) {
vs.erase(vs.find(f[RRR]));
RRR--;
}
}
for (int i = 1; i <= n; ++i) {
ans[i][x] = di[i] + f[ri[i]];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> L[i] >> R[i];
}
resiretarda(0);
for (int i = 1; i <= n; ++i) {
int X = L[i];
L[i] = n+1-R[i], R[i] = n+1-X;
}
reverse(L+1, L+n+1);
reverse(R+1, R+n+1);
resiretarda(1);
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
cout << (min(ans[x][0], ans[n-x+1][1]) > 5e5 ? -1 : min(ans[x][0], ans[n-x+1][1])) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
7 ms |
34756 KB |
Output is correct |
3 |
Correct |
6 ms |
34640 KB |
Output is correct |
4 |
Correct |
1414 ms |
129896 KB |
Output is correct |
5 |
Correct |
436 ms |
81736 KB |
Output is correct |
6 |
Correct |
209 ms |
70592 KB |
Output is correct |
7 |
Correct |
351 ms |
69696 KB |
Output is correct |
8 |
Correct |
269 ms |
81480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34640 KB |
Output is correct |
2 |
Correct |
7 ms |
34640 KB |
Output is correct |
3 |
Correct |
8 ms |
34640 KB |
Output is correct |
4 |
Correct |
7 ms |
34640 KB |
Output is correct |
5 |
Correct |
7 ms |
34732 KB |
Output is correct |
6 |
Correct |
7 ms |
34748 KB |
Output is correct |
7 |
Correct |
7 ms |
34640 KB |
Output is correct |
8 |
Correct |
6 ms |
34640 KB |
Output is correct |
9 |
Correct |
6 ms |
34640 KB |
Output is correct |
10 |
Correct |
7 ms |
34732 KB |
Output is correct |
11 |
Correct |
7 ms |
34640 KB |
Output is correct |
12 |
Correct |
7 ms |
34640 KB |
Output is correct |
13 |
Correct |
6 ms |
34640 KB |
Output is correct |
14 |
Correct |
6 ms |
34640 KB |
Output is correct |
15 |
Correct |
6 ms |
34648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34640 KB |
Output is correct |
2 |
Correct |
7 ms |
34640 KB |
Output is correct |
3 |
Correct |
8 ms |
34640 KB |
Output is correct |
4 |
Correct |
7 ms |
34640 KB |
Output is correct |
5 |
Correct |
7 ms |
34732 KB |
Output is correct |
6 |
Correct |
7 ms |
34748 KB |
Output is correct |
7 |
Correct |
7 ms |
34640 KB |
Output is correct |
8 |
Correct |
6 ms |
34640 KB |
Output is correct |
9 |
Correct |
6 ms |
34640 KB |
Output is correct |
10 |
Correct |
7 ms |
34732 KB |
Output is correct |
11 |
Correct |
7 ms |
34640 KB |
Output is correct |
12 |
Correct |
7 ms |
34640 KB |
Output is correct |
13 |
Correct |
6 ms |
34640 KB |
Output is correct |
14 |
Correct |
6 ms |
34640 KB |
Output is correct |
15 |
Correct |
6 ms |
34648 KB |
Output is correct |
16 |
Correct |
14 ms |
35408 KB |
Output is correct |
17 |
Correct |
10 ms |
35152 KB |
Output is correct |
18 |
Correct |
12 ms |
35512 KB |
Output is correct |
19 |
Correct |
11 ms |
35408 KB |
Output is correct |
20 |
Correct |
8 ms |
34896 KB |
Output is correct |
21 |
Correct |
9 ms |
35152 KB |
Output is correct |
22 |
Correct |
9 ms |
34896 KB |
Output is correct |
23 |
Correct |
11 ms |
35152 KB |
Output is correct |
24 |
Correct |
10 ms |
35320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34640 KB |
Output is correct |
2 |
Correct |
7 ms |
34640 KB |
Output is correct |
3 |
Correct |
8 ms |
34640 KB |
Output is correct |
4 |
Correct |
7 ms |
34640 KB |
Output is correct |
5 |
Correct |
7 ms |
34732 KB |
Output is correct |
6 |
Correct |
7 ms |
34748 KB |
Output is correct |
7 |
Correct |
7 ms |
34640 KB |
Output is correct |
8 |
Correct |
6 ms |
34640 KB |
Output is correct |
9 |
Correct |
6 ms |
34640 KB |
Output is correct |
10 |
Correct |
7 ms |
34732 KB |
Output is correct |
11 |
Correct |
7 ms |
34640 KB |
Output is correct |
12 |
Correct |
7 ms |
34640 KB |
Output is correct |
13 |
Correct |
6 ms |
34640 KB |
Output is correct |
14 |
Correct |
6 ms |
34640 KB |
Output is correct |
15 |
Correct |
6 ms |
34648 KB |
Output is correct |
16 |
Correct |
14 ms |
35408 KB |
Output is correct |
17 |
Correct |
10 ms |
35152 KB |
Output is correct |
18 |
Correct |
12 ms |
35512 KB |
Output is correct |
19 |
Correct |
11 ms |
35408 KB |
Output is correct |
20 |
Correct |
8 ms |
34896 KB |
Output is correct |
21 |
Correct |
9 ms |
35152 KB |
Output is correct |
22 |
Correct |
9 ms |
34896 KB |
Output is correct |
23 |
Correct |
11 ms |
35152 KB |
Output is correct |
24 |
Correct |
10 ms |
35320 KB |
Output is correct |
25 |
Correct |
6 ms |
34808 KB |
Output is correct |
26 |
Correct |
6 ms |
34508 KB |
Output is correct |
27 |
Correct |
18 ms |
35768 KB |
Output is correct |
28 |
Correct |
10 ms |
35152 KB |
Output is correct |
29 |
Correct |
9 ms |
35152 KB |
Output is correct |
30 |
Correct |
9 ms |
35152 KB |
Output is correct |
31 |
Correct |
10 ms |
35152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
7 ms |
34756 KB |
Output is correct |
3 |
Correct |
6 ms |
34640 KB |
Output is correct |
4 |
Correct |
1414 ms |
129896 KB |
Output is correct |
5 |
Correct |
436 ms |
81736 KB |
Output is correct |
6 |
Correct |
209 ms |
70592 KB |
Output is correct |
7 |
Correct |
351 ms |
69696 KB |
Output is correct |
8 |
Correct |
269 ms |
81480 KB |
Output is correct |
9 |
Correct |
8 ms |
34640 KB |
Output is correct |
10 |
Correct |
7 ms |
34640 KB |
Output is correct |
11 |
Correct |
8 ms |
34640 KB |
Output is correct |
12 |
Correct |
7 ms |
34640 KB |
Output is correct |
13 |
Correct |
7 ms |
34732 KB |
Output is correct |
14 |
Correct |
7 ms |
34748 KB |
Output is correct |
15 |
Correct |
7 ms |
34640 KB |
Output is correct |
16 |
Correct |
6 ms |
34640 KB |
Output is correct |
17 |
Correct |
6 ms |
34640 KB |
Output is correct |
18 |
Correct |
7 ms |
34732 KB |
Output is correct |
19 |
Correct |
7 ms |
34640 KB |
Output is correct |
20 |
Correct |
7 ms |
34640 KB |
Output is correct |
21 |
Correct |
6 ms |
34640 KB |
Output is correct |
22 |
Correct |
6 ms |
34640 KB |
Output is correct |
23 |
Correct |
6 ms |
34648 KB |
Output is correct |
24 |
Correct |
14 ms |
35408 KB |
Output is correct |
25 |
Correct |
10 ms |
35152 KB |
Output is correct |
26 |
Correct |
12 ms |
35512 KB |
Output is correct |
27 |
Correct |
11 ms |
35408 KB |
Output is correct |
28 |
Correct |
8 ms |
34896 KB |
Output is correct |
29 |
Correct |
9 ms |
35152 KB |
Output is correct |
30 |
Correct |
9 ms |
34896 KB |
Output is correct |
31 |
Correct |
11 ms |
35152 KB |
Output is correct |
32 |
Correct |
10 ms |
35320 KB |
Output is correct |
33 |
Correct |
6 ms |
34808 KB |
Output is correct |
34 |
Correct |
6 ms |
34508 KB |
Output is correct |
35 |
Correct |
18 ms |
35768 KB |
Output is correct |
36 |
Correct |
10 ms |
35152 KB |
Output is correct |
37 |
Correct |
9 ms |
35152 KB |
Output is correct |
38 |
Correct |
9 ms |
35152 KB |
Output is correct |
39 |
Correct |
10 ms |
35152 KB |
Output is correct |
40 |
Correct |
1611 ms |
149488 KB |
Output is correct |
41 |
Correct |
435 ms |
87236 KB |
Output is correct |
42 |
Correct |
567 ms |
124612 KB |
Output is correct |
43 |
Correct |
649 ms |
125848 KB |
Output is correct |
44 |
Correct |
244 ms |
74312 KB |
Output is correct |
45 |
Correct |
309 ms |
86464 KB |
Output is correct |
46 |
Correct |
198 ms |
55540 KB |
Output is correct |
47 |
Correct |
705 ms |
92276 KB |
Output is correct |
48 |
Correct |
684 ms |
100240 KB |
Output is correct |