#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
#define isz(a) (int)(a).size()
const int NM = 2500;
int ntest = 1;
int N, Q, L[NM+5], R[NM+5], bestL[NM+5][NM+5], bestR[NM+5][NM+5];
vector <pii> adj[NM+5][NM+5];
queue <pii> q;
int dist[NM+5][NM+5];
void solve(){
cin >> N;
for (int i = 1; i <= N; i++)
cin >> L[i] >> R[i];
for (int i = 1; i <= N; i++){
bestL[i][i] = bestR[i][i] = i;
for (int j = i+1; j <= N; j++){
if (mp(L[j], -R[j]) < mp(L[bestL[i][j-1]], -R[bestL[i][j-1]])) bestL[i][j] = j;
else bestL[i][j] = bestL[i][j-1];
if (mp(R[j], -L[j]) > mp(R[bestR[i][j-1]], -L[bestR[i][j-1]])) bestR[i][j] = j;
else bestR[i][j] = bestR[i][j-1];
}
for (int j = i; j <= N; j++){
int x = min(i, L[bestL[i][j]]), y = max(j, R[bestL[i][j]]);
adj[x][y].push_back(mp(i, j));
x = min(i, L[bestR[i][j]]), y = max(j, R[bestR[i][j]]);
adj[x][y].push_back(mp(i, j));
dist[i][j] = -1;
}
}
dist[1][N] = 0;
q.push(mp(1, N));
while (!q.empty()){
int l = q.front().fi, r = q.front().se; q.pop();
for (pii p : adj[l][r]){
if (dist[p.fi][p.se] != -1) continue;
dist[p.fi][p.se] = dist[l][r]+1;
q.push(mp(p.fi, p.se));
}
}
cin >> Q;
while (Q--){
int X; cin >> X;
cout << max(dist[X][X], 1) << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//cin >> ntest;
while (ntest--){
solve();
}
#ifdef daoquanglinh2007
cerr << '\n';
for (int i = 1; i <= 100; i++) cerr << '=';
cerr << "\nExecution time: " << 1*(clock()) << " ms\n";
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
147812 KB |
Output is correct |
2 |
Incorrect |
58 ms |
147792 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
147796 KB |
Output is correct |
2 |
Correct |
60 ms |
147792 KB |
Output is correct |
3 |
Incorrect |
60 ms |
147796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
147796 KB |
Output is correct |
2 |
Correct |
60 ms |
147792 KB |
Output is correct |
3 |
Incorrect |
60 ms |
147796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
147796 KB |
Output is correct |
2 |
Correct |
60 ms |
147792 KB |
Output is correct |
3 |
Incorrect |
60 ms |
147796 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
147812 KB |
Output is correct |
2 |
Incorrect |
58 ms |
147792 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |