#include<cstdio> #include<algorithm> #define S 500007 #define M 1048576 #define C 22 using namespace std; typedef long long ll; int A[S]; int B[S]; int tree[2*M+7]; void sett(int l, int r, int ll, int rr, int w, int c){ if(l > rr || r < ll) return; if(l >= ll && r <= rr){ tree[w] = c; return; } int sr = (l+r)/2; sett(l,sr,ll,rr,w*2,c); sett(sr+1,r,ll,rr,w*2+1,c); } int gett(int x){ x = x+M-1; int ans = 0; while(x != 0){ ans = max(ans, tree[x]); x/=2; } return ans; } int L[S][C+1]; int R[S][C+1]; //TODO: make it ll ll reachability[S]; int LCA(int l, int r){ int p = l; int q = r; if(A[r] < B[l]){ return min(l,r); } int x = C; int y = C; while(1){ if(x == -1 && y == -1){ if(A[r] < B[l]){ return min(l,r); }else{ return L[r][0]; } } if(x == -1){ if(L[r][y] > l || A[L[r][y]] > B[l]){ r = L[r][y]; } y--; }else if(y == -1){ if(R[l][x] > r || B[R[l][x]] < A[r]){ l = R[l][x]; } x--; }else if(A[L[r][y]] < B[R[l][x]]){ if(L[r][y] < R[l][x]){ y--; }else{ x--; } }else{ if(L[r][y] > R[l][x]){ r = L[r][y]; y--; }else{ l = R[l][x]; x--; } } } } ll mod = 1000000007; ll inv[S]; int main(void){ inv[1] = 1; for(int i = 2; i < S; i++){ inv[i] = (-(mod / (ll)i) * inv[mod % i]) % mod; inv[i] += mod; } int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d %d",&A[i+1], &B[i+1]); A[i+1]++; B[i+1]++; } n++; A[1] = 1; B[1] = 2*n; for(int j = 0; j <= C; j++){ L[1][j] = 1; R[1][j] = 1; } sett(1,M,A[1],B[1],1,1); for(int i = 2; i <= n; i++){ L[i][0] = gett(A[i]); R[i][0] = gett(B[i]); sett(1,M,A[i],B[i],1,i); for(int j = 1; j <= C; j++){ L[i][j] = L[L[i][j-1]][j-1]; R[i][j] = R[R[i][j-1]][j-1]; } } for(int i = n; i >= 2; i--){ reachability[i]++; int l = L[i][0]; int r = R[i][0]; int x = LCA(l, r); // printf("%d %d %d %d*\n",i,l,r,x); reachability[l] += reachability[i]; reachability[r] += reachability[i]; reachability[x] -= reachability[i]; } ll ans = 0; for(int i = 2; i <= n; i++){ ans += inv[reachability[i]]; // printf("%d ", reachability[i]); } ans %= mod; printf("%lld\n",ans); 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 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 | #include<cstdio> #include<algorithm> #define S 500007 #define M 1048576 #define C 22 using namespace std; typedef long long ll; int A[S]; int B[S]; int tree[2*M+7]; void sett(int l, int r, int ll, int rr, int w, int c){ if(l > rr || r < ll) return; if(l >= ll && r <= rr){ tree[w] = c; return; } int sr = (l+r)/2; sett(l,sr,ll,rr,w*2,c); sett(sr+1,r,ll,rr,w*2+1,c); } int gett(int x){ x = x+M-1; int ans = 0; while(x != 0){ ans = max(ans, tree[x]); x/=2; } return ans; } int L[S][C+1]; int R[S][C+1]; //TODO: make it ll ll reachability[S]; int LCA(int l, int r){ int p = l; int q = r; if(A[r] < B[l]){ return min(l,r); } int x = C; int y = C; while(1){ if(x == -1 && y == -1){ if(A[r] < B[l]){ return min(l,r); }else{ return L[r][0]; } } if(x == -1){ if(L[r][y] > l || A[L[r][y]] > B[l]){ r = L[r][y]; } y--; }else if(y == -1){ if(R[l][x] > r || B[R[l][x]] < A[r]){ l = R[l][x]; } x--; }else if(A[L[r][y]] < B[R[l][x]]){ if(L[r][y] < R[l][x]){ y--; }else{ x--; } }else{ if(L[r][y] > R[l][x]){ r = L[r][y]; y--; }else{ l = R[l][x]; x--; } } } } ll mod = 1000000007; ll inv[S]; int main(void){ inv[1] = 1; for(int i = 2; i < S; i++){ inv[i] = (-(mod / (ll)i) * inv[mod % i]) % mod; inv[i] += mod; } int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d %d",&A[i+1], &B[i+1]); A[i+1]++; B[i+1]++; } n++; A[1] = 1; B[1] = 2*n; for(int j = 0; j <= C; j++){ L[1][j] = 1; R[1][j] = 1; } sett(1,M,A[1],B[1],1,1); for(int i = 2; i <= n; i++){ L[i][0] = gett(A[i]); R[i][0] = gett(B[i]); sett(1,M,A[i],B[i],1,i); for(int j = 1; j <= C; j++){ L[i][j] = L[L[i][j-1]][j-1]; R[i][j] = R[R[i][j-1]][j-1]; } } for(int i = n; i >= 2; i--){ reachability[i]++; int l = L[i][0]; int r = R[i][0]; int x = LCA(l, r); // printf("%d %d %d %d*\n",i,l,r,x); reachability[l] += reachability[i]; reachability[r] += reachability[i]; reachability[x] -= reachability[i]; } ll ans = 0; for(int i = 2; i <= n; i++){ ans += inv[reachability[i]]; // printf("%d ", reachability[i]); } ans %= mod; printf("%lld\n",ans); return 0; } |