#include <bits/stdc++.h>
using namespace std;
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
pair <int, int> arr[n + 1];
for (int i = 1; i <= n; i++) cin >> arr[i].first >> arr[i].second;
int nxt[n + 1] = {};
nxt[n] = n;
for (int i = n - 1; i >= 1; i--) {
nxt[i] = n;
set <pair <int, int>> cur;
cur.insert(arr[i]);
for (int j = i + 1; j <= n; j++) {
auto l = cur.lower_bound({arr[j].second + 1, 0});
if (l == cur.begin()) {
cur.insert(arr[j]);
continue;
}
l--;
auto x = *l;
if (arr[j].first <= x.first && x.first <= arr[j].second) {
nxt[i] = j - 1;
break;
}
if (arr[j].first <= x.second && x.second <= arr[j].second) {
nxt[i] = j - 1;
break;
}
cur.insert(arr[j]);
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int dp[n + 1] = {}; dp[r] = 1;
for (int i = r - 1; i >= l; i--) {
dp[i] = (nxt[i] == r ? 0 : 1e9);
for (int j = i; j <= min(r - 1, nxt[i]); j++) {
dp[i] = min(dp[i], dp[j + 1]);
}
dp[i]++;
}
cout << dp[l] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
97 ms |
3540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |