#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, inf = 1e9+7;
int ntest = 1;
int N, Q, L[NM+5], R[NM+5], bestL[NM+5][NM+5], bestR[NM+5][NM+5];
vector <pair <pii, int> > adj[NM+5][NM+5];
deque <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(mp(i, j), 1));
x = min(i, L[bestR[i][j]]), y = max(j, R[bestR[i][j]]);
adj[x][y].push_back(mp(mp(i, j), 1));
dist[i][j] = +inf;
}
}
for (int i = 1; i <= N; i++)
for (int j = i; j <= N; j++){
if (i+1 <= j) adj[i+1][j].push_back(mp(mp(i, j), 0));
if (j-1 >= i) adj[i][j-1].push_back(mp(mp(i, j), 0));
}
dist[1][N] = 0;
q.push_back(mp(1, N));
while (!q.empty()){
int l = q.front().fi, r = q.front().se; q.pop_front();
for (pair <pii, int> p : adj[l][r]){
if (dist[l][r]+p.se >= dist[p.fi.fi][p.fi.se]) continue;
dist[p.fi.fi][p.fi.se] = dist[l][r]+p.se;
if (p.se == 0) q.push_front(p.fi);
else q.push_back(p.fi);
}
}
cin >> Q;
while (Q--){
int X; cin >> X;
cout << (dist[X][X] == +inf ? -1 : dist[X][X]) << '\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 |
63 ms |
149588 KB |
Output is correct |
2 |
Correct |
65 ms |
149584 KB |
Output is correct |
3 |
Correct |
64 ms |
149584 KB |
Output is correct |
4 |
Runtime error |
610 ms |
764736 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
149856 KB |
Output is correct |
2 |
Correct |
61 ms |
149572 KB |
Output is correct |
3 |
Correct |
65 ms |
149636 KB |
Output is correct |
4 |
Correct |
66 ms |
149672 KB |
Output is correct |
5 |
Correct |
68 ms |
149432 KB |
Output is correct |
6 |
Correct |
62 ms |
149600 KB |
Output is correct |
7 |
Correct |
64 ms |
149600 KB |
Output is correct |
8 |
Correct |
62 ms |
149708 KB |
Output is correct |
9 |
Correct |
63 ms |
149840 KB |
Output is correct |
10 |
Correct |
60 ms |
149584 KB |
Output is correct |
11 |
Correct |
82 ms |
156168 KB |
Output is correct |
12 |
Correct |
70 ms |
156868 KB |
Output is correct |
13 |
Correct |
73 ms |
156500 KB |
Output is correct |
14 |
Correct |
71 ms |
156188 KB |
Output is correct |
15 |
Correct |
70 ms |
157008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
149856 KB |
Output is correct |
2 |
Correct |
61 ms |
149572 KB |
Output is correct |
3 |
Correct |
65 ms |
149636 KB |
Output is correct |
4 |
Correct |
66 ms |
149672 KB |
Output is correct |
5 |
Correct |
68 ms |
149432 KB |
Output is correct |
6 |
Correct |
62 ms |
149600 KB |
Output is correct |
7 |
Correct |
64 ms |
149600 KB |
Output is correct |
8 |
Correct |
62 ms |
149708 KB |
Output is correct |
9 |
Correct |
63 ms |
149840 KB |
Output is correct |
10 |
Correct |
60 ms |
149584 KB |
Output is correct |
11 |
Correct |
82 ms |
156168 KB |
Output is correct |
12 |
Correct |
70 ms |
156868 KB |
Output is correct |
13 |
Correct |
73 ms |
156500 KB |
Output is correct |
14 |
Correct |
71 ms |
156188 KB |
Output is correct |
15 |
Correct |
70 ms |
157008 KB |
Output is correct |
16 |
Correct |
419 ms |
378064 KB |
Output is correct |
17 |
Correct |
831 ms |
428208 KB |
Output is correct |
18 |
Correct |
524 ms |
397552 KB |
Output is correct |
19 |
Correct |
450 ms |
394492 KB |
Output is correct |
20 |
Correct |
694 ms |
405252 KB |
Output is correct |
21 |
Correct |
621 ms |
434512 KB |
Output is correct |
22 |
Correct |
474 ms |
406572 KB |
Output is correct |
23 |
Correct |
473 ms |
414008 KB |
Output is correct |
24 |
Correct |
494 ms |
413552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
149856 KB |
Output is correct |
2 |
Correct |
61 ms |
149572 KB |
Output is correct |
3 |
Correct |
65 ms |
149636 KB |
Output is correct |
4 |
Correct |
66 ms |
149672 KB |
Output is correct |
5 |
Correct |
68 ms |
149432 KB |
Output is correct |
6 |
Correct |
62 ms |
149600 KB |
Output is correct |
7 |
Correct |
64 ms |
149600 KB |
Output is correct |
8 |
Correct |
62 ms |
149708 KB |
Output is correct |
9 |
Correct |
63 ms |
149840 KB |
Output is correct |
10 |
Correct |
60 ms |
149584 KB |
Output is correct |
11 |
Correct |
82 ms |
156168 KB |
Output is correct |
12 |
Correct |
70 ms |
156868 KB |
Output is correct |
13 |
Correct |
73 ms |
156500 KB |
Output is correct |
14 |
Correct |
71 ms |
156188 KB |
Output is correct |
15 |
Correct |
70 ms |
157008 KB |
Output is correct |
16 |
Correct |
419 ms |
378064 KB |
Output is correct |
17 |
Correct |
831 ms |
428208 KB |
Output is correct |
18 |
Correct |
524 ms |
397552 KB |
Output is correct |
19 |
Correct |
450 ms |
394492 KB |
Output is correct |
20 |
Correct |
694 ms |
405252 KB |
Output is correct |
21 |
Correct |
621 ms |
434512 KB |
Output is correct |
22 |
Correct |
474 ms |
406572 KB |
Output is correct |
23 |
Correct |
473 ms |
414008 KB |
Output is correct |
24 |
Correct |
494 ms |
413552 KB |
Output is correct |
25 |
Correct |
67 ms |
149588 KB |
Output is correct |
26 |
Correct |
67 ms |
149584 KB |
Output is correct |
27 |
Correct |
472 ms |
393284 KB |
Output is correct |
28 |
Correct |
848 ms |
431812 KB |
Output is correct |
29 |
Correct |
669 ms |
405328 KB |
Output is correct |
30 |
Correct |
602 ms |
435168 KB |
Output is correct |
31 |
Correct |
508 ms |
406244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
149588 KB |
Output is correct |
2 |
Correct |
65 ms |
149584 KB |
Output is correct |
3 |
Correct |
64 ms |
149584 KB |
Output is correct |
4 |
Runtime error |
610 ms |
764736 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |