#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define st first #define nd second #define vt vector #define VAR(__var) #__var << ": " << __var << " " #define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " " #define FOR(__var, __start, __end) for(int __var=__start; __var<__end; ++__var) #define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var) #define REP(__var, __cnt) for(int __var=0; __var<(__cnt); ++__var) #define EACH(__var, __x) for(auto __var : __x) #define maxi(__x, __y) __x = (__x>__y?__x:__y) #define mini(__x, __y) __x = (__x<__y?__x:__y) #define all(__var) (__var).begin(),(__var).end() #define rall(__var) (__var).rbegin(),(__var).rend() #define sz(__var) (int)(__var).size() using namespace std; using namespace __gnu_pbds; template <typename T> using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #ifdef DEBUG template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const bool __test_cases = false; const int INF = 1e9+2137; const ll mod = 1'000'000'007; vt<ll> sln; inline ll add(ll a, ll b) { return (a+b)%mod; } inline ll mul(ll a, ll b) { return (a*b)%mod; } ll bin(ll a, ll b) { if(b == 0) return 1ll; if(b&1) return mul(bin(a, b-1), a); ll x = bin(a, b/2); return mul(x, x); } inline ll inv(ll a) { return bin(a, mod-2); } inline ll nck(ll n, ll k) { return mul(sln[n], inv(mul(sln[k], sln[n-k]))); } struct Segment { vt<int> t; int base; Segment(int n) { base = 1; while(base < n) base *= 2; t.resize(2*base, -1); } void upd(int l, int r, int tl, int tr, int v, int x) { if(r < tl || tr < l) return; if(l <= tl && tr <= r) { t[v] = x; return; } int tm = tl + (tr-tl)/2; upd(l, r, tl, tm, 2*v, x); upd(l, r, tm+1, tr, 2*v+1, x); } void upd(int l, int r, int x) { upd(l, r, 0, base-1, 1, x); } int get(int v) { v += base; int res = -1; while(v > 0) { if(t[v] > res) res = t[v]; v/=2; } return res; } }; void solve() { int n; cin >> n; sln.resize(n+1); sln[0] = 1; FOR(i, 1, n+1) sln[i] = mul(sln[i-1], i); vt<vt<int>> g(n); vt<int> wch(n, 0), ile(n, 0); Segment seg(2*n); vt<pii> a; FOR(i, 0, n) { int l, r; cin >> l >> r; a.pb({l-1, r-1}); } FOR(i, 0, n) { auto [l, r] = a[i]; int res = seg.get(l); if(res != -1) { g[i].pb(res); ++wch[res]; } int prev = res; res = seg.get(r); if(res != -1 && prev != res) { g[i].pb(res); ++wch[res]; } seg.upd(l, r, i); } queue<int> q; FOR(i, 0, n) { if(wch[i] == 0) q.push(i); } const int MAXN = 100'000; vector<bitset<MAXN>> kto(n); while(!q.empty()) { auto v = q.front(); q.pop(); for(auto u : g[v]) { --wch[u]; if(wch[u] == 0) q.push(u); kto[u] |= kto[v]; kto[u][v] = 1; } } FOR(i, 0, n) ile[i] = kto[i].count(); ll res = 0; FOR(i, 0, n) { res = add(res, mul(nck(n, ile[i]+1), mul(sln[ile[i]], sln[n-ile[i]-1]))); } cout << mul(res, inv(sln[n])) << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; if(__test_cases) cin >> t; while(t--) { 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define st first #define nd second #define vt vector #define VAR(__var) #__var << ": " << __var << " " #define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " " #define FOR(__var, __start, __end) for(int __var=__start; __var<__end; ++__var) #define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var) #define REP(__var, __cnt) for(int __var=0; __var<(__cnt); ++__var) #define EACH(__var, __x) for(auto __var : __x) #define maxi(__x, __y) __x = (__x>__y?__x:__y) #define mini(__x, __y) __x = (__x<__y?__x:__y) #define all(__var) (__var).begin(),(__var).end() #define rall(__var) (__var).rbegin(),(__var).rend() #define sz(__var) (int)(__var).size() using namespace std; using namespace __gnu_pbds; template <typename T> using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #ifdef DEBUG template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const bool __test_cases = false; const int INF = 1e9+2137; const ll mod = 1'000'000'007; vt<ll> sln; inline ll add(ll a, ll b) { return (a+b)%mod; } inline ll mul(ll a, ll b) { return (a*b)%mod; } ll bin(ll a, ll b) { if(b == 0) return 1ll; if(b&1) return mul(bin(a, b-1), a); ll x = bin(a, b/2); return mul(x, x); } inline ll inv(ll a) { return bin(a, mod-2); } inline ll nck(ll n, ll k) { return mul(sln[n], inv(mul(sln[k], sln[n-k]))); } struct Segment { vt<int> t; int base; Segment(int n) { base = 1; while(base < n) base *= 2; t.resize(2*base, -1); } void upd(int l, int r, int tl, int tr, int v, int x) { if(r < tl || tr < l) return; if(l <= tl && tr <= r) { t[v] = x; return; } int tm = tl + (tr-tl)/2; upd(l, r, tl, tm, 2*v, x); upd(l, r, tm+1, tr, 2*v+1, x); } void upd(int l, int r, int x) { upd(l, r, 0, base-1, 1, x); } int get(int v) { v += base; int res = -1; while(v > 0) { if(t[v] > res) res = t[v]; v/=2; } return res; } }; void solve() { int n; cin >> n; sln.resize(n+1); sln[0] = 1; FOR(i, 1, n+1) sln[i] = mul(sln[i-1], i); vt<vt<int>> g(n); vt<int> wch(n, 0), ile(n, 0); Segment seg(2*n); vt<pii> a; FOR(i, 0, n) { int l, r; cin >> l >> r; a.pb({l-1, r-1}); } FOR(i, 0, n) { auto [l, r] = a[i]; int res = seg.get(l); if(res != -1) { g[i].pb(res); ++wch[res]; } int prev = res; res = seg.get(r); if(res != -1 && prev != res) { g[i].pb(res); ++wch[res]; } seg.upd(l, r, i); } queue<int> q; FOR(i, 0, n) { if(wch[i] == 0) q.push(i); } const int MAXN = 100'000; vector<bitset<MAXN>> kto(n); while(!q.empty()) { auto v = q.front(); q.pop(); for(auto u : g[v]) { --wch[u]; if(wch[u] == 0) q.push(u); kto[u] |= kto[v]; kto[u][v] = 1; } } FOR(i, 0, n) ile[i] = kto[i].count(); ll res = 0; FOR(i, 0, n) { res = add(res, mul(nck(n, ile[i]+1), mul(sln[ile[i]], sln[n-ile[i]-1]))); } cout << mul(res, inv(sln[n])) << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; if(__test_cases) cin >> t; while(t--) { solve(); } return 0; } |