#include <vector> #include <cstdio> #include <stack> #include <algorithm> #include <queue> #include <map> #include <cmath> #include <unordered_set> #include <unordered_map> #include <set> #include <cstring> #include <bitset> #include <iostream> #include <queue> #include <iomanip> #include <complex> using namespace std; #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define RED(i, n) FORD(i, n, 1) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define REPD(i, n) FORD(i, n-1,0) const int maxn = 500012, mod = 1000000007, bis = 4096; int d[maxn * 8], il[maxn], base, f[maxn], inf[maxn]; vector<int>v[maxn]; bitset<bis>bi[maxn]; void ins(int x, int y, int ak) { x += base; y += base; d[x] = ak; d[y] = ak; while (x/2 != y/2){ if(!(x&1))d[x + 1] = ak; if(y&1)d[y - 1] = ak; x>>=1; y>>=1; } } int que(int x) { int odp = 0; x += base; while (x) { odp = max(odp, d[x]); x>>=1; } return odp; } int mul(int a, int b) { return int(a * 1ll * b % mod); } int binPow(int a, int k) { int ans = 1; while (k > 0) { if (k & 1) ans = mul(ans, a); a = mul(a, a); k >>= 1; } return ans; } void precalc(int n) { f[0] = inf[0] = 1; RE(i, n)f[i] = mul(f[i - 1], i); inf[n] = binPow(f[n], mod - 2); RED(i, n - 1)inf[i] = mul(inf[i + 1], i + 1); } int C(int n, int k) { if (k < 0 || n < k) return 0; return mul(f[n], mul(inf[n - k], inf[k])); } int norm(int a) { while (a >= mod) a -= mod; while (a < 0) a += mod; return a; } void solve() { int l, r, n; cin>>n; base = (1<<((int)log2(2 * n) + 1)) - 1; RE(i, n) { cin>>l>>r; auto le = que(l), re = que(r); if(le)v[i].push_back(le); if(re && le != re)v[i].push_back(re); ins(l, r, i); } // RE(i, n){ // cout<<i<<" "; // for(auto ne: v[i])cout<<ne<<" "; // cout<<"\n"; // } precalc(n + 1); for(int i = n; i >= 1; i -= bis){ RE(j, i)bi[j].reset(); REP(j, min(bis, i))bi[i - j][j] = true; RED(j, i) for(auto ne: v[j])bi[ne] |= bi[j]; RE(j, i)il[j] += bi[j].count(); } RE(i, n)il[i] --; int res = 0; RE(i, n) { auto pom = mul(mul(f[il[i]], f[n - il[i] - 1]), C(n, n - il[i] - 1)); res = norm(res + pom); } cout<<mul(res, inf[n])<<"\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int tt = 1; // cin >> tt; while (tt--) { solve(); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #include <vector> #include <cstdio> #include <stack> #include <algorithm> #include <queue> #include <map> #include <cmath> #include <unordered_set> #include <unordered_map> #include <set> #include <cstring> #include <bitset> #include <iostream> #include <queue> #include <iomanip> #include <complex> using namespace std; #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define RED(i, n) FORD(i, n, 1) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define REPD(i, n) FORD(i, n-1,0) const int maxn = 500012, mod = 1000000007, bis = 4096; int d[maxn * 8], il[maxn], base, f[maxn], inf[maxn]; vector<int>v[maxn]; bitset<bis>bi[maxn]; void ins(int x, int y, int ak) { x += base; y += base; d[x] = ak; d[y] = ak; while (x/2 != y/2){ if(!(x&1))d[x + 1] = ak; if(y&1)d[y - 1] = ak; x>>=1; y>>=1; } } int que(int x) { int odp = 0; x += base; while (x) { odp = max(odp, d[x]); x>>=1; } return odp; } int mul(int a, int b) { return int(a * 1ll * b % mod); } int binPow(int a, int k) { int ans = 1; while (k > 0) { if (k & 1) ans = mul(ans, a); a = mul(a, a); k >>= 1; } return ans; } void precalc(int n) { f[0] = inf[0] = 1; RE(i, n)f[i] = mul(f[i - 1], i); inf[n] = binPow(f[n], mod - 2); RED(i, n - 1)inf[i] = mul(inf[i + 1], i + 1); } int C(int n, int k) { if (k < 0 || n < k) return 0; return mul(f[n], mul(inf[n - k], inf[k])); } int norm(int a) { while (a >= mod) a -= mod; while (a < 0) a += mod; return a; } void solve() { int l, r, n; cin>>n; base = (1<<((int)log2(2 * n) + 1)) - 1; RE(i, n) { cin>>l>>r; auto le = que(l), re = que(r); if(le)v[i].push_back(le); if(re && le != re)v[i].push_back(re); ins(l, r, i); } // RE(i, n){ // cout<<i<<" "; // for(auto ne: v[i])cout<<ne<<" "; // cout<<"\n"; // } precalc(n + 1); for(int i = n; i >= 1; i -= bis){ RE(j, i)bi[j].reset(); REP(j, min(bis, i))bi[i - j][j] = true; RED(j, i) for(auto ne: v[j])bi[ne] |= bi[j]; RE(j, i)il[j] += bi[j].count(); } RE(i, n)il[i] --; int res = 0; RE(i, n) { auto pom = mul(mul(f[il[i]], f[n - il[i] - 1]), C(n, n - il[i] - 1)); res = norm(res + pom); } cout<<mul(res, inf[n])<<"\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int tt = 1; // cin >> tt; while (tt--) { solve(); } return 0; } |