Submission #1188612

#TimeUsernameProblemLanguageResultExecution timeMemory
1188612MalixBoat (APIO16_boat)C++20
100 / 100
1116 ms8316 KiB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<ll,ll,ll> ti;
typedef vector<ll> li;
typedef vector<li> lii;
 
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define LSOne(s) ((s)&(-s))
 
ll INF=1000000000000000010;
int inf=1e9+10;
ll M=1e9+7;

int main() {   
// ios::sync_with_stdio(0);
// cin.tie(0);
    int n;cin>>n;
    vector<pair<ll,ll>> a(n);
    vector<ll> md(n);
    md[1]=1;
    REP(i,2,n)md[i]=M-(M/i)*md[M%i]%M;
    vector<ll> arr;
    REP(i,0,n){
        cin>>a[i].F>>a[i].S;
        a[i].S++;
        arr.PB(a[i].F);
        arr.PB(a[i].S);
    }
    sort(arr.begin(),arr.end());
    arr.erase(unique(arr.begin(),arr.end()),arr.end());
    int m=arr.size();
    vector<vector<ll>> dp(n,vector<ll>(m,0)),sm(n,vector<ll>(m,0));
    vector<pair<int,int>> b(n);
    REP(i,0,n){
        b[i].F=lower_bound(arr.begin(),arr.end(),a[i].F)-arr.begin();
        b[i].S=lower_bound(arr.begin(),arr.end(),a[i].S)-arr.begin();
    }
    REP(i,0,n){
        REP(j,b[i].F,b[i].S){
            dp[i][j]=arr[j+1]-arr[j];
            sm[i][j]=1;
            if(j!=0)REP(k,0,i){
                sm[i][j]+=dp[k][j-1];
                sm[i][j]%=M;
                dp[i][j]+=dp[k][j-1]*(arr[j+1]-arr[j]);
                dp[i][j]%=M;
            }
            ll N=arr[j+1]-arr[j]-1;
            ll K=1;
            ll tot=N;
            for(int k=i-1;k>=0;k--){
                if(sm[k][j]==0)continue;
                N++;K++;
                tot*=N;
                tot%=M;
                tot*=md[K];
                tot%=M;
                dp[i][j]+=tot*sm[k][j];
                dp[i][j]%=M;
            }
        }
        REP(j,1,m)dp[i][j]+=dp[i][j-1];
    }
    ll ans=0;
    REP(i,0,n){
        ans+=dp[i][m-1];
        ans%=M;
    }
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...