#include <bits/stdc++.h>
#define ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int N = 1e5 + 5;
ll add(ll a, ll b) {
return a + b - (a + b >= mod ? mod : 0);
}
ll mul(ll a, ll b) {
return a * b % mod;
}
ll exp(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
return ans;
}
ll fac[N], inv[N];
ll C(int n, int k) {
if(n < k) return 0;
return fac[n] * inv[k] % mod * inv[n-k];
}
ll dp[505][505][2];
vector<int> cmp;
int n, L[N], R[N], m;
signed main() {
fac[0] = inv[0] = 1;
for(int i=1; i<N; i++) {
fac[i] = fac[i-1] * i % mod;
inv[i] = exp(fac[i], mod - 2);
}
cin >> n;
set<int> st = { 0 };
for(int i=1; i<=n; i++) {
cin >> L[i] >> R[i];
st.insert(L[i]);
st.insert(++R[i]);
}
cmp = vector<int>( st.begin(), st.end() );
for(int i=1; i<=n; i++) {
L[i] = lower_bound(cmp.begin(), cmp.end(), L[i]) - cmp.begin();
R[i] = lower_bound(cmp.begin(), cmp.end(), R[i]) - cmp.begin() - 1;
}
m = cmp.size() - 1;
memset(dp, 0, sizeof(dp));
// for(int i=1; i<=n; i++)
// cout << L[i] << " " << R[i] << endl;
dp[0][0][0] = 1;
for(int i=1; i<=n; i++) {
for(int j=1; j<m; j++) {
for(int t=0; t<2; t++)
dp[i][j][t] = add(dp[i][j][t], dp[i-1][j][t]);
int l=L[i], r=R[i];
for(int k=i; k<=n; k++) {
if(R[k] < l || L[k] > r) break;
l = max(l, L[k]);
r = min(r, R[k]);
if(l <= j && j <= r && cmp[j+1]-cmp[j] >= k-i+1) {
if(i == 2 && k == 2 && j == 3) {
}
for(int x=0; x<j; x++) {
dp[k][j][1] = add(dp[k][j][1], mul(dp[i-1][x][1], C(cmp[j+1]-cmp[j], k-i+1)));
dp[k][j][1] = add(dp[k][j][1], mul(dp[i-1][x][0], C(cmp[j+1]-cmp[j], k-i+1)));
}
}
}
}
}
ll ans = 0;
for(int i=1; i<=n; i++) {
for(int j=1; j<m; j++) {
ans = add(ans, dp[i][j][1]);
// cout << i << " za " << cmp[j] << " " << cmp[j+1]-1 << ": " << dp[i][j][1] << endl;
}
}
cout << ans << '\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... |