Submission #1236748

#TimeUsernameProblemLanguageResultExecution timeMemory
1236748lrnnzBoat (APIO16_boat)C++20
100 / 100
362 ms8460 KiB
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;

#define all(a) (a).begin(), (a).end()
#define ll long long
#define ld long double
#define ui uint64_t
#define cont(set, element) ((set).find(element) != (set).end())

#define chmin(x, y) (x = min(x, y)) 
#define chmax(x, y) (x = max(x, y))

/********* DEBUG *********/

template <typename T>
void outvec(const vector<T>& Z){
    for (const T& x : Z)
    cout << x << ' ';
    cout << "\n";
}
void printVariable(const any& var) {
    if (!var.has_value()) {
        cout << "null";
        return;
    }

    if (var.type() == typeid(int)) {
        cout << any_cast<int>(var);
    } else if (var.type() == typeid(double)) {
        cout << any_cast<double>(var);
    } else if (var.type() == typeid(float)) {
        cout << any_cast<float>(var);
    } else if (var.type() == typeid(char)) {
        cout << any_cast<char>(var);
    } else if (var.type() == typeid(bool)) {
        cout << (any_cast<bool>(var) ? "true" : "false");
    } else if (var.type() == typeid(string)) {
        cout << any_cast<string>(var);
    } else if (var.type() == typeid(const char*)) {
        cout << any_cast<const char*>(var);
    } else if (var.type() == typeid(long long)) {
        cout << any_cast<long long>(var);
    } else {
        cout << "[unknown type]";
    }
}

template<typename... Args>
void outval(Args... args) {
    vector<any> variables = {args...};
    
    for (size_t i = 0; i < variables.size(); ++i) {
        printVariable(variables[i]);
        if (i != variables.size() - 1) {
            cout << " ";
        }
    }
    cout << "\n";
}

#define sp << " " <<
#define fi first
#define se second

/********* DEBUG *********/

const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
const ll inf = 1e18;
const ll mxN = 100005;

ll binpow(ll a, ll b, ll m){
    a %= m;
    ll ans = 1;

    while (b){
        if (b & 1)
            ans = (ans * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }

    return ans;
}

ll modinv(ll a, ll m){
    return binpow(a, m-2, m);
}

void solve(){
    ll n;
    cin >> n;

    vector<ll> vec, l(n), r(n);
    for (int i = 0; i < n; i++){
        cin >> l[i] >> r[i];

        l[i]--;
        vec.push_back(l[i]);
        vec.push_back(r[i]);
    }

    sort(all(vec));
    vec.resize(unique(all(vec))-vec.begin());
    vector<vector<ll>> c(vec.size()+5, vector<ll>(n+5));
    for (int i = 1; i < vec.size(); i++){
        c[i][1] = vec[i] - vec[i-1];

        for (int j = 2; j <= n; j++){
            c[i][j] = ((c[i][j-1] * (vec[i] - vec[i-1] + j - 1) % MOD ) * modinv(j, MOD)) % MOD;
        }
    }

    // dp[i][j] = number of ways using first i schools, such that number of boats sent <= vec[j]
    vector<vector<ll>> dp(n+5, vector<ll>(vec.size()+5));
    for (int i = 0; i < vec.size(); i++){
        dp[0][i] = 1;
    }

    for (int i = 1; i <= n; i++){
        dp[i][0] = 1;

        for (int j = 1; j < vec.size(); j++){
            dp[i][j] = dp[i][j-1];
            ll take = 1;

            for (int k = i; k; k--){

                if (l[k-1] < vec[j] && vec[j] <= r[k-1]){
                    dp[i][j] = (dp[i][j] + dp[k-1][j-1] * c[j][take]) % MOD;
                    take++;
                }

            }

            //outval("i,j,dp[i][j]:",i,j,dp[i][j]);
            dp[i][j] %= MOD;
        }
    }

    outval(dp[n][vec.size()-1]-1);
}

int main() {    
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}    
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...