#include <iostream> #include <vector> #include <fstream> #define ll long long using namespace std; const int M = 500005, mod = 1000000007; int n, pol[2 * M], reach[M]; pair <int, int> graph[M]; bool vis[M]; ll pot(ll a, ll b) { if (b == 1) return a; ll res = pot(a, b / 2); res = res * res % mod; if (b % 2) { res *= a; res %= mod; } return res; } ll odw(ll a) { return pot(a, mod - 2); } void dfs(int v) { if (vis[v]) return; vis[v] = 1; reach[v]++; dfs(graph[v].first); dfs(graph[v].second); } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); //fstream cin("in.in"); cin >> n; for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; graph[i] = { pol[l], pol[r] }; for (int j = l; j <= r; j++) pol[j] = i; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= n; j++) vis[j] = 0; dfs(i); } ll res = 0; for (int i = 1; i <= n; i++) res = (res + odw(reach[i])) % mod; cout << res; }
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 | #include <iostream> #include <vector> #include <fstream> #define ll long long using namespace std; const int M = 500005, mod = 1000000007; int n, pol[2 * M], reach[M]; pair <int, int> graph[M]; bool vis[M]; ll pot(ll a, ll b) { if (b == 1) return a; ll res = pot(a, b / 2); res = res * res % mod; if (b % 2) { res *= a; res %= mod; } return res; } ll odw(ll a) { return pot(a, mod - 2); } void dfs(int v) { if (vis[v]) return; vis[v] = 1; reach[v]++; dfs(graph[v].first); dfs(graph[v].second); } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); //fstream cin("in.in"); cin >> n; for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; graph[i] = { pol[l], pol[r] }; for (int j = l; j <= r; j++) pol[j] = i; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= n; j++) vis[j] = 0; dfs(i); } ll res = 0; for (int i = 1; i <= n; i++) res = (res + odw(reach[i])) % mod; cout << res; } |