#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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
17236 KB |
Output is correct |
2 |
Correct |
10 ms |
17252 KB |
Output is correct |
3 |
Correct |
9 ms |
17344 KB |
Output is correct |
4 |
Correct |
847 ms |
83040 KB |
Output is correct |
5 |
Correct |
476 ms |
65336 KB |
Output is correct |
6 |
Correct |
237 ms |
61124 KB |
Output is correct |
7 |
Correct |
366 ms |
75188 KB |
Output is correct |
8 |
Correct |
219 ms |
74880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
17344 KB |
Output is correct |
2 |
Correct |
11 ms |
17312 KB |
Output is correct |
3 |
Correct |
8 ms |
17360 KB |
Output is correct |
4 |
Correct |
11 ms |
17236 KB |
Output is correct |
5 |
Correct |
9 ms |
17236 KB |
Output is correct |
6 |
Correct |
12 ms |
17236 KB |
Output is correct |
7 |
Correct |
8 ms |
17328 KB |
Output is correct |
8 |
Correct |
10 ms |
17244 KB |
Output is correct |
9 |
Correct |
10 ms |
17336 KB |
Output is correct |
10 |
Correct |
9 ms |
17264 KB |
Output is correct |
11 |
Correct |
8 ms |
17336 KB |
Output is correct |
12 |
Correct |
10 ms |
17364 KB |
Output is correct |
13 |
Correct |
10 ms |
17468 KB |
Output is correct |
14 |
Correct |
10 ms |
17460 KB |
Output is correct |
15 |
Correct |
9 ms |
17428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
17344 KB |
Output is correct |
2 |
Correct |
11 ms |
17312 KB |
Output is correct |
3 |
Correct |
8 ms |
17360 KB |
Output is correct |
4 |
Correct |
11 ms |
17236 KB |
Output is correct |
5 |
Correct |
9 ms |
17236 KB |
Output is correct |
6 |
Correct |
12 ms |
17236 KB |
Output is correct |
7 |
Correct |
8 ms |
17328 KB |
Output is correct |
8 |
Correct |
10 ms |
17244 KB |
Output is correct |
9 |
Correct |
10 ms |
17336 KB |
Output is correct |
10 |
Correct |
9 ms |
17264 KB |
Output is correct |
11 |
Correct |
8 ms |
17336 KB |
Output is correct |
12 |
Correct |
10 ms |
17364 KB |
Output is correct |
13 |
Correct |
10 ms |
17468 KB |
Output is correct |
14 |
Correct |
10 ms |
17460 KB |
Output is correct |
15 |
Correct |
9 ms |
17428 KB |
Output is correct |
16 |
Correct |
13 ms |
18128 KB |
Output is correct |
17 |
Correct |
16 ms |
18004 KB |
Output is correct |
18 |
Correct |
15 ms |
18208 KB |
Output is correct |
19 |
Correct |
14 ms |
18252 KB |
Output is correct |
20 |
Correct |
12 ms |
17992 KB |
Output is correct |
21 |
Correct |
16 ms |
18004 KB |
Output is correct |
22 |
Correct |
12 ms |
18260 KB |
Output is correct |
23 |
Correct |
15 ms |
18132 KB |
Output is correct |
24 |
Correct |
16 ms |
18120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
17344 KB |
Output is correct |
2 |
Correct |
11 ms |
17312 KB |
Output is correct |
3 |
Correct |
8 ms |
17360 KB |
Output is correct |
4 |
Correct |
11 ms |
17236 KB |
Output is correct |
5 |
Correct |
9 ms |
17236 KB |
Output is correct |
6 |
Correct |
12 ms |
17236 KB |
Output is correct |
7 |
Correct |
8 ms |
17328 KB |
Output is correct |
8 |
Correct |
10 ms |
17244 KB |
Output is correct |
9 |
Correct |
10 ms |
17336 KB |
Output is correct |
10 |
Correct |
9 ms |
17264 KB |
Output is correct |
11 |
Correct |
8 ms |
17336 KB |
Output is correct |
12 |
Correct |
10 ms |
17364 KB |
Output is correct |
13 |
Correct |
10 ms |
17468 KB |
Output is correct |
14 |
Correct |
10 ms |
17460 KB |
Output is correct |
15 |
Correct |
9 ms |
17428 KB |
Output is correct |
16 |
Correct |
13 ms |
18128 KB |
Output is correct |
17 |
Correct |
16 ms |
18004 KB |
Output is correct |
18 |
Correct |
15 ms |
18208 KB |
Output is correct |
19 |
Correct |
14 ms |
18252 KB |
Output is correct |
20 |
Correct |
12 ms |
17992 KB |
Output is correct |
21 |
Correct |
16 ms |
18004 KB |
Output is correct |
22 |
Correct |
12 ms |
18260 KB |
Output is correct |
23 |
Correct |
15 ms |
18132 KB |
Output is correct |
24 |
Correct |
16 ms |
18120 KB |
Output is correct |
25 |
Correct |
8 ms |
17356 KB |
Output is correct |
26 |
Correct |
9 ms |
17236 KB |
Output is correct |
27 |
Correct |
13 ms |
18124 KB |
Output is correct |
28 |
Correct |
15 ms |
18056 KB |
Output is correct |
29 |
Correct |
13 ms |
18004 KB |
Output is correct |
30 |
Correct |
12 ms |
18132 KB |
Output is correct |
31 |
Correct |
16 ms |
18132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
17236 KB |
Output is correct |
2 |
Correct |
10 ms |
17252 KB |
Output is correct |
3 |
Correct |
9 ms |
17344 KB |
Output is correct |
4 |
Correct |
847 ms |
83040 KB |
Output is correct |
5 |
Correct |
476 ms |
65336 KB |
Output is correct |
6 |
Correct |
237 ms |
61124 KB |
Output is correct |
7 |
Correct |
366 ms |
75188 KB |
Output is correct |
8 |
Correct |
219 ms |
74880 KB |
Output is correct |
9 |
Correct |
11 ms |
17344 KB |
Output is correct |
10 |
Correct |
11 ms |
17312 KB |
Output is correct |
11 |
Correct |
8 ms |
17360 KB |
Output is correct |
12 |
Correct |
11 ms |
17236 KB |
Output is correct |
13 |
Correct |
9 ms |
17236 KB |
Output is correct |
14 |
Correct |
12 ms |
17236 KB |
Output is correct |
15 |
Correct |
8 ms |
17328 KB |
Output is correct |
16 |
Correct |
10 ms |
17244 KB |
Output is correct |
17 |
Correct |
10 ms |
17336 KB |
Output is correct |
18 |
Correct |
9 ms |
17264 KB |
Output is correct |
19 |
Correct |
8 ms |
17336 KB |
Output is correct |
20 |
Correct |
10 ms |
17364 KB |
Output is correct |
21 |
Correct |
10 ms |
17468 KB |
Output is correct |
22 |
Correct |
10 ms |
17460 KB |
Output is correct |
23 |
Correct |
9 ms |
17428 KB |
Output is correct |
24 |
Correct |
13 ms |
18128 KB |
Output is correct |
25 |
Correct |
16 ms |
18004 KB |
Output is correct |
26 |
Correct |
15 ms |
18208 KB |
Output is correct |
27 |
Correct |
14 ms |
18252 KB |
Output is correct |
28 |
Correct |
12 ms |
17992 KB |
Output is correct |
29 |
Correct |
16 ms |
18004 KB |
Output is correct |
30 |
Correct |
12 ms |
18260 KB |
Output is correct |
31 |
Correct |
15 ms |
18132 KB |
Output is correct |
32 |
Correct |
16 ms |
18120 KB |
Output is correct |
33 |
Correct |
8 ms |
17356 KB |
Output is correct |
34 |
Correct |
9 ms |
17236 KB |
Output is correct |
35 |
Correct |
13 ms |
18124 KB |
Output is correct |
36 |
Correct |
15 ms |
18056 KB |
Output is correct |
37 |
Correct |
13 ms |
18004 KB |
Output is correct |
38 |
Correct |
12 ms |
18132 KB |
Output is correct |
39 |
Correct |
16 ms |
18132 KB |
Output is correct |
40 |
Correct |
989 ms |
84272 KB |
Output is correct |
41 |
Correct |
509 ms |
65304 KB |
Output is correct |
42 |
Correct |
597 ms |
92508 KB |
Output is correct |
43 |
Correct |
643 ms |
92240 KB |
Output is correct |
44 |
Correct |
242 ms |
61068 KB |
Output is correct |
45 |
Correct |
323 ms |
67000 KB |
Output is correct |
46 |
Correct |
181 ms |
40128 KB |
Output is correct |
47 |
Correct |
534 ms |
73252 KB |
Output is correct |
48 |
Correct |
416 ms |
77056 KB |
Output is correct |