This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> dist(n + 1, vector<int>(n + 1, 1e9));
int l, r;
for(int i = 1; i <= n; i++){
cin >> l >> r;
for(int j = l; j <= r; j++) dist[i][j] = 1;
dist[i][i] = 0;
}
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
int q;
cin >> q;
while(q--){
int a;
cin >> a;
int ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, dist[a][i] + dist[i][1] + dist[1][n]);
}
if(ans == 1e9) cout << "-1\n";
else cout << ans << "\n";
}
}
// g++ -std=gnu++20 pC.cpp -o run -Wall -Wextra -fsanitize=undefined -fsanitize=address
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |