#include <cstdio> #include <vector> #include <cstring> #include <functional> #include <algorithm> using i64 = long long; template <class T, class Compare = std::less<T> > class BitDirectRMQ { public: static constexpr int block_size = 32; void build(const std::vector<T> &a) { build(a.data(), a.size()); } void build(const T *a, int n) { blocks = (n + block_size - 1) / block_size; buildSparseTable(a, n); buildInnerBlocks(a, n); } // return the index of the minimum value in [l, r] in O(1) int query(const T *a, int l, int r) const { int x = l / block_size, y = r / block_size; if (x == y) return query_lookup(l, r); int ret = l; if (x + 1 <= y - 1) ret = query_st(a, x + 1, y - 1); ret = leftMinIndex(a, query_lookup(l, x * block_size + block_size - 1), ret); ret = leftMinIndex(a, ret, query_lookup(y * block_size, r)); return ret; } int query_st(const T *a, int x, int y) const { int z = log[y - x + 1]; return leftMinIndex(a, table[x + z * blocks], table[y - (1 << z) + 1 + z * blocks]); } int query_lookup(int x, int y) const { return x + __builtin_ctz(lookup[y] >> (x % block_size)); } private: int minIndex(const T *a, int x, int y) const { return compare(a[x], a[y]) || (a[x] == a[y] && x < y) ? x : y; } int leftMinIndex(const T *a, int x, int y) const { return compare(a[y], a[x]) ? y : x; } void buildSparseTable(const T *a, int n) { int height = 0; while ((1 << height) < blocks) ++height; table.resize(blocks * (height + 1)); log.assign(blocks + 1, -1); for (int i = 1; i <= blocks; ++i) { log[i] = log[i >> 1] + 1; } int *u = table.data(); for (int i = 0, idx = 0; i < n; i += block_size, ++idx) { u[idx] = i; for (int j = i + 1; j < n && j < i + block_size; ++j) { u[idx] = leftMinIndex(a, u[idx], j); } } for (int t = 1; t * 2 < blocks; t *= 2) { memcpy(u + blocks, u, sizeof(int) * blocks); u += blocks; for (int i = 0; i < blocks - t; ++i) { u[i] = leftMinIndex(a, u[i], u[i + t]); } } } void buildInnerBlocks(const T *a, int n) { lookup.assign(n, 0); std::vector<int> stack(block_size); for (int i = 0; i < blocks; ++i) { int l = i * block_size; int r = std::min(n, l + block_size); uint32_t state = 0; for (int j = l, top = 0; j < r; ++j) { while (top && compare(a[j], a[stack[top - 1]])) { state ^= uint32_t(1) << (stack[top - 1] - l); --top; } stack[top++] = j; state ^= uint32_t(1) << (j - l); lookup[j] = state; } } } std::vector<uint32_t> lookup; std::vector<int> log; std::vector<int> table; int blocks; Compare compare; }; const int N = 300000 + 10, mod = 1e9 + 7; struct Tarjan {// index from 0 to n - 1 int low[N], dfn[N], col[N]; int stk[N], top, scc_cnt, sz; void dfs(int x, const std::vector<int> G[]) { low[x] = dfn[x] = ++sz; stk[++top] = x; for (auto &y: G[x]) { if (!dfn[y]) { dfs(y, G); low[x] = std::min(low[x], low[y]); } else if (col[y] == -1) { low[x] = std::min(low[x], dfn[y]); } } if (dfn[x] == low[x]) { do { col[stk[top]] = scc_cnt; } while (stk[top--] != x); scc_cnt++; } } void run(int n,const std::vector<int> G[]) { sz = top = scc_cnt = 0; memset(dfn, 0, sizeof(*dfn) * n); memset(col, -1, sizeof(*col) * n); for (int i = 0; i < n; ++i) { if (!dfn[i]) dfs(i,G); } } } scc; std::vector<int> edges[N], dag[N]; i64 a[N], r[N], x[N], y[N], xs[N * 2]; int deg[N], id[N], ways[N * 2]; int n, m; void add(int x, int y) { for (; x <= m; x += ~x &x + 1) { ways[x] = (ways[x] + y) % mod; } } int get(int x, int r = 0) { for (; x >= 0; x -= ~x & x + 1) { r = (r + ways[x]) % mod; } return r; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &a[i], &r[i]); x[i] = a[i] - r[i]; y[i] = a[i] + r[i]; } BitDirectRMQ<i64, std::less<i64>> rmq_mi; BitDirectRMQ<i64, std::greater<i64>> rmq_mx; rmq_mi.build(x, n); rmq_mx.build(y, n); for (int i = 0; i < n; ++i) { int l = std::lower_bound(a, a + n, x[i]) - a; int r = std::upper_bound(a, a + n, y[i]) - a; int p = rmq_mi.query(x, l, r - 1); int q = rmq_mx.query(y, l, r - 1); edges[i] = {p, q}; //printf("l=%d r=%d p=%d q=%d\n", l, r, p, q); } scc.run(n, edges); for (int i = 0; i < scc.scc_cnt; ++i) { x[i] = a[n - 1] + r[n - 1]; y[i] = a[0] - r[0]; } for (int u = 0; u < n; ++u) { int cu = scc.col[u]; x[cu] = std::min(x[cu], a[u] - r[u]); y[cu] = std::max(y[cu], a[u] + r[u]); for (auto &v: edges[u]) { if (cu != scc.col[v]) { dag[scc.col[v]].push_back(cu); deg[cu]++; } } } std::vector<int> queue; for (int i = 0; i < scc.scc_cnt; ++i) { if (deg[i] == 0) queue.push_back(i); } for (size_t i = 0; i < queue.size(); ++i) { int u = queue[i]; for (auto &v: dag[u]) { if (--deg[v] == 0) queue.push_back(v); x[v] = std::min(x[v], x[u]); y[v] = std::max(y[v], y[u]); } } for (int i = 0; i < scc.scc_cnt; ++i) { x[i] = std::lower_bound(a, a + n, x[i]) - a; y[i] = std::upper_bound(a, a + n, y[i]) - a - 1; //printf("x=%lld y=%lld\n", x[i], y[i]); } n = scc.scc_cnt; // dp for (int i = 0; i < n * 2; i += 2) { id[i / 2] = i / 2; xs[i] = x[i / 2]; xs[i + 1] = y[i / 2]; } std::sort(xs, xs + n * 2); m = std::unique(xs, xs + n * 2) - xs; std::sort(id, id + n, [&](int a, int b) { return x[a] < x[b] || (x[a] == x[b] && y[a] > y[b]); }); add(0, 1); for (int i = 0; i < n; ++i) { if (i && x[id[i - 1]] == x[id[i]] && y[id[i - 1]] == y[id[i]]) continue; int p = std::lower_bound(xs, xs + m, y[id[i]]) - xs + 1; add(p, get(p - 1)); } printf("%d\n", get(m)); 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 | #include <cstdio> #include <vector> #include <cstring> #include <functional> #include <algorithm> using i64 = long long; template <class T, class Compare = std::less<T> > class BitDirectRMQ { public: static constexpr int block_size = 32; void build(const std::vector<T> &a) { build(a.data(), a.size()); } void build(const T *a, int n) { blocks = (n + block_size - 1) / block_size; buildSparseTable(a, n); buildInnerBlocks(a, n); } // return the index of the minimum value in [l, r] in O(1) int query(const T *a, int l, int r) const { int x = l / block_size, y = r / block_size; if (x == y) return query_lookup(l, r); int ret = l; if (x + 1 <= y - 1) ret = query_st(a, x + 1, y - 1); ret = leftMinIndex(a, query_lookup(l, x * block_size + block_size - 1), ret); ret = leftMinIndex(a, ret, query_lookup(y * block_size, r)); return ret; } int query_st(const T *a, int x, int y) const { int z = log[y - x + 1]; return leftMinIndex(a, table[x + z * blocks], table[y - (1 << z) + 1 + z * blocks]); } int query_lookup(int x, int y) const { return x + __builtin_ctz(lookup[y] >> (x % block_size)); } private: int minIndex(const T *a, int x, int y) const { return compare(a[x], a[y]) || (a[x] == a[y] && x < y) ? x : y; } int leftMinIndex(const T *a, int x, int y) const { return compare(a[y], a[x]) ? y : x; } void buildSparseTable(const T *a, int n) { int height = 0; while ((1 << height) < blocks) ++height; table.resize(blocks * (height + 1)); log.assign(blocks + 1, -1); for (int i = 1; i <= blocks; ++i) { log[i] = log[i >> 1] + 1; } int *u = table.data(); for (int i = 0, idx = 0; i < n; i += block_size, ++idx) { u[idx] = i; for (int j = i + 1; j < n && j < i + block_size; ++j) { u[idx] = leftMinIndex(a, u[idx], j); } } for (int t = 1; t * 2 < blocks; t *= 2) { memcpy(u + blocks, u, sizeof(int) * blocks); u += blocks; for (int i = 0; i < blocks - t; ++i) { u[i] = leftMinIndex(a, u[i], u[i + t]); } } } void buildInnerBlocks(const T *a, int n) { lookup.assign(n, 0); std::vector<int> stack(block_size); for (int i = 0; i < blocks; ++i) { int l = i * block_size; int r = std::min(n, l + block_size); uint32_t state = 0; for (int j = l, top = 0; j < r; ++j) { while (top && compare(a[j], a[stack[top - 1]])) { state ^= uint32_t(1) << (stack[top - 1] - l); --top; } stack[top++] = j; state ^= uint32_t(1) << (j - l); lookup[j] = state; } } } std::vector<uint32_t> lookup; std::vector<int> log; std::vector<int> table; int blocks; Compare compare; }; const int N = 300000 + 10, mod = 1e9 + 7; struct Tarjan {// index from 0 to n - 1 int low[N], dfn[N], col[N]; int stk[N], top, scc_cnt, sz; void dfs(int x, const std::vector<int> G[]) { low[x] = dfn[x] = ++sz; stk[++top] = x; for (auto &y: G[x]) { if (!dfn[y]) { dfs(y, G); low[x] = std::min(low[x], low[y]); } else if (col[y] == -1) { low[x] = std::min(low[x], dfn[y]); } } if (dfn[x] == low[x]) { do { col[stk[top]] = scc_cnt; } while (stk[top--] != x); scc_cnt++; } } void run(int n,const std::vector<int> G[]) { sz = top = scc_cnt = 0; memset(dfn, 0, sizeof(*dfn) * n); memset(col, -1, sizeof(*col) * n); for (int i = 0; i < n; ++i) { if (!dfn[i]) dfs(i,G); } } } scc; std::vector<int> edges[N], dag[N]; i64 a[N], r[N], x[N], y[N], xs[N * 2]; int deg[N], id[N], ways[N * 2]; int n, m; void add(int x, int y) { for (; x <= m; x += ~x &x + 1) { ways[x] = (ways[x] + y) % mod; } } int get(int x, int r = 0) { for (; x >= 0; x -= ~x & x + 1) { r = (r + ways[x]) % mod; } return r; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &a[i], &r[i]); x[i] = a[i] - r[i]; y[i] = a[i] + r[i]; } BitDirectRMQ<i64, std::less<i64>> rmq_mi; BitDirectRMQ<i64, std::greater<i64>> rmq_mx; rmq_mi.build(x, n); rmq_mx.build(y, n); for (int i = 0; i < n; ++i) { int l = std::lower_bound(a, a + n, x[i]) - a; int r = std::upper_bound(a, a + n, y[i]) - a; int p = rmq_mi.query(x, l, r - 1); int q = rmq_mx.query(y, l, r - 1); edges[i] = {p, q}; //printf("l=%d r=%d p=%d q=%d\n", l, r, p, q); } scc.run(n, edges); for (int i = 0; i < scc.scc_cnt; ++i) { x[i] = a[n - 1] + r[n - 1]; y[i] = a[0] - r[0]; } for (int u = 0; u < n; ++u) { int cu = scc.col[u]; x[cu] = std::min(x[cu], a[u] - r[u]); y[cu] = std::max(y[cu], a[u] + r[u]); for (auto &v: edges[u]) { if (cu != scc.col[v]) { dag[scc.col[v]].push_back(cu); deg[cu]++; } } } std::vector<int> queue; for (int i = 0; i < scc.scc_cnt; ++i) { if (deg[i] == 0) queue.push_back(i); } for (size_t i = 0; i < queue.size(); ++i) { int u = queue[i]; for (auto &v: dag[u]) { if (--deg[v] == 0) queue.push_back(v); x[v] = std::min(x[v], x[u]); y[v] = std::max(y[v], y[u]); } } for (int i = 0; i < scc.scc_cnt; ++i) { x[i] = std::lower_bound(a, a + n, x[i]) - a; y[i] = std::upper_bound(a, a + n, y[i]) - a - 1; //printf("x=%lld y=%lld\n", x[i], y[i]); } n = scc.scc_cnt; // dp for (int i = 0; i < n * 2; i += 2) { id[i / 2] = i / 2; xs[i] = x[i / 2]; xs[i + 1] = y[i / 2]; } std::sort(xs, xs + n * 2); m = std::unique(xs, xs + n * 2) - xs; std::sort(id, id + n, [&](int a, int b) { return x[a] < x[b] || (x[a] == x[b] && y[a] > y[b]); }); add(0, 1); for (int i = 0; i < n; ++i) { if (i && x[id[i - 1]] == x[id[i]] && y[id[i - 1]] == y[id[i]]) continue; int p = std::lower_bound(xs, xs + m, y[id[i]]) - xs + 1; add(p, get(p - 1)); } printf("%d\n", get(m)); return 0; } |