#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define LOCAL false
const int MAX_N = 1<<19; // >500k
int n;
const int TREE_SIZE = MAX_N<<1;
pii tree[TREE_SIZE];
int t = 1;
void setrange(int l, int r, int x) {
if (l > r) return;
for (l += MAX_N, r += MAX_N+1; l<r; l>>=1, r>>=1) {
if (l&1) tree[l++] = {x, t};
if (r&1) tree[--r] = {x, t};
}
t++;
}
int getval(int x) {
int val = 0;
int maxt = 0;
x += MAX_N;
while (x) {
if (tree[x].second > maxt) {
val = tree[x].first;
maxt = tree[x].second;
}
x >>= 1;
}
return val;
}
vector<int> graph[MAX_N];
int anccnt[MAX_N];
bool vis[MAX_N];
void dfs(int v, bool root = true) {
vis[v] = true;
if (!root) anccnt[v]++;
for (int to: graph[v]) if (!vis[to]) dfs(to, false);
}
int fastpow(int n, int p) {
int ans = 1;
while (p) {
if (p&1) ans = ((ll)ans*n)%MOD;
n = ((ll)n*n)%MOD;
p >>= 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
if (LOCAL) {
ignore=freopen("io/in", "r", stdin);
ignore=freopen("io/out", "w", stdout);
}
cin >> n;
int l, r;
rep1(v, n) {
cin >> l >> r;
int lv = getval(l);
int rv = getval(r);
if (lv != 0) graph[v].push_back(lv);
if (rv != 0 && rv != lv) graph[v].push_back(rv);
setrange(l+1, r-1, v);
}
rep1(v, n) {
rep1(v2, n) vis[v2] = false;
dfs(v);
}
int ans = 0;
rep1(v, n) ans = (ans+fastpow(anccnt[v]+1, MOD-2))%MOD;
cout << ans << '\n';
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 | #include <bits/stdc++.h> using namespace std; #define rep(a, b) for (int a = 0; a < (b); a++) #define rep1(a, b) for (int a = 1; a <= (b); a++) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; #define LOCAL false const int MAX_N = 1<<19; // >500k int n; const int TREE_SIZE = MAX_N<<1; pii tree[TREE_SIZE]; int t = 1; void setrange(int l, int r, int x) { if (l > r) return; for (l += MAX_N, r += MAX_N+1; l<r; l>>=1, r>>=1) { if (l&1) tree[l++] = {x, t}; if (r&1) tree[--r] = {x, t}; } t++; } int getval(int x) { int val = 0; int maxt = 0; x += MAX_N; while (x) { if (tree[x].second > maxt) { val = tree[x].first; maxt = tree[x].second; } x >>= 1; } return val; } vector<int> graph[MAX_N]; int anccnt[MAX_N]; bool vis[MAX_N]; void dfs(int v, bool root = true) { vis[v] = true; if (!root) anccnt[v]++; for (int to: graph[v]) if (!vis[to]) dfs(to, false); } int fastpow(int n, int p) { int ans = 1; while (p) { if (p&1) ans = ((ll)ans*n)%MOD; n = ((ll)n*n)%MOD; p >>= 1; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (LOCAL) { ignore=freopen("io/in", "r", stdin); ignore=freopen("io/out", "w", stdout); } cin >> n; int l, r; rep1(v, n) { cin >> l >> r; int lv = getval(l); int rv = getval(r); if (lv != 0) graph[v].push_back(lv); if (rv != 0 && rv != lv) graph[v].push_back(rv); setrange(l+1, r-1, v); } rep1(v, n) { rep1(v2, n) vis[v2] = false; dfs(v); } int ans = 0; rep1(v, n) ans = (ans+fastpow(anccnt[v]+1, MOD-2))%MOD; cout << ans << '\n'; return 0; } |
English