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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
template<int modd> struct modint {
static const int mod = modd;
int v; explicit operator int() const { return v;}
modint() : v(0) {}
modint(ll x) : v(int(x%mod)) {if (v < 0) v += mod;}
friend bool operator==(const modint& a, const modint& b) {return a.v == b.v;}
friend bool operator!=(const modint& a, const modint& b) {return a.v != b.v;}
friend bool operator<(const modint& a, const modint& b) {return a.v < b.v;}
modint& operator+=(const modint& a) {if ((v += a.v) >= mod) v -= mod; return *this;}
modint& operator-=(const modint& a) {if ((v -= a.v) < 0) v += mod; return *this;}
modint& operator*=(const modint& a) {v = int((ll)v*a.v%mod); return *this;}
modint& operator/=(const modint& a) {return (*this) *= inv(a);}
friend modint pow(modint a, ll p) {modint ret = 1; for (; p; p /= 2, a *= a){if (p&1) ret *= a;} return ret;}
friend modint inv(const modint& a) {return pow(a,mod-2);}
modint operator-() const {return modint(-v);}
modint& operator++() {return *this += 1;}
modint& operator--() {return *this -= 1;}
friend modint operator+(modint a, const modint& b) {return a += b;}
friend modint operator-(modint a, const modint& b) {return a -= b;}
friend modint operator*(modint a, const modint& b) {return a *= b;}
friend modint operator/(modint a, const modint& b) {return a /= b;}
friend ostream& operator<<(ostream& out, modint a) {return out << a.v;}
friend istream& operator>>(istream& in, modint& a) {ll b; in >> b; a.v = b; return in;}
};
using mint = modint<1000000007>;
const int mx = 505;
int a[mx], b[mx], n;
mint choose[5*mx][5*mx], dp[5*mx][5*mx];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
map<int,int> pts;
for (int q = 1; q <= n; q++) {
cin >> a[q] >> b[q];
pts[a[q]] = 1;
pts[b[q]] = 1;
}
auto it = pts.begin();
vector<pii> inter = {{0,0}};
while (it != pts.end()) {
inter.emplace_back(it->fi, it->fi);
auto it1 = it;
it1++;
if (it1 != pts.end()) {
if (it1->fi - 1 >= it->fi + 1) {
inter.emplace_back(it->fi + 1, it1->fi - 1);
}
}
it++;
}
int m = SZ(inter);
for (int q = 0; q < m; q++) {
int x = inter[q].se-inter[q].fi+1;
for (int w = 0; w <= n; w++) {
if (w+2 > x+w) continue;
mint num = 1, den = 1;
for (int e = 1; e <= w+2; e++) {
num *= (x+w+1-e);
den *= e;
}
choose[q][w] = num*inv(den);
}
}
for (int q = 0; q < 5*mx; q++) dp[0][q] = 1;
for (int q = 1; q <= n; q++) {
dp[q][0] = 1;
for (int w = 1; w < m; w++) {
if (inter[w].fi < a[q] || inter[w].se > b[q]) continue;
dp[q][w] += dp[q-1][w-1]*(inter[w].se-inter[w].fi+1);
int ctr = 0;
for (int e = q-1; e > 0; e--) {
if (inter[w].fi >= a[e] && inter[w].se <= b[e]) {
dp[q][w] += dp[e-1][w-1]*choose[w][ctr];
ctr++;
}
}
}
for (int w = 2; w < m; w++) dp[q][w] += dp[q][w-1];
for (int w = 1; w < m; w++) dp[q][w] += dp[q-1][w];
}
cout << dp[n][m-1]-1 << "\n";
return 0;
}
# | 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... |