This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
bool intersect (pair <int, int> a, pair <int, int> b) {
return !(a.second < b.first || b.second < a.first);
}
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;
for (int j = i + 1; j <= n; j++) {
if (intersect(arr[i], arr[j])) {
nxt[i] = j - 1;
break;
}
}
nxt[i] = min(nxt[i], nxt[i + 1]);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
vector <int> d;
for (int i = l; i <= r; i++) {
d.push_back(min(r, nxt[i]));
}
int dp[(int)(d.size())] = {};
for (int j = (int)d.size() - 1; j >= 0; j--) {
if (d[j] == r) {
dp[j] = 1;
continue;
}
dp[j] = 1e9;
for (int i = j; i <= nxt[j]; i++) {
dp[j] = min(dp[j], dp[i + 1]);
}
dp[j]++;
}
cout << dp[0] << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |