This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _GLIBCXX_DEBUG_PEDANTIC
#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
struct Seg {
int l;
int r;
Seg(int _l = -1, int _r = -1) : l(_l), r(_r) {}
bool empty() const {
return r - l + 1 <= 0;
}
bool operator<(const Seg& other) const {
return tie(l, r) < tie(other.l, other.r);
}
bool operator==(const Seg& other) const {
return tie(l, r) == tie(other.l, other.r);
}
friend Seg intersect(const Seg& lhs, const Seg& rhs) {
return {max(lhs.l, rhs.l), min(lhs.r, rhs.r)};
}
};
const int MOD = 1e9 + 7;
const int INV2 = 500000004;
int add(int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int mul(long long a, int b) {
return a * b % MOD;
}
const int MAX_N = 500 + 1;
Seg segs[MAX_N];
int dp[MAX_N][MAX_N * MAX_N];
int main_idx[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> segs[i].l >> segs[i].r;
}
vector<Seg> states;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (segs[i].l <= segs[j].r) {
states.emplace_back(segs[i].l, segs[j].r);
}
if (segs[i].l <= segs[j].l - 1) {
states.emplace_back(segs[i].l, segs[j].l - 1);
}
}
}
sort(states.begin(), states.end());
states.resize(unique(states.begin(), states.end()) - states.begin());
int m = states.size();
segs[0] = {-1, -1};
dp[0][0] = 1;
int ans = 0;
for (int i = 1; i <= n; ++i) {
main_idx[i] = lower_bound(states.begin(), states.end(), segs[i]) - states.begin();
for (int j = 0; j < m; ++j) {
auto [l1, r1] = states[j];
if (segs[i].l > l1 || r1 > segs[i].r) continue;
for (int k = 0; k < i; ++k) {
auto [l2, r2] = segs[k];
if (r1 < l2) {
continue;
}
if (r2 < l1) {
dp[i][j] = add(dp[i][j], mul(dp[k][main_idx[k]], r1 - l1 + 1));
continue;
}
auto [l3, r3] = intersect(states[j], segs[k]);
dp[i][j] = add(dp[i][j], mul(dp[k][main_idx[k]], r1 - r3));
int idx1 = lower_bound(states.begin(), states.end(), Seg{l3, r3}) - states.begin();
dp[i][j] = add(dp[i][j], mul(dp[k][idx1], mul(r3 - l3, INV2)));
if (l3 - 1 >= l2) {
int idx2 = lower_bound(states.begin(), states.end(), Seg{l2, l3 - 1}) - states.begin();
dp[i][j] = add(dp[i][j], mul(r3 - l1 + 1, dp[k][idx2]));
}
}
}
ans = add(ans, dp[i][main_idx[i]]);
}
cout << ans << '\n';
return 0;
}
# | 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... |