#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
const int INF = 1e9;
int l[MAX_N + 1], r[MAX_N + 1], maxR[MAX_N + 1], maxPrefR[MAX_N + 1], dist1[MAX_N + 1], distN[MAX_N + 1];
int n;
void calc_dist1() {
struct dr {
int i, dist, maxR;
bool operator < ( const dr &x ) const {
if ( dist == x.dist )
return maxR < x.maxR;
return dist > x.dist;
}
};
for ( int i = 0; i <= n; i++ )
dist1[i] = INF, maxR[i] = 0;
priority_queue<dr> pq;
dist1[1] = 0;
maxR[1] = 0;
pq.push( { 1, dist1[1], maxR[1] } );
while ( !pq.empty() ) {
auto crt = pq.top();
pq.pop();
int i = crt.i;
for ( int j = 1; j <= n; j++ ) {
if ( l[j] <= i && i <= r[j] ) {
if ( dist1[i] + 1 < dist1[j] && max( maxR[i], r[j] ) > maxR[j] ) {
dist1[j] = dist1[i] + 1;
maxR[j] = max( maxR[i], r[j] );
pq.push( { j, dist1[j], maxR[j] } );
}
}
}
}
dist1[1] = 1;
maxR[1] = r[1];
}
void calc_distN() {
for ( int i = n - 1; i >= 0; i-- )
distN[i] = INF;
for ( int i = 1; i <= n; i++ )
maxPrefR[i] = max( maxPrefR[i - 1], r[i] );
distN[n] = 0;
for ( int i = n - 1; i >= 1; i-- )
distN[i] = distN[maxPrefR[i]] + 1;
}
int main() {
ios_base::sync_with_stdio( false );
cin.tie( NULL );
cout.tie( NULL );
int q;
cin >> n;
for ( int i = 1; i <= n; i++ )
cin >> l[i] >> r[i];
calc_dist1();
calc_distN();
/* for ( int i = 1; i <= n; i++ )
cerr << dist1[i] << " ";
cerr << "\n";
for ( int i = 1; i <= n; i++ )
cerr << distN[i] << " ";
cerr << "\n";*/
cin >> q;
while ( q-- ) {
int x;
cin >> x;
int ans = dist1[x] + distN[maxR[x]];
cout << (ans > n ? -1 : ans) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4552 KB |
Output is correct |
4 |
Execution timed out |
2037 ms |
10952 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4560 KB |
Output is correct |
6 |
Correct |
1 ms |
4560 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4640 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4560 KB |
Output is correct |
6 |
Correct |
1 ms |
4560 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4640 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
11 ms |
4444 KB |
Output is correct |
17 |
Correct |
6 ms |
4444 KB |
Output is correct |
18 |
Correct |
6 ms |
4444 KB |
Output is correct |
19 |
Correct |
6 ms |
4444 KB |
Output is correct |
20 |
Correct |
5 ms |
4444 KB |
Output is correct |
21 |
Correct |
5 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
6 ms |
4588 KB |
Output is correct |
24 |
Correct |
4 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4560 KB |
Output is correct |
6 |
Correct |
1 ms |
4560 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4640 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
11 ms |
4444 KB |
Output is correct |
17 |
Correct |
6 ms |
4444 KB |
Output is correct |
18 |
Correct |
6 ms |
4444 KB |
Output is correct |
19 |
Correct |
6 ms |
4444 KB |
Output is correct |
20 |
Correct |
5 ms |
4444 KB |
Output is correct |
21 |
Correct |
5 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
6 ms |
4588 KB |
Output is correct |
24 |
Correct |
4 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Incorrect |
13 ms |
4596 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4552 KB |
Output is correct |
4 |
Execution timed out |
2037 ms |
10952 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |