#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long int lli; const int MAXN = 500005; const lli MOD = 1000000007; lli pot(lli a, lli n) { if (n == 0) return 1; if (n%2 == 0) return pot((a*a)%MOD, n/2); else return (a*pot(a, n-1)) % MOD; } lli rev(lli a) { return pot(a, MOD-2); } /*set<pair<int, int>> occ; pair<int, int> get_int(int x) { if (occ.empty()) return {-1, -1}; auto it = occ.upper_bound({x, x}); if (it == occ.begin()) return {-1, -1}; it--; auto p = *it; if (p.st <= x && x <= p.nd) return p; else return {-1, -1}; } void add_int(int a, int b) { auto it = occ.upper_bound({a, a}); if (it != occ.begin()) it--; while (it != occ.end()) { auto p = *it; if (p.st > b) break; } occ.insert({a, b}); }*/ lli dp[MAXN*16]; lli dp_get(int x, int tar, int pp, int kk) { if (dp[x] != 0) return dp[x]; if (kk == pp+1) return dp[x]; int s = (pp+kk)/2; if (tar < s) return dp_get(2*x, tar, pp, s); else return dp_get(2*x+1, tar, s, kk); } void dp_set(int x, lli v, int p, int k, int pp, int kk) { if (p == pp && k == kk) { dp[x] = v; return; } if (dp[x] != 0) { dp[2*x] = dp[x]; dp[2*x+1] = dp[x]; dp[x] = 0; } int s = (pp+kk) / 2; if (k <= s) return dp_set(2*x, v, p, k, pp, s); else if (s <= p) return dp_set(2*x+1, v, p, k, s, kk); dp_set(2*x, v, p, s, pp, s); dp_set(2*x+1, v, s, k, s, kk); } vector<int> d[MAXN]; vector<lli> all_ups[MAXN]; int prp[MAXN][2]; int prp_cnt = 0; void dfs(int x) { prp[x][0] = prp_cnt++; for (int a : d[x]) dfs(a); prp[x][1] = prp_cnt++; } int vis_cnt[MAXN]; int count_ancestors(int x, int id) { if (vis_cnt[x] == id) return 0; vis_cnt[x] = id; int res = 1; for (int a : all_ups[x]) res += count_ancestors(a, id); return res; } int main() { int n; scanf("%d", &n); int pd = 2; while (pd <= 2*n+5) pd *= 2; // for (int i=0; i<=2*pd+10; i++) { // dp[i] = -1; // } for (int i=1; i<=n; i++) { int a, b; scanf("%d%d", &a, &b); lli l = dp_get(1, a, 0, pd); lli p = dp_get(1, b, 0, pd); dp_set(1, i, a, b+1, 0, pd); d[l].push_back(i); all_ups[l].push_back(i); all_ups[p].push_back(i); } // for (int i=0; i<=n; i++) { // printf("%d: ", i); // for (int x : d[i]) // printf(" %d", x); // printf("\n"); // } dfs(0); lli res = 0; for (int i=1; i<=n; i++) { int x = count_ancestors(i, i); // printf(" %d %d\n", i, x); res = (res + rev(x)) % MOD; } printf("%lld\n", res); 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long int lli; const int MAXN = 500005; const lli MOD = 1000000007; lli pot(lli a, lli n) { if (n == 0) return 1; if (n%2 == 0) return pot((a*a)%MOD, n/2); else return (a*pot(a, n-1)) % MOD; } lli rev(lli a) { return pot(a, MOD-2); } /*set<pair<int, int>> occ; pair<int, int> get_int(int x) { if (occ.empty()) return {-1, -1}; auto it = occ.upper_bound({x, x}); if (it == occ.begin()) return {-1, -1}; it--; auto p = *it; if (p.st <= x && x <= p.nd) return p; else return {-1, -1}; } void add_int(int a, int b) { auto it = occ.upper_bound({a, a}); if (it != occ.begin()) it--; while (it != occ.end()) { auto p = *it; if (p.st > b) break; } occ.insert({a, b}); }*/ lli dp[MAXN*16]; lli dp_get(int x, int tar, int pp, int kk) { if (dp[x] != 0) return dp[x]; if (kk == pp+1) return dp[x]; int s = (pp+kk)/2; if (tar < s) return dp_get(2*x, tar, pp, s); else return dp_get(2*x+1, tar, s, kk); } void dp_set(int x, lli v, int p, int k, int pp, int kk) { if (p == pp && k == kk) { dp[x] = v; return; } if (dp[x] != 0) { dp[2*x] = dp[x]; dp[2*x+1] = dp[x]; dp[x] = 0; } int s = (pp+kk) / 2; if (k <= s) return dp_set(2*x, v, p, k, pp, s); else if (s <= p) return dp_set(2*x+1, v, p, k, s, kk); dp_set(2*x, v, p, s, pp, s); dp_set(2*x+1, v, s, k, s, kk); } vector<int> d[MAXN]; vector<lli> all_ups[MAXN]; int prp[MAXN][2]; int prp_cnt = 0; void dfs(int x) { prp[x][0] = prp_cnt++; for (int a : d[x]) dfs(a); prp[x][1] = prp_cnt++; } int vis_cnt[MAXN]; int count_ancestors(int x, int id) { if (vis_cnt[x] == id) return 0; vis_cnt[x] = id; int res = 1; for (int a : all_ups[x]) res += count_ancestors(a, id); return res; } int main() { int n; scanf("%d", &n); int pd = 2; while (pd <= 2*n+5) pd *= 2; // for (int i=0; i<=2*pd+10; i++) { // dp[i] = -1; // } for (int i=1; i<=n; i++) { int a, b; scanf("%d%d", &a, &b); lli l = dp_get(1, a, 0, pd); lli p = dp_get(1, b, 0, pd); dp_set(1, i, a, b+1, 0, pd); d[l].push_back(i); all_ups[l].push_back(i); all_ups[p].push_back(i); } // for (int i=0; i<=n; i++) { // printf("%d: ", i); // for (int x : d[i]) // printf(" %d", x); // printf("\n"); // } dfs(0); lli res = 0; for (int i=1; i<=n; i++) { int x = count_ancestors(i, i); // printf(" %d %d\n", i, x); res = (res + rev(x)) % MOD; } printf("%lld\n", res); return 0; } |