/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const LL mod = 1e9 + 7; struct Graph { int n; vector<vector<int> > kraw; vector<int> cnt; vector<int> depth; vector<int> res; vector<int> vis; int iter = 0; Graph(int _n, vector<vector<int> > &_kraw) { n = _n; swap(kraw, _kraw); cnt.resize(n); depth.resize(n); vis.resize(n); res.resize(n); } void get_depth() { for (int i = n-1; i >= 0; i--) { if (ssize(kraw[i]) != 0) { depth[i] = 2*n; for (int v : kraw[i]) { depth[i] = min(depth[i], depth[v] + 1); } } } } void solve_node(int x) { debug(x); iter++; priority_queue<int> order; order.push(-x); vis[x] = iter; bool ok = false; while (ssize(order) > 1 || !ok) { ok = true; int v = -order.top(); debug(v); order.pop(); res[x]++; for (int u : kraw[v]) { if (vis[u] != iter) { order.push(-u); vis[u] = iter; } } } if (ssize(order) == 1) { int v = -order.top(); res[x] += 1 + res[v]; debug("witam", v); } res[x]--; } void solve() { for (int i = n-1; i >= 0; i--) { solve_node(i); } REP(i, n) { res[i] = n - res[i]; } } }; struct Lines { int n; vector<pair<int, int> > tab; Lines(int _n, vector<pair<int, int> > &_tab) { n = _n; swap(tab, _tab); } Graph gen() { vector<vector<int> > kraw(n); set<pair<int, int> > opened; for (int i = n-1; i >= 0; i--) { auto it = opened.lower_bound({tab[i].st, 0}); while (it != opened.end() && (*it).st < tab[i].nd) { kraw[i].push_back((*it).nd); auto prv = it; it++; opened.erase(prv); } opened.insert({tab[i].st, i}); opened.insert({tab[i].nd, i}); } return Graph(n, kraw); } }; struct Binom { int s; vector<LL> fact, inv, inv_fact, pow2; Binom(int _s) { s = _s; fact.resize(s + 1, 1); inv.resize(s + 1, 1); inv_fact.resize(s + 1, 1); pow2.resize(s + 1, 1); pow2[1] = 2; FOR(i, 2, s) { pow2[i] = pow2[i - 1] * 2 % mod; fact[i] = fact[i - 1] * i % mod; inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod; inv_fact[i] = inv_fact[i - 1] * inv[i] % mod; } } LL get(int n, int k) { if (n < k || k < 0) { return 0; } return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<pair<int, int> > lista(n); REP(i, n) { cin >> lista[i].st >> lista[i].nd; } Lines lines(n, lista); Graph g = lines.gen(); g.solve(); debug(g.res); Binom binom(n); LL res = 0; REP(i, n) { res = (res + binom.inv[n - g.res[i] + 1]) % mod; } cout << res << endl; }
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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const LL mod = 1e9 + 7; struct Graph { int n; vector<vector<int> > kraw; vector<int> cnt; vector<int> depth; vector<int> res; vector<int> vis; int iter = 0; Graph(int _n, vector<vector<int> > &_kraw) { n = _n; swap(kraw, _kraw); cnt.resize(n); depth.resize(n); vis.resize(n); res.resize(n); } void get_depth() { for (int i = n-1; i >= 0; i--) { if (ssize(kraw[i]) != 0) { depth[i] = 2*n; for (int v : kraw[i]) { depth[i] = min(depth[i], depth[v] + 1); } } } } void solve_node(int x) { debug(x); iter++; priority_queue<int> order; order.push(-x); vis[x] = iter; bool ok = false; while (ssize(order) > 1 || !ok) { ok = true; int v = -order.top(); debug(v); order.pop(); res[x]++; for (int u : kraw[v]) { if (vis[u] != iter) { order.push(-u); vis[u] = iter; } } } if (ssize(order) == 1) { int v = -order.top(); res[x] += 1 + res[v]; debug("witam", v); } res[x]--; } void solve() { for (int i = n-1; i >= 0; i--) { solve_node(i); } REP(i, n) { res[i] = n - res[i]; } } }; struct Lines { int n; vector<pair<int, int> > tab; Lines(int _n, vector<pair<int, int> > &_tab) { n = _n; swap(tab, _tab); } Graph gen() { vector<vector<int> > kraw(n); set<pair<int, int> > opened; for (int i = n-1; i >= 0; i--) { auto it = opened.lower_bound({tab[i].st, 0}); while (it != opened.end() && (*it).st < tab[i].nd) { kraw[i].push_back((*it).nd); auto prv = it; it++; opened.erase(prv); } opened.insert({tab[i].st, i}); opened.insert({tab[i].nd, i}); } return Graph(n, kraw); } }; struct Binom { int s; vector<LL> fact, inv, inv_fact, pow2; Binom(int _s) { s = _s; fact.resize(s + 1, 1); inv.resize(s + 1, 1); inv_fact.resize(s + 1, 1); pow2.resize(s + 1, 1); pow2[1] = 2; FOR(i, 2, s) { pow2[i] = pow2[i - 1] * 2 % mod; fact[i] = fact[i - 1] * i % mod; inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod; inv_fact[i] = inv_fact[i - 1] * inv[i] % mod; } } LL get(int n, int k) { if (n < k || k < 0) { return 0; } return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<pair<int, int> > lista(n); REP(i, n) { cin >> lista[i].st >> lista[i].nd; } Lines lines(n, lista); Graph g = lines.gen(); g.solve(); debug(g.res); Binom binom(n); LL res = 0; REP(i, n) { res = (res + binom.inv[n - g.res[i] + 1]) % mod; } cout << res << endl; } |