#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <int> L(n), R(n);
for ( int i = 0; i < n; i++ ){
cin >> L[i] >> R[i];
--L[i], --R[i];
}
int qq; cin >> qq;
while ( qq-- ){
int s; cin >> s;
--s;
if ( !s ){
int ans = 0, lst = -1;
bool flag = true;
priority_queue <int> q;
q.push(R[s]);
for ( int i = 1; i < n; i++ ){
if ( lst < i ){
if ( q.empty() || q.top() < i ){
flag = false;
break;
}
lst = q.top();
q.pop(); ++ans;
} q.push(R[i]);
}
cout << (flag ? ans : -1) << ln;
continue;
}
const int inf = 1e15;
vector <vector<int>> dp(n, vector <int> (n, inf));
dp[L[s]][R[s]] = 1;
queue <ar<int,2>> q;
q.push({L[s], R[s]});
while ( !q.empty() ){
auto [u, v] = q.front();
q.pop();
for ( int j = u; j <= v; j++ ){
int x = min(u, L[j]), y = max(v, R[j]);
if ( chmin(dp[x][y], dp[u][v] + 1) ){
q.push({x, y});
}
}
}
int ans = dp[0][n - 1];
if ( ans == inf ){
ans = -1;
}
cout << ans << ln;
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
41 ms |
5704 KB |
Output is correct |
5 |
Correct |
30 ms |
5588 KB |
Output is correct |
6 |
Correct |
24 ms |
3420 KB |
Output is correct |
7 |
Correct |
24 ms |
5680 KB |
Output is correct |
8 |
Correct |
18 ms |
4060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
1116 KB |
Output is correct |
16 |
Correct |
1723 ms |
47344 KB |
Output is correct |
17 |
Correct |
844 ms |
48976 KB |
Output is correct |
18 |
Correct |
37 ms |
49496 KB |
Output is correct |
19 |
Correct |
43 ms |
46680 KB |
Output is correct |
20 |
Execution timed out |
2083 ms |
49500 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
1116 KB |
Output is correct |
16 |
Correct |
1723 ms |
47344 KB |
Output is correct |
17 |
Correct |
844 ms |
48976 KB |
Output is correct |
18 |
Correct |
37 ms |
49496 KB |
Output is correct |
19 |
Correct |
43 ms |
46680 KB |
Output is correct |
20 |
Execution timed out |
2083 ms |
49500 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
41 ms |
5704 KB |
Output is correct |
5 |
Correct |
30 ms |
5588 KB |
Output is correct |
6 |
Correct |
24 ms |
3420 KB |
Output is correct |
7 |
Correct |
24 ms |
5680 KB |
Output is correct |
8 |
Correct |
18 ms |
4060 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
1723 ms |
47344 KB |
Output is correct |
25 |
Correct |
844 ms |
48976 KB |
Output is correct |
26 |
Correct |
37 ms |
49496 KB |
Output is correct |
27 |
Correct |
43 ms |
46680 KB |
Output is correct |
28 |
Execution timed out |
2083 ms |
49500 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |