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;
#define int long long
#define MOD 1000000007
int pwr(int x, int n) {
if(n == 0) return 1;
if(n == 1) return x;
int h = pwr(x, n/2);
if(n%2 == 0) return h*h%MOD;
else return h*h%MOD*x%MOD;
}
int inv2(int x) {
return pwr(x, MOD-2);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
// regions [l, r)
vector<pair<int, int>> v;
vector<int> len;
int a[n+1], b[n+1], inv[n+1], ans = 0;
for(int i = 1; i <= n; i++) inv[i] = inv2(i);
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
v.push_back({a[i], i});
v.push_back({b[i]+1, i+n});
}
v.push_back({0, 0});
sort(v.begin(), v.end());
int cnt = 0;
for(int i = 0; i < v.size(); i++) {
if(v[i].second <= n) a[v[i].second] = cnt;
else b[v[i].second-n] = cnt-1;
if(i != v.size()-1 && v[i].first != v[i+1].first) {
len.push_back(v[i+1].first-v[i].first);
cnt++;
}
}
int dp[n+1][cnt], pref[n+1][cnt];
memset(dp, 0, sizeof(dp));
memset(pref, 0, sizeof(pref));
dp[0][0] = 1;
for(int i = 0; i < cnt; i++) pref[0][i] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j < cnt; j++) {
if(j >= a[i] && j <= b[i]) {
int c = len[j], cur = 1;
for(int k = i-1; k >= 0; k--) {
dp[i][j] += pref[k][j-1] * c % MOD;
dp[i][j] %= MOD;
if(j >= a[k] && j <= b[k]) {
cur++;
c = c*(len[j]+cur-1)%MOD * inv[cur]%MOD;
}
}
}
pref[i][j] = (pref[i][j-1]+dp[i][j]) % MOD;
ans = (ans+dp[i][j]) % MOD;
}
}
cout << ans;
}
Compilation message (stderr)
boat.cpp: In function 'int32_t main()':
boat.cpp:36:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
boat.cpp:39:8: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(i != v.size()-1 && v[i].first != v[i+1].first) {
| ~~^~~~~~~~~~~~~
# | 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... |