#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <numeric> #include <random> /* #include "../../simple-console-debug/debug.h" */ using namespace std; random_device rd; int r(int a, int b) { return uniform_int_distribution<int>(a, b)(rd); } const int MOD = 1000000007; long long power(long long b, int e) { if (e==0) return 1; long long half = power(b, e/2); half = (half * half) % MOD; if (e%2) return (b * half) % MOD; else return half; } long long inv(long long x) { return power(x, MOD-2); } struct Kranik { int l, r; }; class IntervalPine { int epoch, start; vector<int> T; public: IntervalPine(int n) { start = 1; while (start < n) start *= 2; T.resize(2*start, -1); epoch = 0; } int get(int p) { p += start; int result = -1; while (p) { result = max(result, T[p]); p /= 2; } return result; } void set(int a, int b) { a += start; b += start; while (a <= b) { if (a%2 == 1) T[a++] = epoch; if (b%2 == 0) T[b--] = epoch; a /= 2; b /= 2; } epoch++; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Kranik> K(n); for (Kranik& k : K) cin >> k.l >> k.r; // build DAG vector<int> LPl(n), LPr(n); IntervalPine IP(2*n+1); for (int i=0; i<n; i++) { LPl[i] = IP.get(K[i].l); LPr[i] = IP.get(K[i].r); IP.set(K[i].l, K[i].r); } // for each vertex find number of ancestors queue<int> Q; vector<int> Ancestors(n, 0); vector<int> FloodReasons(n, 0); vector<bool> Processed(n, false); for (int init_source=n-1; init_source>=0; init_source--) { if (Processed[init_source]) continue; /* cerr << endl; */ // initial flood down Q.push(init_source); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int ch : {LPl[v], LPr[v]}) if (ch>=0) if (FloodReasons[ch]++ == 0) Q.push(ch); } // lowering the flood source FloodReasons[init_source]++; int counter = 0, source = init_source; while (source >= 0) { /* cerr << "SOURCE = " << source << endl; */ // disable water at the source if (!Processed[source]) { counter++; Processed[source] = true; } FloodReasons[source]--; Q.push(source); // choose next source if (LPl[source] < 0) source = LPr[source]; else if (LPr[source] < 0) source = LPl[source]; else if (!Processed[LPl[source]]) source = LPl[source]; else if (!Processed[LPr[source]]) source = LPr[source]; else if (LPr[source] < LPl[source]) source = LPl[source]; else source = LPr[source]; if (source >= 0) FloodReasons[source]++; // dry some vertices while (!Q.empty()) { int v = Q.front(); Q.pop(); Ancestors[v] += counter; /* cerr << "Adding " << counter << " ancestors to " << v << endl; */ for (int ch : {LPl[v], LPr[v]}) if (ch>=0) if (--FloodReasons[ch] == 0) Q.push(ch); } } } // compute the result long long wnk_int=0, wnk_num=0, wnk_den=1; auto add_to_wnk = [&](long long num, long long den) { __int128_t new_wnk_num = (__int128_t)wnk_num*den + (__int128_t)wnk_den*num; __int128_t new_wnk_den = (__int128_t)wnk_den*den; wnk_int += new_wnk_num / new_wnk_den; new_wnk_num %= new_wnk_den; wnk_num = (long long)(new_wnk_num % MOD); wnk_den = (long long)(new_wnk_den % MOD); long long d = gcd(wnk_num, wnk_den); wnk_num = (wnk_num / d); wnk_den = (wnk_den / d); }; for (int i=0; i<n; i++) add_to_wnk(1, Ancestors[i]); cout << ((wnk_num * inv(wnk_den) + wnk_int) % MOD) << '\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 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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <numeric> #include <random> /* #include "../../simple-console-debug/debug.h" */ using namespace std; random_device rd; int r(int a, int b) { return uniform_int_distribution<int>(a, b)(rd); } const int MOD = 1000000007; long long power(long long b, int e) { if (e==0) return 1; long long half = power(b, e/2); half = (half * half) % MOD; if (e%2) return (b * half) % MOD; else return half; } long long inv(long long x) { return power(x, MOD-2); } struct Kranik { int l, r; }; class IntervalPine { int epoch, start; vector<int> T; public: IntervalPine(int n) { start = 1; while (start < n) start *= 2; T.resize(2*start, -1); epoch = 0; } int get(int p) { p += start; int result = -1; while (p) { result = max(result, T[p]); p /= 2; } return result; } void set(int a, int b) { a += start; b += start; while (a <= b) { if (a%2 == 1) T[a++] = epoch; if (b%2 == 0) T[b--] = epoch; a /= 2; b /= 2; } epoch++; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Kranik> K(n); for (Kranik& k : K) cin >> k.l >> k.r; // build DAG vector<int> LPl(n), LPr(n); IntervalPine IP(2*n+1); for (int i=0; i<n; i++) { LPl[i] = IP.get(K[i].l); LPr[i] = IP.get(K[i].r); IP.set(K[i].l, K[i].r); } // for each vertex find number of ancestors queue<int> Q; vector<int> Ancestors(n, 0); vector<int> FloodReasons(n, 0); vector<bool> Processed(n, false); for (int init_source=n-1; init_source>=0; init_source--) { if (Processed[init_source]) continue; /* cerr << endl; */ // initial flood down Q.push(init_source); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int ch : {LPl[v], LPr[v]}) if (ch>=0) if (FloodReasons[ch]++ == 0) Q.push(ch); } // lowering the flood source FloodReasons[init_source]++; int counter = 0, source = init_source; while (source >= 0) { /* cerr << "SOURCE = " << source << endl; */ // disable water at the source if (!Processed[source]) { counter++; Processed[source] = true; } FloodReasons[source]--; Q.push(source); // choose next source if (LPl[source] < 0) source = LPr[source]; else if (LPr[source] < 0) source = LPl[source]; else if (!Processed[LPl[source]]) source = LPl[source]; else if (!Processed[LPr[source]]) source = LPr[source]; else if (LPr[source] < LPl[source]) source = LPl[source]; else source = LPr[source]; if (source >= 0) FloodReasons[source]++; // dry some vertices while (!Q.empty()) { int v = Q.front(); Q.pop(); Ancestors[v] += counter; /* cerr << "Adding " << counter << " ancestors to " << v << endl; */ for (int ch : {LPl[v], LPr[v]}) if (ch>=0) if (--FloodReasons[ch] == 0) Q.push(ch); } } } // compute the result long long wnk_int=0, wnk_num=0, wnk_den=1; auto add_to_wnk = [&](long long num, long long den) { __int128_t new_wnk_num = (__int128_t)wnk_num*den + (__int128_t)wnk_den*num; __int128_t new_wnk_den = (__int128_t)wnk_den*den; wnk_int += new_wnk_num / new_wnk_den; new_wnk_num %= new_wnk_den; wnk_num = (long long)(new_wnk_num % MOD); wnk_den = (long long)(new_wnk_den % MOD); long long d = gcd(wnk_num, wnk_den); wnk_num = (wnk_num / d); wnk_den = (wnk_den / d); }; for (int i=0; i<n; i++) add_to_wnk(1, Ancestors[i]); cout << ((wnk_num * inv(wnk_den) + wnk_int) % MOD) << '\n'; return 0; } |