#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) ((int)(v).size())
#define all(v) begin((v)), end(v)
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define dbg(x) "[" #x " = " << x << "]"
using namespace std;
const int MAXN = 2505, infINT = 1e9 + 23737, MAXB = 31;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}
struct E{
int nl, nr, nw;
E(int _nl = 0, int _nr = 0, int _nw = 0):
nl(_nl), nr(_nr), nw(_nw) {};
};
int numVal, numQuery, dist[MAXN][MAXN], query[MAXN];
int L[MAXN], R[MAXN], bestL[MAXN][MAXN], bestR[MAXN][MAXN];
vector<E> adj[MAXN][MAXN];
void input(){
cin >> numVal;
for(int i = 1; i <= numVal; i++) cin >> L[i] >> R[i];
cin >> numQuery;
for(int q = 1; q <= numQuery; q++) cin >> query[q];
}
void addEdge(int l, int r, int nl, int nr, int nw){
adj[nl][nr].push_back(E(l, r, nw));
}
void bfs(){
memset(dist, 0x3f, sizeof dist);
deque<ii> q;
q.emplace_back(1, numVal); dist[1][numVal] = 0;
while(q.size()){
int l = q.front().fi, r = q.front().se;
q.pop_front();
for(const E &e: adj[l][r]){
if (minimize(dist[e.nl][e.nr], dist[l][r] + e.nw)){
if (e.nw == 0) q.emplace_front(e.nl, e.nr);
else q.emplace_back(e.nl, e.nr);
}
}
}
}
void solve(){
for(int l = 1; l <= numVal; l++){
bestL[l][l] = L[l];
bestR[l][l] = R[l];
for(int r = l + 1; r <= numVal; r++){
bestL[l][r] = min(bestL[l][r - 1], L[r]);
bestR[l][r] = max(bestR[l][r - 1], R[r]);
}
}
for(int i = 1; i <= numVal; i++){
addEdge(i, i, L[i], R[i], 1);
}
for(int l = 1; l <= numVal; l++)
for(int r = l + 1; r <= numVal; r++){
addEdge(l, r, bestL[l][r], r, 1);
addEdge(l, r, l, bestR[l][r], 1);
addEdge(l, r, l + 1, r, 0);
addEdge(l, r, l, r - 1, 0);
}
bfs();
for(int q = 1; q <= numQuery; q++){
int d = dist[query[q]][query[q]];
cout << (d <= infINT ? d: -1) << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "THONGHANH"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t--){
input();
solve();
}
cerr << TIME << ".s\n";
}
Compilation message (stderr)
passport.cpp: In function 'int main()':
passport.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |