#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return (a > b ? (a = b) == b : false);
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return (a < b ? (a = b) == b : false);
}
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 998244353;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
int l[n], r[n];
vector<vector<int>> d(n, vector<int> (n, inf));
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
x--;
if (!x) {
int res = 0, last = -1;
bool flag = true;
priority_queue<int> q;
q.push(r[x]);
for (int i = 1; i < n; ++i) {
if (last < i) {
if (q.empty() || q.top() < i) {
flag = false;
break;
}
last = q.top();
q.pop();
res++;
}
q.push(r[i]);
}
cout << (flag ? res : -1) << '\n';
} else {
queue<pair<int, int>> q;
d[l[x]][r[x]] = 1;
q.push({l[x], r[x]});
while (!q.empty()) {
auto [L, R] = q.front();
q.pop();
for (int i = L; i <= R; ++i) {
int new_l = min(L, l[i]), new_r = max(R, r[i]);
if (chmin(d[new_l][new_r], d[L][R] + 1)) {
q.push({new_l, new_r});
}
}
}
cout << (d[0][n - 1] == inf ? -1 : d[0][n - 1]) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
437 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
756 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
756 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
1409 ms |
23752 KB |
Output is correct |
17 |
Correct |
1114 ms |
24732 KB |
Output is correct |
18 |
Correct |
32 ms |
25172 KB |
Output is correct |
19 |
Correct |
42 ms |
23712 KB |
Output is correct |
20 |
Execution timed out |
2096 ms |
24920 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
756 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
1409 ms |
23752 KB |
Output is correct |
17 |
Correct |
1114 ms |
24732 KB |
Output is correct |
18 |
Correct |
32 ms |
25172 KB |
Output is correct |
19 |
Correct |
42 ms |
23712 KB |
Output is correct |
20 |
Execution timed out |
2096 ms |
24920 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 |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
437 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |