#include <bits/stdc++.h> using namespace std; #define SIZE 1024 #define pii pair<int, int> #define ll long long #define MOD 1000000007ll int tree[2100000]; int read(int i) { int p = 0; while (i > 0) { if (tree[i] != 0) { p = tree[i]; } i /= 2; } return p; } void insert(int p, int a, int b, int l, int r, int val) { if (a > r || b < l) { return; } if (a >= l && b <= r) { tree[p] = val; return; } if (tree[p] != 0) { tree[2 * p] = tree[p]; tree[2 * p + 1] = tree[p]; tree[p] = 0; } insert(2 * p, a, (a + b) / 2, l, r, val); insert(2 * p + 1, (a + b) / 2 + 1, b, l, r, val); } int l[500007]; int r[500007]; void print_tree(int m) { int p = 1, i = 1; while (i < 2 * m) { for (int j = 0; j < p; j++) { cout << tree[i + j] << " "; } cout << "\n"; i += p; p *= 2; } } ll power(ll base, ll ind) { ll out = 1ll; while (ind > 0ll) { if (ind % 2ll == 1ll) { out *= base; out %= MOD; } ind /= 2ll; base *= base; base %= MOD; } return out; } ll inv[500007]; int anc[500007]; void print_anc(int n) { for (int i = 1; i <= n; i++) { cout << i << ": " << anc[i] << "\n"; } } void push_down(int v, int k, vector<bitset<SIZE>>& pred) { if ((v - 1) / SIZE == k) { pred[v] |= (bitset<SIZE>(1) << ((v - 1) % SIZE)); } if (l[v] != 0) { pred[l[v]] |= pred[v]; } if (r[v] != 0 && r[v] != l[v]) { pred[r[v]] |= pred[v]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { inv[i] = power((ll)(i), MOD - 2ll); } int m = 1; while (m < 2 * n) { m *= 2; } for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; l[i] = read(a + m - 1); r[i] = read(b + m - 1); if (a + 1 <= b - 1) insert(1, m, 2 * m - 1, a + m - 1 + 1, b + m - 1 - 1, i); } for (int k = 0; k < (n + SIZE - 1) / SIZE; k++) { vector<bitset<SIZE>> pred(n + 1, bitset<SIZE>(0ll)); for (int i = n; i >= 1; i--) { push_down(i, k, pred); anc[i] += (int)(pred[i].count()); } } ll out = 0; for (int i = 1; i <= n; i++) { out += inv[anc[i]]; out %= MOD; } cout << out << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; #define SIZE 1024 #define pii pair<int, int> #define ll long long #define MOD 1000000007ll int tree[2100000]; int read(int i) { int p = 0; while (i > 0) { if (tree[i] != 0) { p = tree[i]; } i /= 2; } return p; } void insert(int p, int a, int b, int l, int r, int val) { if (a > r || b < l) { return; } if (a >= l && b <= r) { tree[p] = val; return; } if (tree[p] != 0) { tree[2 * p] = tree[p]; tree[2 * p + 1] = tree[p]; tree[p] = 0; } insert(2 * p, a, (a + b) / 2, l, r, val); insert(2 * p + 1, (a + b) / 2 + 1, b, l, r, val); } int l[500007]; int r[500007]; void print_tree(int m) { int p = 1, i = 1; while (i < 2 * m) { for (int j = 0; j < p; j++) { cout << tree[i + j] << " "; } cout << "\n"; i += p; p *= 2; } } ll power(ll base, ll ind) { ll out = 1ll; while (ind > 0ll) { if (ind % 2ll == 1ll) { out *= base; out %= MOD; } ind /= 2ll; base *= base; base %= MOD; } return out; } ll inv[500007]; int anc[500007]; void print_anc(int n) { for (int i = 1; i <= n; i++) { cout << i << ": " << anc[i] << "\n"; } } void push_down(int v, int k, vector<bitset<SIZE>>& pred) { if ((v - 1) / SIZE == k) { pred[v] |= (bitset<SIZE>(1) << ((v - 1) % SIZE)); } if (l[v] != 0) { pred[l[v]] |= pred[v]; } if (r[v] != 0 && r[v] != l[v]) { pred[r[v]] |= pred[v]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { inv[i] = power((ll)(i), MOD - 2ll); } int m = 1; while (m < 2 * n) { m *= 2; } for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; l[i] = read(a + m - 1); r[i] = read(b + m - 1); if (a + 1 <= b - 1) insert(1, m, 2 * m - 1, a + m - 1 + 1, b + m - 1 - 1, i); } for (int k = 0; k < (n + SIZE - 1) / SIZE; k++) { vector<bitset<SIZE>> pred(n + 1, bitset<SIZE>(0ll)); for (int i = n; i >= 1; i--) { push_down(i, k, pred); anc[i] += (int)(pred[i].count()); } } ll out = 0; for (int i = 1; i <= n; i++) { out += inv[anc[i]]; out %= MOD; } cout << out << "\n"; } |