#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> #define us unsigned using namespace std; const int N = 6e5; const int N_ = (1<<20); int n; int tree_mem[2*N_]; int query(int x) { int out = -1; for (x += N_; x > 0; x >>= 1) out = max(out, tree_mem[x]); return out; } void modify(int a, int b, int m) { b++; for (a += N_, b += N_; a < b; a >>= 1, b >>= 1) { if (a&1) { tree_mem[a] = max(tree_mem[a], m); a++; } if (b&1) { b--; tree_mem[b] = max(tree_mem[b], m); } } } int left_edge[N]; int right_edge[N]; int left_child[N]; int right_child[N]; const int M = 1e9+7; int inv(int a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } int add_to_subtree[N]; const int LOG_N=20; int exp_jmp_l[N][LOG_N]; int exp_jmp_r[N][LOG_N]; int jmp_l(int x, int p){ if(x == -1) return -1; return exp_jmp_l[x][p]; } int jmp_r(int x, int p){ if(x == -1) return -1; return exp_jmp_r[x][p]; } int find_intersection(int l, int r){ if(l <= r) goto skip; for(int lk = LOG_N-1; lk>=0; lk--){ int l_ = jmp_r(l, lk); if(l_ > r) l = l_; } l = jmp_r(l, 0); skip: for(int rk = LOG_N-1; rk>=0; rk--){ int r_ = jmp_l(r, rk); if(r_ >= l) r = r_; } if(r == -1) return -1; if(l == -1) return -1; if(l == r) return l; if(l == jmp_l(r, 0)) return l; if(right_edge[l] > left_edge[r]) return l; for(int lk = LOG_N-1; lk>=0; lk--){ int l_ = jmp_r(l, lk); if(l_==-1) continue; if(r < l_){ l = l_; continue; } int r_ = r; for(int rk = LOG_N-1; rk>=0; rk--){ int r__ = jmp_l(r_, rk); if(r__ > l_) r_ = r__; } if(l_ == jmp_l(r_, 0)) continue; if(right_edge[l_] > left_edge[r_]) continue; l = l_; } int r_ = r; for(int rk = LOG_N-1; rk>=0; rk--){ int r__ = jmp_l(r_, rk); if(r__ > l) r_ = r__; } return jmp_r(l, 0); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); foru(i, 2*N_) tree_mem[i] = -1; cin >> n; foru(i, n) { int a, b; cin >> a >> b; a--; b--; left_edge[i] = a; right_edge[i] = b; left_child[i] = query(a); right_child[i] = query(b); modify(a+1, b-1, i); exp_jmp_l[i][0]=left_child[i]; exp_jmp_r[i][0]=right_child[i]; } fors(k, LOG_N, 1) foru(i, n){ exp_jmp_l[i][k]=jmp_l(jmp_l(i, k-1), k-1); exp_jmp_r[i][k]=jmp_r(jmp_r(i, k-1), k-1); } foru(i, n) add_to_subtree[i]=1; for(int i = n-1; i>=0; i--){ int l = left_child[i]; int r = right_child[i]; if(l != -1) add_to_subtree[l] += add_to_subtree[i]; if(r != -1) add_to_subtree[r] += add_to_subtree[i]; int inter = find_intersection(l, r); if(inter != -1) add_to_subtree[inter] -= add_to_subtree[i]; } int ans = 0; foru(i, n) { ans += inv(add_to_subtree[i]); if(ans >= M) ans -= M; } cout << ans << 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 163 164 165 166 167 168 169 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> #define us unsigned using namespace std; const int N = 6e5; const int N_ = (1<<20); int n; int tree_mem[2*N_]; int query(int x) { int out = -1; for (x += N_; x > 0; x >>= 1) out = max(out, tree_mem[x]); return out; } void modify(int a, int b, int m) { b++; for (a += N_, b += N_; a < b; a >>= 1, b >>= 1) { if (a&1) { tree_mem[a] = max(tree_mem[a], m); a++; } if (b&1) { b--; tree_mem[b] = max(tree_mem[b], m); } } } int left_edge[N]; int right_edge[N]; int left_child[N]; int right_child[N]; const int M = 1e9+7; int inv(int a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } int add_to_subtree[N]; const int LOG_N=20; int exp_jmp_l[N][LOG_N]; int exp_jmp_r[N][LOG_N]; int jmp_l(int x, int p){ if(x == -1) return -1; return exp_jmp_l[x][p]; } int jmp_r(int x, int p){ if(x == -1) return -1; return exp_jmp_r[x][p]; } int find_intersection(int l, int r){ if(l <= r) goto skip; for(int lk = LOG_N-1; lk>=0; lk--){ int l_ = jmp_r(l, lk); if(l_ > r) l = l_; } l = jmp_r(l, 0); skip: for(int rk = LOG_N-1; rk>=0; rk--){ int r_ = jmp_l(r, rk); if(r_ >= l) r = r_; } if(r == -1) return -1; if(l == -1) return -1; if(l == r) return l; if(l == jmp_l(r, 0)) return l; if(right_edge[l] > left_edge[r]) return l; for(int lk = LOG_N-1; lk>=0; lk--){ int l_ = jmp_r(l, lk); if(l_==-1) continue; if(r < l_){ l = l_; continue; } int r_ = r; for(int rk = LOG_N-1; rk>=0; rk--){ int r__ = jmp_l(r_, rk); if(r__ > l_) r_ = r__; } if(l_ == jmp_l(r_, 0)) continue; if(right_edge[l_] > left_edge[r_]) continue; l = l_; } int r_ = r; for(int rk = LOG_N-1; rk>=0; rk--){ int r__ = jmp_l(r_, rk); if(r__ > l) r_ = r__; } return jmp_r(l, 0); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); foru(i, 2*N_) tree_mem[i] = -1; cin >> n; foru(i, n) { int a, b; cin >> a >> b; a--; b--; left_edge[i] = a; right_edge[i] = b; left_child[i] = query(a); right_child[i] = query(b); modify(a+1, b-1, i); exp_jmp_l[i][0]=left_child[i]; exp_jmp_r[i][0]=right_child[i]; } fors(k, LOG_N, 1) foru(i, n){ exp_jmp_l[i][k]=jmp_l(jmp_l(i, k-1), k-1); exp_jmp_r[i][k]=jmp_r(jmp_r(i, k-1), k-1); } foru(i, n) add_to_subtree[i]=1; for(int i = n-1; i>=0; i--){ int l = left_child[i]; int r = right_child[i]; if(l != -1) add_to_subtree[l] += add_to_subtree[i]; if(r != -1) add_to_subtree[r] += add_to_subtree[i]; int inter = find_intersection(l, r); if(inter != -1) add_to_subtree[inter] -= add_to_subtree[i]; } int ans = 0; foru(i, n) { ans += inv(add_to_subtree[i]); if(ans >= M) ans -= M; } cout << ans << endl; } |