#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, v] = pq.top(); pq.pop();
for (auto [i, w]: T.t[v]){
if (dist[i] > dist[v] + w){
dist[i] = dist[v] + w;
pq.push({dist[i], i});
}
}
}
for (int i = 1; i <= 4 * n; i++){
if (dist[i] == inf){
out[i] = -1;
continue;
}
if (out[i] == -1) continue;
out[i] = max(out[i], dist[i]);
}
};
dijkstra(T.idx[1]);
dijkstra(T.idx[n]);
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 1; i <= n; i++){
if (out[i] > 0){
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;
cout<<out[T.idx[x]]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
524 ms |
70000 KB |
Output is correct |
5 |
Correct |
215 ms |
48488 KB |
Output is correct |
6 |
Correct |
120 ms |
39452 KB |
Output is correct |
7 |
Correct |
151 ms |
42856 KB |
Output is correct |
8 |
Correct |
111 ms |
43708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
524 ms |
70000 KB |
Output is correct |
5 |
Correct |
215 ms |
48488 KB |
Output is correct |
6 |
Correct |
120 ms |
39452 KB |
Output is correct |
7 |
Correct |
151 ms |
42856 KB |
Output is correct |
8 |
Correct |
111 ms |
43708 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |