#include <bits/stdc++.h> using namespace std; typedef long long LL; const LL mod = 1000000007; vector<vector<int>> graf; void unique(vector<int> &v) { set<int> s(v.begin(), v.end()); v.resize(0); v.reserve(s.size()); for (auto &e : s) v.push_back(e); } vector<int> cnt, marked; void dfs(int v) { marked[v] = 1; cnt[v]++; for (auto &e : graf[v]) if (!marked[e]) dfs(e); } LL fpow(LL a, LL b) { if (b == 0) return 1; LL w = fpow(a, b/2); LL ww = w*w % mod; if (b%2 == 0) return ww; else return ww*a % mod; return -1; } LL inv(LL n) { return fpow(n, mod-2); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, int>> v(n); for (auto &e : v) cin >> e.first >> e.second; graf = vector<vector<int>>(n); set<pair<int, int>> s; for (int i = n-1; i >= 0; i--) { int l = v[i].first; int r = v[i].second; auto it = s.lower_bound(make_pair(l, -1)); vector<pair<int, int>> kosz; for (auto it2 = it; it2 != s.end(); it2++) { if (it2->first <= r) kosz.push_back(*it2); else break; } for (auto &e : kosz) { graf[e.second].push_back(i); s.erase(e); } s.insert(make_pair(l, i)); s.insert(make_pair(r, i)); } for (auto &e : graf) unique(e); cnt = marked = vector<int>(n); for (int i = 0; i < n; i++) { for (auto &e : marked) e = 0; dfs(i); } LL ans = 0; for (auto &e : cnt) { ans += inv(e); ans %= mod; } cout << ans << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL mod = 1000000007; vector<vector<int>> graf; void unique(vector<int> &v) { set<int> s(v.begin(), v.end()); v.resize(0); v.reserve(s.size()); for (auto &e : s) v.push_back(e); } vector<int> cnt, marked; void dfs(int v) { marked[v] = 1; cnt[v]++; for (auto &e : graf[v]) if (!marked[e]) dfs(e); } LL fpow(LL a, LL b) { if (b == 0) return 1; LL w = fpow(a, b/2); LL ww = w*w % mod; if (b%2 == 0) return ww; else return ww*a % mod; return -1; } LL inv(LL n) { return fpow(n, mod-2); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, int>> v(n); for (auto &e : v) cin >> e.first >> e.second; graf = vector<vector<int>>(n); set<pair<int, int>> s; for (int i = n-1; i >= 0; i--) { int l = v[i].first; int r = v[i].second; auto it = s.lower_bound(make_pair(l, -1)); vector<pair<int, int>> kosz; for (auto it2 = it; it2 != s.end(); it2++) { if (it2->first <= r) kosz.push_back(*it2); else break; } for (auto &e : kosz) { graf[e.second].push_back(i); s.erase(e); } s.insert(make_pair(l, i)); s.insert(make_pair(r, i)); } for (auto &e : graf) unique(e); cnt = marked = vector<int>(n); for (int i = 0; i < n; i++) { for (auto &e : marked) e = 0; dfs(i); } LL ans = 0; for (auto &e : cnt) { ans += inv(e); ans %= mod; } cout << ans << "\n"; } |