#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n+1);
vector<pair<int,int>> ranges(n+1);
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
ranges[i]={l,r};
for(int j=l;j<=r;j++)adj[j].emplace_back(i);
}
vector<int> distL(n+1,INF);
vector<int> distR(n+1,INF);
{
// distL calculation
queue<pair<int,int>> q;
for(int i=1;i<=n;i++){
if(ranges[i].first==1)q.emplace(i,0);
}
vector<bool> visited(n+1);
while(!q.empty()){
auto [idx,dist] = q.front();q.pop();
if(visited[idx])continue;
visited[idx]=true;
distL[idx]=dist;
for(int&i:adj[idx])if(!visited[i])q.emplace(i,dist+1);
}
}
{
// distR calculation
queue<pair<int,int>> q;
for(int i=1;i<=n;i++){
if(ranges[i].second==n)q.emplace(i,0);
}
vector<bool> visited(n+1);
while(!q.empty()){
auto [idx,dist] = q.front();q.pop();
if(visited[idx])continue;
visited[idx]=true;
distR[idx]=dist;
for(int&i:adj[idx])if(!visited[i])q.emplace(i,dist+1);
}
}
vector<int> totdist(n+1,INF);
{
// totdist calculation
vector<bool> visited(n+1);
priority_queue<pair<int,int>> q;
for(int i=1;i<=n;i++){
q.emplace(-(distL[i]+distR[i]+1),i);
}
while(!q.empty()){
auto [dist,idx] = q.top();q.pop();dist=-dist;
if(visited[idx])continue;
visited[idx]=true;
totdist[idx]=dist;
for(int&i:adj[idx])if(!visited[i])q.emplace(-dist-1,i);
}
}
int q;
cin >> q;
for(int i=1;i<=q;i++){
int x;cin>>x;
if(totdist[x]>=INF)cout<<"-1\n";
else cout << totdist[x] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
1207 ms |
1048580 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
864 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
864 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
79 ms |
27904 KB |
Output is correct |
17 |
Correct |
4 ms |
1368 KB |
Output is correct |
18 |
Correct |
51 ms |
33852 KB |
Output is correct |
19 |
Correct |
58 ms |
33460 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
5212 KB |
Output is correct |
22 |
Correct |
311 ms |
71728 KB |
Output is correct |
23 |
Correct |
140 ms |
53452 KB |
Output is correct |
24 |
Correct |
153 ms |
46528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
864 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
79 ms |
27904 KB |
Output is correct |
17 |
Correct |
4 ms |
1368 KB |
Output is correct |
18 |
Correct |
51 ms |
33852 KB |
Output is correct |
19 |
Correct |
58 ms |
33460 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
5212 KB |
Output is correct |
22 |
Correct |
311 ms |
71728 KB |
Output is correct |
23 |
Correct |
140 ms |
53452 KB |
Output is correct |
24 |
Correct |
153 ms |
46528 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
94 ms |
32012 KB |
Output is correct |
28 |
Correct |
5 ms |
1104 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
8 ms |
4956 KB |
Output is correct |
31 |
Correct |
78 ms |
35544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
1207 ms |
1048580 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |