#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int inf = 1e9;
struct ST{
vector<vector<pii>> t;
vector<int> idx;
int n;
void build(int v, int tl, int tr){
if (tl == tr){
idx[tl] = v;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
build(vv, tl, tm);
build(vv + 1, tm + 1, tr);
t[vv].push_back({v, 0});
t[vv + 1].push_back({v, 0});
}
ST(int ns){
n = ns;
t.resize(4 * n);
idx.resize(n + 1);
build(1, 1, n);
}
void add(int v, int tl, int tr, int& l, int& r, int& i){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
if (i != v){
t[v].push_back({i, 1});
}
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
add(vv, tl, tm, l, r, i);
add(vv + 1, tm + 1, tr, l, r, i);
}
void add(int p, int l, int r){
add(1, 1, n, l, r, idx[p]);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
ST T(n);
for (int i = 1; i <= n; i++){
int l, r; cin>>l>>r;
T.add(i, l, r);
}
vector<int> out(4 * n);
auto dijkstra = [&](int v){
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, v});
vector<int> dist(4 * n, inf);
dist[v] = 0;
while (!pq.empty()){
auto [d, k] = pq.top(); pq.pop();
for (auto [i, w]: T.t[k]){
if (dist[i] > dist[k] + w){
dist[i] = dist[k] + w;
pq.push({dist[i], i});
}
}
}
dist[v] = 1;
for (int i = 1; i < 4 * n; i++){
out[i] += dist[i];
}
};
dijkstra(T.idx[1]);
dijkstra(T.idx[n]);
for (int i = 1; i <= n; i++){
out[T.idx[i]]--;
}
for (int i = 1; i < 4 * n; i++){
out[i] = min(out[i], inf);
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 1; i < 4 * n; i++){
if (out[i] != inf){
pq.push({out[i], i});
}
}
while (!pq.empty()){
auto [d, v] = pq.top(); pq.pop();
for (auto [i, w]: T.t[v]){
if (out[i] > out[v] + w){
out[i] = out[v] + w;
pq.push({out[i], i});
}
}
}
int q; cin>>q;
while (q--){
int x; cin>>x;
int ans = out[T.idx[x]];
if (ans == inf){
cout<<-1<<"\n";
continue;
}
cout<<ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
641 ms |
71484 KB |
Output is correct |
5 |
Correct |
297 ms |
51764 KB |
Output is correct |
6 |
Correct |
142 ms |
43932 KB |
Output is correct |
7 |
Correct |
228 ms |
43488 KB |
Output is correct |
8 |
Correct |
107 ms |
43672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
19 |
Correct |
4 ms |
1368 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
928 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
19 |
Correct |
4 ms |
1368 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
928 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
1256 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
641 ms |
71484 KB |
Output is correct |
5 |
Correct |
297 ms |
51764 KB |
Output is correct |
6 |
Correct |
142 ms |
43932 KB |
Output is correct |
7 |
Correct |
228 ms |
43488 KB |
Output is correct |
8 |
Correct |
107 ms |
43672 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1372 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
928 KB |
Output is correct |
31 |
Correct |
3 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
4 ms |
1256 KB |
Output is correct |
36 |
Correct |
4 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
3 ms |
908 KB |
Output is correct |
40 |
Correct |
666 ms |
75760 KB |
Output is correct |
41 |
Correct |
339 ms |
56780 KB |
Output is correct |
42 |
Correct |
428 ms |
83132 KB |
Output is correct |
43 |
Correct |
408 ms |
83820 KB |
Output is correct |
44 |
Correct |
175 ms |
48544 KB |
Output is correct |
45 |
Correct |
225 ms |
52500 KB |
Output is correct |
46 |
Correct |
108 ms |
20812 KB |
Output is correct |
47 |
Correct |
362 ms |
59288 KB |
Output is correct |
48 |
Correct |
285 ms |
63548 KB |
Output is correct |