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 ll long long
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
const int maxn = 2e3 + 5;
const ll mod = 1e9 + 7;
int n;
int a[maxn], b[maxn];
vector<int> pos;
int sz;
pii itv[maxn];
ll dp[maxn], temp[maxn];
ll add(ll x, ll y) {
return ((x+y)%mod + mod)%mod;
}
ll mul(ll x, ll y) {
return ((x*y)%mod + mod)%mod;
}
ll inv(ll a, ll b){
return 1<a ? b - inv(b%a,a)*b/a : 1;
}
void show(int key) {
printf("show %d\n",key);
for(int i=1;i<=sz;i++) printf("---- [%d, %d] : %lld\n",itv[i].X,itv[i].Y,dp[i]);
}
void init() {
pos.pb(0); pos.pb(2e9);
for(int i=1;i<=n;i++) pos.pb(a[i]), pos.pb(b[i]);
sort(pos.begin(),pos.end());
unique(pos.begin(),pos.end());
for(int i=0;i<pos.size()-1;i++) {
itv[++sz] = {pos[i],pos[i]};
if(pos[i]+1<=pos[i+1]-1) itv[++sz] = {pos[i]+1,pos[i+1]-1};
}
// show(0);
}
ll solve() {
dp[1] = 1;
for(int cur=1;cur<=n;cur++) {
for(int x=1;x<=sz;x++) {
temp[x] = 0;
if(a[cur]<=itv[x].X && itv[x].Y<=b[cur]) {
int len = itv[x].Y-itv[x].X+1;
for(int y=1;y<x;y++) temp[x] = add(temp[x], mul(len, dp[y]));
temp[x] = add(temp[x], (mul(mul(len-1, inv(2,mod)), dp[x])));
}
}
for(int x=1;x<=sz;x++) dp[x] = add(dp[x], temp[x]);
// show(cur);
}
ll ans = 0;
for(int x=2;x<=sz;x++) ans = add(ans, dp[x]);
return ans;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
init();
printf("%lld",solve());
}
Compilation message (stderr)
boat.cpp: In function 'void init()':
boat.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<pos.size()-1;i++) {
^
boat.cpp: In function 'int main()':
boat.cpp:67:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
boat.cpp:68:52: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
^
# | 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... |