#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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n; C = n;
int L[n+1], R[n+1];
build();
for (int i = 1; i <= n; ++i) {
cin >> L[i] >> R[i];
Add(L[i], R[i], i);
}
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<>> pq;
fill(di, di+C+1, 1e9);
for (int i = 1; i <= n; ++i) {
if (L[i] == 1) {
ri[i] = R[i], di[i] = 1;
pq.push({1, i});
}
}
while (!pq.empty()) {
auto [D, 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;
pq.push({di[u], u});
}
if (di[u] == di[s] + w) ri[u] = max({ri[u], ri[s], Ri});
}
}
multiset<int> ms, vs;
for (int i = 1; i <= n; ++i) ms.insert(R[i]);
fill(f, f+n+1, 1e9);
f[n] = 0;
vs.insert(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--;
}
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
cout << (di[x] + f[ri[x]] > 5e5 ? -1 : di[x] + f[ri[x]]) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
28240 KB |
Output is correct |
2 |
Correct |
6 ms |
28240 KB |
Output is correct |
3 |
Correct |
6 ms |
28356 KB |
Output is correct |
4 |
Correct |
592 ms |
102692 KB |
Output is correct |
5 |
Correct |
213 ms |
72780 KB |
Output is correct |
6 |
Correct |
143 ms |
64328 KB |
Output is correct |
7 |
Correct |
161 ms |
63924 KB |
Output is correct |
8 |
Correct |
167 ms |
72808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
28240 KB |
Output is correct |
2 |
Correct |
6 ms |
28408 KB |
Output is correct |
3 |
Correct |
6 ms |
28384 KB |
Output is correct |
4 |
Correct |
6 ms |
28412 KB |
Output is correct |
5 |
Correct |
6 ms |
28240 KB |
Output is correct |
6 |
Correct |
6 ms |
28240 KB |
Output is correct |
7 |
Correct |
6 ms |
28240 KB |
Output is correct |
8 |
Correct |
6 ms |
28240 KB |
Output is correct |
9 |
Correct |
7 ms |
28240 KB |
Output is correct |
10 |
Correct |
6 ms |
28240 KB |
Output is correct |
11 |
Correct |
6 ms |
28240 KB |
Output is correct |
12 |
Correct |
7 ms |
28240 KB |
Output is correct |
13 |
Correct |
6 ms |
28240 KB |
Output is correct |
14 |
Correct |
6 ms |
28240 KB |
Output is correct |
15 |
Correct |
6 ms |
28312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
28240 KB |
Output is correct |
2 |
Correct |
6 ms |
28408 KB |
Output is correct |
3 |
Correct |
6 ms |
28384 KB |
Output is correct |
4 |
Correct |
6 ms |
28412 KB |
Output is correct |
5 |
Correct |
6 ms |
28240 KB |
Output is correct |
6 |
Correct |
6 ms |
28240 KB |
Output is correct |
7 |
Correct |
6 ms |
28240 KB |
Output is correct |
8 |
Correct |
6 ms |
28240 KB |
Output is correct |
9 |
Correct |
7 ms |
28240 KB |
Output is correct |
10 |
Correct |
6 ms |
28240 KB |
Output is correct |
11 |
Correct |
6 ms |
28240 KB |
Output is correct |
12 |
Correct |
7 ms |
28240 KB |
Output is correct |
13 |
Correct |
6 ms |
28240 KB |
Output is correct |
14 |
Correct |
6 ms |
28240 KB |
Output is correct |
15 |
Correct |
6 ms |
28312 KB |
Output is correct |
16 |
Correct |
10 ms |
28752 KB |
Output is correct |
17 |
Correct |
8 ms |
28752 KB |
Output is correct |
18 |
Correct |
8 ms |
29216 KB |
Output is correct |
19 |
Correct |
9 ms |
29008 KB |
Output is correct |
20 |
Correct |
11 ms |
28752 KB |
Output is correct |
21 |
Correct |
8 ms |
28752 KB |
Output is correct |
22 |
Correct |
7 ms |
28752 KB |
Output is correct |
23 |
Correct |
9 ms |
28752 KB |
Output is correct |
24 |
Correct |
9 ms |
28752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
28240 KB |
Output is correct |
2 |
Correct |
6 ms |
28408 KB |
Output is correct |
3 |
Correct |
6 ms |
28384 KB |
Output is correct |
4 |
Correct |
6 ms |
28412 KB |
Output is correct |
5 |
Correct |
6 ms |
28240 KB |
Output is correct |
6 |
Correct |
6 ms |
28240 KB |
Output is correct |
7 |
Correct |
6 ms |
28240 KB |
Output is correct |
8 |
Correct |
6 ms |
28240 KB |
Output is correct |
9 |
Correct |
7 ms |
28240 KB |
Output is correct |
10 |
Correct |
6 ms |
28240 KB |
Output is correct |
11 |
Correct |
6 ms |
28240 KB |
Output is correct |
12 |
Correct |
7 ms |
28240 KB |
Output is correct |
13 |
Correct |
6 ms |
28240 KB |
Output is correct |
14 |
Correct |
6 ms |
28240 KB |
Output is correct |
15 |
Correct |
6 ms |
28312 KB |
Output is correct |
16 |
Correct |
10 ms |
28752 KB |
Output is correct |
17 |
Correct |
8 ms |
28752 KB |
Output is correct |
18 |
Correct |
8 ms |
29216 KB |
Output is correct |
19 |
Correct |
9 ms |
29008 KB |
Output is correct |
20 |
Correct |
11 ms |
28752 KB |
Output is correct |
21 |
Correct |
8 ms |
28752 KB |
Output is correct |
22 |
Correct |
7 ms |
28752 KB |
Output is correct |
23 |
Correct |
9 ms |
28752 KB |
Output is correct |
24 |
Correct |
9 ms |
28752 KB |
Output is correct |
25 |
Correct |
7 ms |
28240 KB |
Output is correct |
26 |
Correct |
6 ms |
28240 KB |
Output is correct |
27 |
Incorrect |
9 ms |
29008 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
28240 KB |
Output is correct |
2 |
Correct |
6 ms |
28240 KB |
Output is correct |
3 |
Correct |
6 ms |
28356 KB |
Output is correct |
4 |
Correct |
592 ms |
102692 KB |
Output is correct |
5 |
Correct |
213 ms |
72780 KB |
Output is correct |
6 |
Correct |
143 ms |
64328 KB |
Output is correct |
7 |
Correct |
161 ms |
63924 KB |
Output is correct |
8 |
Correct |
167 ms |
72808 KB |
Output is correct |
9 |
Correct |
6 ms |
28240 KB |
Output is correct |
10 |
Correct |
6 ms |
28408 KB |
Output is correct |
11 |
Correct |
6 ms |
28384 KB |
Output is correct |
12 |
Correct |
6 ms |
28412 KB |
Output is correct |
13 |
Correct |
6 ms |
28240 KB |
Output is correct |
14 |
Correct |
6 ms |
28240 KB |
Output is correct |
15 |
Correct |
6 ms |
28240 KB |
Output is correct |
16 |
Correct |
6 ms |
28240 KB |
Output is correct |
17 |
Correct |
7 ms |
28240 KB |
Output is correct |
18 |
Correct |
6 ms |
28240 KB |
Output is correct |
19 |
Correct |
6 ms |
28240 KB |
Output is correct |
20 |
Correct |
7 ms |
28240 KB |
Output is correct |
21 |
Correct |
6 ms |
28240 KB |
Output is correct |
22 |
Correct |
6 ms |
28240 KB |
Output is correct |
23 |
Correct |
6 ms |
28312 KB |
Output is correct |
24 |
Correct |
10 ms |
28752 KB |
Output is correct |
25 |
Correct |
8 ms |
28752 KB |
Output is correct |
26 |
Correct |
8 ms |
29216 KB |
Output is correct |
27 |
Correct |
9 ms |
29008 KB |
Output is correct |
28 |
Correct |
11 ms |
28752 KB |
Output is correct |
29 |
Correct |
8 ms |
28752 KB |
Output is correct |
30 |
Correct |
7 ms |
28752 KB |
Output is correct |
31 |
Correct |
9 ms |
28752 KB |
Output is correct |
32 |
Correct |
9 ms |
28752 KB |
Output is correct |
33 |
Correct |
7 ms |
28240 KB |
Output is correct |
34 |
Correct |
6 ms |
28240 KB |
Output is correct |
35 |
Incorrect |
9 ms |
29008 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |