#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
const int inf = 1e9;
typedef pair<int, int> pii;
int dist[2][2*MAXN];
int sumdist[2*MAXN];
int n;
vector<pii> edges[2*MAXN];
void build() {
for (int i = 1; i < n; i++) {
edges[2*i].push_back(pii(i, 0));
edges[2*i+1].push_back(pii(i, 0));
}
}
void assign(int i, int l, int r) {
for (; r > l; l /= 2, r /= 2) {
if (l & 1) edges[l++].push_back(pii(i, 1));
if (r & 1) edges[--r].push_back(pii(i, 1));
}
}
void dijkstra(int curdist[]) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 0; i < 2*n; i++)
pq.push(pii(curdist[i], i));
while (!pq.empty()) {
pii tp = pq.top();
pq.pop();
int cur = tp.second;
if (tp.first > curdist[cur]) continue;
for (pii p: edges[cur]) {
int nxtdist = tp.first+p.second;
if (nxtdist < curdist[p.first]) {
curdist[p.first] = nxtdist;
pq.push(pii(nxtdist, p.first));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
build();
fill(dist[0], dist[0]+2*n, inf);
fill(dist[1], dist[1]+2*n, inf);
for (int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
l--; r--;
assign(i+n, l+n, r+n+1);
if (l == 0) dist[0][i+n] = 0;
if (r == n-1) dist[1][i+n] = 0;
}
dijkstra(dist[0]);
dijkstra(dist[1]);
for (int i = 0; i < 2*n; i++) {
sumdist[i] = (i < n) ? inf : min(inf, dist[0][i]+dist[1][i]);
}
// for (int i = n; i < 2*n; i++) cerr << sumdist[i] << '\n';
dijkstra(sumdist);
int q; cin >> q;
for (int i = 0; i < q; i++) {
int s; cin >> s; s += n-1;
// cerr << dist[0][s] << ' ' << dist[1][s] << '\n';
cout << ((sumdist[s] < inf) ? sumdist[s]+1 : -1) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
599 ms |
62008 KB |
Output is correct |
5 |
Correct |
360 ms |
42012 KB |
Output is correct |
6 |
Correct |
301 ms |
38756 KB |
Output is correct |
7 |
Correct |
264 ms |
56520 KB |
Output is correct |
8 |
Correct |
191 ms |
53864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9680 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9680 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
10 ms |
10196 KB |
Output is correct |
17 |
Correct |
8 ms |
10068 KB |
Output is correct |
18 |
Correct |
8 ms |
10324 KB |
Output is correct |
19 |
Correct |
9 ms |
10368 KB |
Output is correct |
20 |
Correct |
7 ms |
10068 KB |
Output is correct |
21 |
Correct |
7 ms |
10068 KB |
Output is correct |
22 |
Correct |
6 ms |
10324 KB |
Output is correct |
23 |
Correct |
8 ms |
10252 KB |
Output is correct |
24 |
Correct |
7 ms |
10196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9680 KB |
Output is correct |
11 |
Correct |
7 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
10 ms |
10196 KB |
Output is correct |
17 |
Correct |
8 ms |
10068 KB |
Output is correct |
18 |
Correct |
8 ms |
10324 KB |
Output is correct |
19 |
Correct |
9 ms |
10368 KB |
Output is correct |
20 |
Correct |
7 ms |
10068 KB |
Output is correct |
21 |
Correct |
7 ms |
10068 KB |
Output is correct |
22 |
Correct |
6 ms |
10324 KB |
Output is correct |
23 |
Correct |
8 ms |
10252 KB |
Output is correct |
24 |
Correct |
7 ms |
10196 KB |
Output is correct |
25 |
Correct |
4 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
9 ms |
10196 KB |
Output is correct |
28 |
Correct |
8 ms |
10152 KB |
Output is correct |
29 |
Correct |
7 ms |
10068 KB |
Output is correct |
30 |
Correct |
7 ms |
10068 KB |
Output is correct |
31 |
Correct |
8 ms |
10196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
599 ms |
62008 KB |
Output is correct |
5 |
Correct |
360 ms |
42012 KB |
Output is correct |
6 |
Correct |
301 ms |
38756 KB |
Output is correct |
7 |
Correct |
264 ms |
56520 KB |
Output is correct |
8 |
Correct |
191 ms |
53864 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9680 KB |
Output is correct |
19 |
Correct |
7 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
4 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
10 ms |
10196 KB |
Output is correct |
25 |
Correct |
8 ms |
10068 KB |
Output is correct |
26 |
Correct |
8 ms |
10324 KB |
Output is correct |
27 |
Correct |
9 ms |
10368 KB |
Output is correct |
28 |
Correct |
7 ms |
10068 KB |
Output is correct |
29 |
Correct |
7 ms |
10068 KB |
Output is correct |
30 |
Correct |
6 ms |
10324 KB |
Output is correct |
31 |
Correct |
8 ms |
10252 KB |
Output is correct |
32 |
Correct |
7 ms |
10196 KB |
Output is correct |
33 |
Correct |
4 ms |
9684 KB |
Output is correct |
34 |
Correct |
5 ms |
9684 KB |
Output is correct |
35 |
Correct |
9 ms |
10196 KB |
Output is correct |
36 |
Correct |
8 ms |
10152 KB |
Output is correct |
37 |
Correct |
7 ms |
10068 KB |
Output is correct |
38 |
Correct |
7 ms |
10068 KB |
Output is correct |
39 |
Correct |
8 ms |
10196 KB |
Output is correct |
40 |
Correct |
664 ms |
68228 KB |
Output is correct |
41 |
Correct |
412 ms |
44920 KB |
Output is correct |
42 |
Correct |
466 ms |
74384 KB |
Output is correct |
43 |
Correct |
471 ms |
74516 KB |
Output is correct |
44 |
Correct |
322 ms |
41564 KB |
Output is correct |
45 |
Correct |
325 ms |
45784 KB |
Output is correct |
46 |
Correct |
143 ms |
24948 KB |
Output is correct |
47 |
Correct |
369 ms |
56300 KB |
Output is correct |
48 |
Correct |
347 ms |
56984 KB |
Output is correct |