#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 200008;
const int INF = 1e9;
int n, q, d[4 * N], leaf[N], ll[N], rr[N];
vector<pair<int, int>> g[4 * N];
void ae(int u, int v, int w) {
g[u].push_back({v, w});
}
struct SegTree {
int size = 1;
void init(int n) {
while (size < n) size *= 2;
}
void build(int l, int r, int id) {
if (l == r) {
leaf[l] = id;
return;
}
int mid = (l + r) / 2;
build(l, mid, id * 2);
build(mid + 1, r, id * 2 + 1);
ae(id * 2, id, 0);
ae(id * 2 + 1, id, 0);
}
void update(int x, int ql, int qr, int l, int r, int id) {
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
if (l != r) ae(id, leaf[x], 1);
else ae(leaf[l], leaf[x], 1);
return;
}
int mid = (l + r) / 2;
update(x, ql, qr, l, mid, id * 2);
update(x, ql, qr, mid + 1, r, id * 2 + 1);
}
} ST;
vector<int> compute(int s) {
vector<int> dist(4 * N, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0, s});
dist[s] = 0;
while (!pq.empty()) {
auto [dd, u] = pq.top();
pq.pop();
if (dist[u] != dd) continue;
for (auto& [v, w] : g[u]) {
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
return dist;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
ST.init(n + 1);
ST.build(1, n, 1);
for (int i = 1; i <= n; i++) {
cin >> ll[i] >> rr[i];
ST.update(i, ll[i], rr[i], 1, n, 1);
}
vector<int> d1 = compute(leaf[1]);
vector<int> dn = compute(leaf[n]);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
for (int i = 0; i < 4 * N; i++) d[i] = INF;
for (int i = 1; i <= n; i++) {
d[leaf[i]] = d1[leaf[i]] + dn[leaf[i]] - 1;
if (ll[i] == 1 && rr[i] == n) d[leaf[i]] = 1;
else if (ll[i] == 1) d[leaf[i]] = dn[leaf[i]];
else if (rr[i] == n) d[leaf[i]] = d1[leaf[i]];
// cout << "dist; " << d1[leaf[i]] << " " << dn[leaf[i]] << '\n';
// if (ll[i] == 1 && rr[i] == n) d[leaf[i]] = 1;
// else if (rr[i] == n) d[leaf[i]]--;
// else if (ll[i] == 1) d[leaf[i]]--;
// cout << d[leaf[i]] << " " << i << '\n';
pq.push({d[leaf[i]], leaf[i]});
}
while (!pq.empty()) {
auto [dd, u] = pq.top();
pq.pop();
if (d[u] != dd) continue;
for (auto& [v, w] : g[u]) {
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
pq.push({d[v], v});
}
}
}
cin >> q;
while (q--) {
int x;
cin >> x;
if (d[leaf[x]] >= INF) cout << "-1\n";
else cout << d[leaf[x]] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
42576 KB |
Output is correct |
2 |
Correct |
8 ms |
42604 KB |
Output is correct |
3 |
Correct |
8 ms |
42576 KB |
Output is correct |
4 |
Correct |
727 ms |
120684 KB |
Output is correct |
5 |
Correct |
321 ms |
79296 KB |
Output is correct |
6 |
Correct |
177 ms |
69048 KB |
Output is correct |
7 |
Correct |
187 ms |
67120 KB |
Output is correct |
8 |
Correct |
165 ms |
97492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
42576 KB |
Output is correct |
2 |
Correct |
9 ms |
42576 KB |
Output is correct |
3 |
Correct |
8 ms |
42576 KB |
Output is correct |
4 |
Correct |
10 ms |
42576 KB |
Output is correct |
5 |
Correct |
8 ms |
42576 KB |
Output is correct |
6 |
Correct |
10 ms |
42576 KB |
Output is correct |
7 |
Correct |
8 ms |
42576 KB |
Output is correct |
8 |
Correct |
8 ms |
42576 KB |
Output is correct |
9 |
Correct |
8 ms |
42580 KB |
Output is correct |
10 |
Correct |
8 ms |
42580 KB |
Output is correct |
11 |
Correct |
9 ms |
42592 KB |
Output is correct |
12 |
Correct |
10 ms |
42580 KB |
Output is correct |
13 |
Correct |
9 ms |
42764 KB |
Output is correct |
14 |
Correct |
8 ms |
42580 KB |
Output is correct |
15 |
Correct |
9 ms |
42580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
42576 KB |
Output is correct |
2 |
Correct |
9 ms |
42576 KB |
Output is correct |
3 |
Correct |
8 ms |
42576 KB |
Output is correct |
4 |
Correct |
10 ms |
42576 KB |
Output is correct |
5 |
Correct |
8 ms |
42576 KB |
Output is correct |
6 |
Correct |
10 ms |
42576 KB |
Output is correct |
7 |
Correct |
8 ms |
42576 KB |
Output is correct |
8 |
Correct |
8 ms |
42576 KB |
Output is correct |
9 |
Correct |
8 ms |
42580 KB |
Output is correct |
10 |
Correct |
8 ms |
42580 KB |
Output is correct |
11 |
Correct |
9 ms |
42592 KB |
Output is correct |
12 |
Correct |
10 ms |
42580 KB |
Output is correct |
13 |
Correct |
9 ms |
42764 KB |
Output is correct |
14 |
Correct |
8 ms |
42580 KB |
Output is correct |
15 |
Correct |
9 ms |
42580 KB |
Output is correct |
16 |
Correct |
17 ms |
43376 KB |
Output is correct |
17 |
Correct |
11 ms |
43092 KB |
Output is correct |
18 |
Correct |
11 ms |
43604 KB |
Output is correct |
19 |
Correct |
16 ms |
43524 KB |
Output is correct |
20 |
Correct |
10 ms |
43092 KB |
Output is correct |
21 |
Correct |
12 ms |
43092 KB |
Output is correct |
22 |
Correct |
9 ms |
42836 KB |
Output is correct |
23 |
Correct |
11 ms |
43092 KB |
Output is correct |
24 |
Correct |
11 ms |
43348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
42576 KB |
Output is correct |
2 |
Correct |
9 ms |
42576 KB |
Output is correct |
3 |
Correct |
8 ms |
42576 KB |
Output is correct |
4 |
Correct |
10 ms |
42576 KB |
Output is correct |
5 |
Correct |
8 ms |
42576 KB |
Output is correct |
6 |
Correct |
10 ms |
42576 KB |
Output is correct |
7 |
Correct |
8 ms |
42576 KB |
Output is correct |
8 |
Correct |
8 ms |
42576 KB |
Output is correct |
9 |
Correct |
8 ms |
42580 KB |
Output is correct |
10 |
Correct |
8 ms |
42580 KB |
Output is correct |
11 |
Correct |
9 ms |
42592 KB |
Output is correct |
12 |
Correct |
10 ms |
42580 KB |
Output is correct |
13 |
Correct |
9 ms |
42764 KB |
Output is correct |
14 |
Correct |
8 ms |
42580 KB |
Output is correct |
15 |
Correct |
9 ms |
42580 KB |
Output is correct |
16 |
Correct |
17 ms |
43376 KB |
Output is correct |
17 |
Correct |
11 ms |
43092 KB |
Output is correct |
18 |
Correct |
11 ms |
43604 KB |
Output is correct |
19 |
Correct |
16 ms |
43524 KB |
Output is correct |
20 |
Correct |
10 ms |
43092 KB |
Output is correct |
21 |
Correct |
12 ms |
43092 KB |
Output is correct |
22 |
Correct |
9 ms |
42836 KB |
Output is correct |
23 |
Correct |
11 ms |
43092 KB |
Output is correct |
24 |
Correct |
11 ms |
43348 KB |
Output is correct |
25 |
Correct |
11 ms |
42712 KB |
Output is correct |
26 |
Correct |
10 ms |
42580 KB |
Output is correct |
27 |
Correct |
11 ms |
43388 KB |
Output is correct |
28 |
Correct |
11 ms |
43092 KB |
Output is correct |
29 |
Correct |
10 ms |
43092 KB |
Output is correct |
30 |
Correct |
11 ms |
43080 KB |
Output is correct |
31 |
Correct |
15 ms |
43204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
42576 KB |
Output is correct |
2 |
Correct |
8 ms |
42604 KB |
Output is correct |
3 |
Correct |
8 ms |
42576 KB |
Output is correct |
4 |
Correct |
727 ms |
120684 KB |
Output is correct |
5 |
Correct |
321 ms |
79296 KB |
Output is correct |
6 |
Correct |
177 ms |
69048 KB |
Output is correct |
7 |
Correct |
187 ms |
67120 KB |
Output is correct |
8 |
Correct |
165 ms |
97492 KB |
Output is correct |
9 |
Correct |
10 ms |
42576 KB |
Output is correct |
10 |
Correct |
9 ms |
42576 KB |
Output is correct |
11 |
Correct |
8 ms |
42576 KB |
Output is correct |
12 |
Correct |
10 ms |
42576 KB |
Output is correct |
13 |
Correct |
8 ms |
42576 KB |
Output is correct |
14 |
Correct |
10 ms |
42576 KB |
Output is correct |
15 |
Correct |
8 ms |
42576 KB |
Output is correct |
16 |
Correct |
8 ms |
42576 KB |
Output is correct |
17 |
Correct |
8 ms |
42580 KB |
Output is correct |
18 |
Correct |
8 ms |
42580 KB |
Output is correct |
19 |
Correct |
9 ms |
42592 KB |
Output is correct |
20 |
Correct |
10 ms |
42580 KB |
Output is correct |
21 |
Correct |
9 ms |
42764 KB |
Output is correct |
22 |
Correct |
8 ms |
42580 KB |
Output is correct |
23 |
Correct |
9 ms |
42580 KB |
Output is correct |
24 |
Correct |
17 ms |
43376 KB |
Output is correct |
25 |
Correct |
11 ms |
43092 KB |
Output is correct |
26 |
Correct |
11 ms |
43604 KB |
Output is correct |
27 |
Correct |
16 ms |
43524 KB |
Output is correct |
28 |
Correct |
10 ms |
43092 KB |
Output is correct |
29 |
Correct |
12 ms |
43092 KB |
Output is correct |
30 |
Correct |
9 ms |
42836 KB |
Output is correct |
31 |
Correct |
11 ms |
43092 KB |
Output is correct |
32 |
Correct |
11 ms |
43348 KB |
Output is correct |
33 |
Correct |
11 ms |
42712 KB |
Output is correct |
34 |
Correct |
10 ms |
42580 KB |
Output is correct |
35 |
Correct |
11 ms |
43388 KB |
Output is correct |
36 |
Correct |
11 ms |
43092 KB |
Output is correct |
37 |
Correct |
10 ms |
43092 KB |
Output is correct |
38 |
Correct |
11 ms |
43080 KB |
Output is correct |
39 |
Correct |
15 ms |
43204 KB |
Output is correct |
40 |
Correct |
788 ms |
129764 KB |
Output is correct |
41 |
Correct |
348 ms |
82752 KB |
Output is correct |
42 |
Correct |
449 ms |
135300 KB |
Output is correct |
43 |
Correct |
442 ms |
137296 KB |
Output is correct |
44 |
Correct |
223 ms |
71992 KB |
Output is correct |
45 |
Correct |
288 ms |
86392 KB |
Output is correct |
46 |
Correct |
168 ms |
58136 KB |
Output is correct |
47 |
Correct |
431 ms |
90392 KB |
Output is correct |
48 |
Correct |
333 ms |
105608 KB |
Output is correct |