#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int part_base = (1 << 19); const int maxN = 500 * 1000 + 10; const int mod = 1e9 + 7; int inv(int k) { int w = mod - 2; int r = 1; while (w) { if (w & 1) r = 1ll * r * k % mod; k = 1ll * k * k % mod; w /= 2; } return r; } int inwers[maxN]; void prep_inwers() { inwers[0] = 1; fwd(i, 1, maxN) inwers[i] = 1ll * inwers[i - 1] * i % mod; int ifact = inv(inwers[maxN - 1]); for (int i = maxN - 1; i >= 1; i --) { inwers[i] = 1ll * ifact * inwers[i - 1] % mod; ifact = 1ll * ifact * i % mod; } } vi left_tree[maxN]; vector<pii> tree_queries[maxN]; pii left_tree_ival[maxN]; vi right_tree[maxN]; int pre_right[maxN]; pii ival_tree[maxN]; pii ival_sons[maxN]; int ival_lca[maxN]; ll ival_cum[maxN]; int id_of_ord[maxN]; int left_tree_root[maxN]; int pre_cnt = 0; void dfs_right(int v) { int new_pre = pre_cnt; pre_cnt += pre_right[v]; pre_right[v] = new_pre; ival_tree[v] = {new_pre, pre_cnt}; for (auto u : right_tree[v]) { dfs_right(u); ival_tree[v].nd = ival_tree[u].nd; } } const int inf = 1e9 + 2137; struct Node { int l, r; int best_ord; Node(int ord) { l = r = -1; best_ord = ord; } }; vector<Node> tree_nodes; int path_from_root[100]; void find_and_remove(int root, int ord) { int l = 0, r = part_base - 1; int path_len = 0; while (l != r) { path_from_root[path_len ++] = root; int mid = (l + r) / 2; if (ord <= mid) { root = tree_nodes[root].l; r = mid; } else { root = tree_nodes[root].r; l = mid + 1; } } tree_nodes[root].best_ord = inf; while (path_len > 0) { root = path_from_root[-- path_len]; tree_nodes[root].best_ord = inf; if (tree_nodes[root].l != -1 && tree_nodes[tree_nodes[root].l].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[tree_nodes[root].l].best_ord; if (tree_nodes[root].r != -1 && tree_nodes[tree_nodes[root].r].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[tree_nodes[root].r].best_ord; } } int find_lower_bound(int root, int ord) { int l = 0, r = part_base - 1; int res = inf; while (l != r) { int mid = (l + r) / 2; if (ord <= mid) { if (tree_nodes[root].r != -1 && tree_nodes[tree_nodes[root].r].best_ord < res) res = tree_nodes[tree_nodes[root].r].best_ord; if (tree_nodes[root].l != -1) { root = tree_nodes[root].l; r = mid; } else break; } else { if (tree_nodes[root].r != -1) { root = tree_nodes[root].r; l = mid + 1; } else break; } } if (l == r && tree_nodes[root].best_ord < res) res = tree_nodes[root].best_ord; return res; } void merge_trees(int root, int other) { if (tree_nodes[other].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[other].best_ord; if (tree_nodes[root].l == -1) tree_nodes[root].l = tree_nodes[other].l; else if (tree_nodes[other].l != -1) merge_trees(tree_nodes[root].l, tree_nodes[other].l); if (tree_nodes[root].r == -1) tree_nodes[root].r = tree_nodes[other].r; else if (tree_nodes[other].r != -1) merge_trees(tree_nodes[root].r, tree_nodes[other].r); } int create_tree(int ord) { int root_res = sz(tree_nodes); tree_nodes.push_back(Node(ord)); int root = root_res; int l = 0, r = part_base - 1; while (l != r) { int son = sz(tree_nodes); tree_nodes.push_back(Node(ord)); int mid = (l + r) / 2; if (ord <= mid) { tree_nodes[root].l = son; root = son; r = mid; } else { tree_nodes[root].r = son; root = son; l = mid + 1; } } return root_res; } void dfs_left(int v) { left_tree_root[v] = -1; for (auto u : left_tree[v]) { dfs_left(u); if (left_tree_root[u] == -1) continue; if (left_tree_root[v] == -1) { left_tree_root[v] = left_tree_root[u]; continue; } merge_trees(left_tree_root[v], left_tree_root[u]); } for (auto [who, id] : tree_queries[v]) { int ord = pre_right[who] ++; id_of_ord[ord] = id; int root = create_tree(ord); if (left_tree_root[v] == -1) { left_tree_root[v] = root; continue; } merge_trees(left_tree_root[v], root); } if (left_tree_root[v] == -1) return; auto [l, r] = ival_tree[v]; while (true) { int ord = find_lower_bound(left_tree_root[v], l); if (ord >= r) break; int id = id_of_ord[ord]; ival_lca[id] = v; find_and_remove(left_tree_root[v], ord); } } const int big_base = (1 << 20); int init_ival_tree[2 * big_base]; void put_ival(int l, int r, int k) { l += big_base; r += big_base; init_ival_tree[l] = init_ival_tree[r] = k; while (l / 2 != r / 2) { if (l % 2 == 0) init_ival_tree[l + 1] = k; if (r % 2 == 1) init_ival_tree[r - 1] = k; l /= 2; r /= 2; } } int get_ival(int p) { int res = 0; p += big_base; while (p) { if (init_ival_tree[p] > res) res = init_ival_tree[p]; p /= 2; } return res; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; fwd(i, 1, n + 1) { int l, r; cin >> l >> r; l -= 1; r -= 1; ival_sons[i].st = get_ival(l); ival_sons[i].nd = get_ival(r); put_ival(l, r, i); left_tree[ival_sons[i].st].push_back(i); right_tree[ival_sons[i].nd].push_back(i); if (ival_sons[i].st == ival_sons[i].nd) ival_lca[i] = ival_sons[i].st; else { tree_queries[ival_sons[i].nd].push_back({ival_sons[i].st, i}); pre_right[ival_sons[i].st] += 1; } } dfs_right(0); dfs_left(0); for (int i = n; i >= 1; i --) { ival_cum[i] += 1; ival_cum[ival_sons[i].st] += ival_cum[i]; ival_cum[ival_sons[i].nd] += ival_cum[i]; ival_cum[ival_lca[i]] -= ival_cum[i]; } prep_inwers(); int ans = 0; fwd(i, 1, n + 1) { ans += inwers[ival_cum[i]]; if (ans >= mod) ans -= mod; } cout << ans << '\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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int part_base = (1 << 19); const int maxN = 500 * 1000 + 10; const int mod = 1e9 + 7; int inv(int k) { int w = mod - 2; int r = 1; while (w) { if (w & 1) r = 1ll * r * k % mod; k = 1ll * k * k % mod; w /= 2; } return r; } int inwers[maxN]; void prep_inwers() { inwers[0] = 1; fwd(i, 1, maxN) inwers[i] = 1ll * inwers[i - 1] * i % mod; int ifact = inv(inwers[maxN - 1]); for (int i = maxN - 1; i >= 1; i --) { inwers[i] = 1ll * ifact * inwers[i - 1] % mod; ifact = 1ll * ifact * i % mod; } } vi left_tree[maxN]; vector<pii> tree_queries[maxN]; pii left_tree_ival[maxN]; vi right_tree[maxN]; int pre_right[maxN]; pii ival_tree[maxN]; pii ival_sons[maxN]; int ival_lca[maxN]; ll ival_cum[maxN]; int id_of_ord[maxN]; int left_tree_root[maxN]; int pre_cnt = 0; void dfs_right(int v) { int new_pre = pre_cnt; pre_cnt += pre_right[v]; pre_right[v] = new_pre; ival_tree[v] = {new_pre, pre_cnt}; for (auto u : right_tree[v]) { dfs_right(u); ival_tree[v].nd = ival_tree[u].nd; } } const int inf = 1e9 + 2137; struct Node { int l, r; int best_ord; Node(int ord) { l = r = -1; best_ord = ord; } }; vector<Node> tree_nodes; int path_from_root[100]; void find_and_remove(int root, int ord) { int l = 0, r = part_base - 1; int path_len = 0; while (l != r) { path_from_root[path_len ++] = root; int mid = (l + r) / 2; if (ord <= mid) { root = tree_nodes[root].l; r = mid; } else { root = tree_nodes[root].r; l = mid + 1; } } tree_nodes[root].best_ord = inf; while (path_len > 0) { root = path_from_root[-- path_len]; tree_nodes[root].best_ord = inf; if (tree_nodes[root].l != -1 && tree_nodes[tree_nodes[root].l].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[tree_nodes[root].l].best_ord; if (tree_nodes[root].r != -1 && tree_nodes[tree_nodes[root].r].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[tree_nodes[root].r].best_ord; } } int find_lower_bound(int root, int ord) { int l = 0, r = part_base - 1; int res = inf; while (l != r) { int mid = (l + r) / 2; if (ord <= mid) { if (tree_nodes[root].r != -1 && tree_nodes[tree_nodes[root].r].best_ord < res) res = tree_nodes[tree_nodes[root].r].best_ord; if (tree_nodes[root].l != -1) { root = tree_nodes[root].l; r = mid; } else break; } else { if (tree_nodes[root].r != -1) { root = tree_nodes[root].r; l = mid + 1; } else break; } } if (l == r && tree_nodes[root].best_ord < res) res = tree_nodes[root].best_ord; return res; } void merge_trees(int root, int other) { if (tree_nodes[other].best_ord < tree_nodes[root].best_ord) tree_nodes[root].best_ord = tree_nodes[other].best_ord; if (tree_nodes[root].l == -1) tree_nodes[root].l = tree_nodes[other].l; else if (tree_nodes[other].l != -1) merge_trees(tree_nodes[root].l, tree_nodes[other].l); if (tree_nodes[root].r == -1) tree_nodes[root].r = tree_nodes[other].r; else if (tree_nodes[other].r != -1) merge_trees(tree_nodes[root].r, tree_nodes[other].r); } int create_tree(int ord) { int root_res = sz(tree_nodes); tree_nodes.push_back(Node(ord)); int root = root_res; int l = 0, r = part_base - 1; while (l != r) { int son = sz(tree_nodes); tree_nodes.push_back(Node(ord)); int mid = (l + r) / 2; if (ord <= mid) { tree_nodes[root].l = son; root = son; r = mid; } else { tree_nodes[root].r = son; root = son; l = mid + 1; } } return root_res; } void dfs_left(int v) { left_tree_root[v] = -1; for (auto u : left_tree[v]) { dfs_left(u); if (left_tree_root[u] == -1) continue; if (left_tree_root[v] == -1) { left_tree_root[v] = left_tree_root[u]; continue; } merge_trees(left_tree_root[v], left_tree_root[u]); } for (auto [who, id] : tree_queries[v]) { int ord = pre_right[who] ++; id_of_ord[ord] = id; int root = create_tree(ord); if (left_tree_root[v] == -1) { left_tree_root[v] = root; continue; } merge_trees(left_tree_root[v], root); } if (left_tree_root[v] == -1) return; auto [l, r] = ival_tree[v]; while (true) { int ord = find_lower_bound(left_tree_root[v], l); if (ord >= r) break; int id = id_of_ord[ord]; ival_lca[id] = v; find_and_remove(left_tree_root[v], ord); } } const int big_base = (1 << 20); int init_ival_tree[2 * big_base]; void put_ival(int l, int r, int k) { l += big_base; r += big_base; init_ival_tree[l] = init_ival_tree[r] = k; while (l / 2 != r / 2) { if (l % 2 == 0) init_ival_tree[l + 1] = k; if (r % 2 == 1) init_ival_tree[r - 1] = k; l /= 2; r /= 2; } } int get_ival(int p) { int res = 0; p += big_base; while (p) { if (init_ival_tree[p] > res) res = init_ival_tree[p]; p /= 2; } return res; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; fwd(i, 1, n + 1) { int l, r; cin >> l >> r; l -= 1; r -= 1; ival_sons[i].st = get_ival(l); ival_sons[i].nd = get_ival(r); put_ival(l, r, i); left_tree[ival_sons[i].st].push_back(i); right_tree[ival_sons[i].nd].push_back(i); if (ival_sons[i].st == ival_sons[i].nd) ival_lca[i] = ival_sons[i].st; else { tree_queries[ival_sons[i].nd].push_back({ival_sons[i].st, i}); pre_right[ival_sons[i].st] += 1; } } dfs_right(0); dfs_left(0); for (int i = n; i >= 1; i --) { ival_cum[i] += 1; ival_cum[ival_sons[i].st] += ival_cum[i]; ival_cum[ival_sons[i].nd] += ival_cum[i]; ival_cum[ival_lca[i]] -= ival_cum[i]; } prep_inwers(); int ans = 0; fwd(i, 1, n + 1) { ans += inwers[ival_cum[i]]; if (ans >= mod) ans -= mod; } cout << ans << '\n'; return 0; } |