#include "bits/stdc++.h"
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define ar array
#define all(v) (v).begin(), (v).end()
#define sz(v) static_cast<int>(v.size())
typedef vector<int> vi;
typedef long long ll;
const int N = 2e5, N_ = 1 << 18;
int n_;
vector<pair<int, int>> g[N_ * 2 + N];
vi getSegs(int l, int r) {
vi v;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if (l & 1) v.push_back(l++);
if (~r & 1) v.push_back(r--);
}
return v;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin.exceptions(cin.failbit);
int n;
cin >> n;
n_ = 1;
while (n_ < n) n_ <<= 1;
F0R(i, n) {
int l, r;
cin >> l >> r, l--, r--;
g[n_ * 2 + i].push_back({n_ + i, 1});
for (int v : getSegs(l, r)) g[v].push_back({n_ * 2 + i, 0});
}
FOR(i, 1, n_) {
g[i << 1 | 0].push_back({i, 0});
g[i << 1 | 1].push_back({i, 0});
}
static int d1[N_ * 2 + N], dn[N_ * 2 + N], d[N_ * 2 + N];
FOR(i, 1, n_ * 2 + n)
d1[i] = dn[i] = d[i] = N_;
priority_queue<pair<int, int>> pq;
pq.push({-(d1[n_ + 0] = 0), n_ + 0});
while (sz(pq)) {
auto [d_, i] = pq.top();
pq.pop();
d_ *= -1;
if (d_ != d1[i]) continue;
for (auto [j, w] : g[i])
if (d1[i] + w < d1[j]) pq.push({-(d1[j] = d1[i] + w), j});
}
pq.push({-(dn[n_ + n - 1] = 0), n_ + n - 1});
while (sz(pq)) {
auto [d_, i] = pq.top();
pq.pop();
d_ *= -1;
if (d_ != dn[i]) continue;
for (auto [j, w] : g[i])
if (dn[i] + w < dn[j]) pq.push({-(dn[j] = dn[i] + w), j});
}
FOR(i, 1, n_ * 2 + n) {
d[i] = min(d[i], d1[i] + dn[i]);
pq.push({-d[i], i});
}
while (sz(pq)) {
auto [d_, i] = pq.top();
pq.pop();
d_ *= -1;
if (d_ != d[i]) continue;
for (auto [j, w] : g[i])
if (d[i] + w < d[j]) pq.push({-(d[j] = d[i] + w), j});
}
int q;
cin >> q;
F0R(h, q) {
int i;
cin >> i, i--;
cout << (d[n_ + i] == N_ ? -1 : d[n_ + i]) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23204 KB |
Output is correct |
2 |
Correct |
6 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
877 ms |
85640 KB |
Output is correct |
5 |
Correct |
435 ms |
66452 KB |
Output is correct |
6 |
Correct |
198 ms |
61948 KB |
Output is correct |
7 |
Correct |
361 ms |
77112 KB |
Output is correct |
8 |
Correct |
212 ms |
75500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
6 ms |
23208 KB |
Output is correct |
3 |
Correct |
5 ms |
23160 KB |
Output is correct |
4 |
Correct |
5 ms |
23204 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23208 KB |
Output is correct |
8 |
Correct |
5 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
5 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
6 ms |
23208 KB |
Output is correct |
3 |
Correct |
5 ms |
23160 KB |
Output is correct |
4 |
Correct |
5 ms |
23204 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23208 KB |
Output is correct |
8 |
Correct |
5 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
5 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23196 KB |
Output is correct |
16 |
Correct |
15 ms |
23896 KB |
Output is correct |
17 |
Correct |
9 ms |
23896 KB |
Output is correct |
18 |
Correct |
11 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23644 KB |
Output is correct |
21 |
Correct |
17 ms |
23716 KB |
Output is correct |
22 |
Correct |
19 ms |
23900 KB |
Output is correct |
23 |
Correct |
12 ms |
23896 KB |
Output is correct |
24 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
6 ms |
23208 KB |
Output is correct |
3 |
Correct |
5 ms |
23160 KB |
Output is correct |
4 |
Correct |
5 ms |
23204 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
5 ms |
23208 KB |
Output is correct |
8 |
Correct |
5 ms |
23132 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
5 ms |
23132 KB |
Output is correct |
12 |
Correct |
5 ms |
23132 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23196 KB |
Output is correct |
16 |
Correct |
15 ms |
23896 KB |
Output is correct |
17 |
Correct |
9 ms |
23896 KB |
Output is correct |
18 |
Correct |
11 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23644 KB |
Output is correct |
21 |
Correct |
17 ms |
23716 KB |
Output is correct |
22 |
Correct |
19 ms |
23900 KB |
Output is correct |
23 |
Correct |
12 ms |
23896 KB |
Output is correct |
24 |
Correct |
10 ms |
23900 KB |
Output is correct |
25 |
Correct |
6 ms |
23132 KB |
Output is correct |
26 |
Correct |
5 ms |
22940 KB |
Output is correct |
27 |
Correct |
10 ms |
23896 KB |
Output is correct |
28 |
Correct |
9 ms |
23776 KB |
Output is correct |
29 |
Correct |
7 ms |
23632 KB |
Output is correct |
30 |
Correct |
10 ms |
23640 KB |
Output is correct |
31 |
Correct |
9 ms |
23724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23204 KB |
Output is correct |
2 |
Correct |
6 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23132 KB |
Output is correct |
4 |
Correct |
877 ms |
85640 KB |
Output is correct |
5 |
Correct |
435 ms |
66452 KB |
Output is correct |
6 |
Correct |
198 ms |
61948 KB |
Output is correct |
7 |
Correct |
361 ms |
77112 KB |
Output is correct |
8 |
Correct |
212 ms |
75500 KB |
Output is correct |
9 |
Correct |
5 ms |
23132 KB |
Output is correct |
10 |
Correct |
6 ms |
23208 KB |
Output is correct |
11 |
Correct |
5 ms |
23160 KB |
Output is correct |
12 |
Correct |
5 ms |
23204 KB |
Output is correct |
13 |
Correct |
5 ms |
23132 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
5 ms |
23208 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
5 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23132 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
5 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23132 KB |
Output is correct |
22 |
Correct |
5 ms |
23132 KB |
Output is correct |
23 |
Correct |
6 ms |
23196 KB |
Output is correct |
24 |
Correct |
15 ms |
23896 KB |
Output is correct |
25 |
Correct |
9 ms |
23896 KB |
Output is correct |
26 |
Correct |
11 ms |
23896 KB |
Output is correct |
27 |
Correct |
12 ms |
23900 KB |
Output is correct |
28 |
Correct |
14 ms |
23644 KB |
Output is correct |
29 |
Correct |
17 ms |
23716 KB |
Output is correct |
30 |
Correct |
19 ms |
23900 KB |
Output is correct |
31 |
Correct |
12 ms |
23896 KB |
Output is correct |
32 |
Correct |
10 ms |
23900 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
5 ms |
22940 KB |
Output is correct |
35 |
Correct |
10 ms |
23896 KB |
Output is correct |
36 |
Correct |
9 ms |
23776 KB |
Output is correct |
37 |
Correct |
7 ms |
23632 KB |
Output is correct |
38 |
Correct |
10 ms |
23640 KB |
Output is correct |
39 |
Correct |
9 ms |
23724 KB |
Output is correct |
40 |
Correct |
1031 ms |
86232 KB |
Output is correct |
41 |
Correct |
491 ms |
66236 KB |
Output is correct |
42 |
Correct |
570 ms |
93612 KB |
Output is correct |
43 |
Correct |
574 ms |
92424 KB |
Output is correct |
44 |
Correct |
272 ms |
63204 KB |
Output is correct |
45 |
Correct |
367 ms |
69004 KB |
Output is correct |
46 |
Correct |
217 ms |
44240 KB |
Output is correct |
47 |
Correct |
664 ms |
75124 KB |
Output is correct |
48 |
Correct |
468 ms |
78372 KB |
Output is correct |