#include <iostream> #include <vector> #include <tuple> #include <algorithm> #define MOD 1000000007 #define BALANCE_SEED 973536953 struct MaxRangeTree { std::vector<std::pair<int, long long>> data; inline static int parent(int x) { return (x - 1) / 2; } inline static int left(int x) { return x * 2 + 1; } inline static int right(int x) { return x * 2 + 2; } inline int first_leaf() { return data.size() / 2; } inline std::pair<int, long long> aggregate(std::pair<int, long long> a, std::pair<int, long long> b) { if (a.second > b.second || (a.second == b.second && (a.first ^ BALANCE_SEED) < (b.first ^ BALANCE_SEED))) return a; return b; } inline void update(int node) {data[node] = aggregate(data[left(node)], data[right(node)]); } void init(std::vector<long long> const& initial) { init(initial.size()); for (int i = 0; i < initial.size(); ++i) { data[first_leaf() + i] = {i, initial[i]}; } for (int i = first_leaf() - 1; i >= 0; --i) { update(i); } } void init(int n) { int size = 1; while (size < n) size <<= 1; data.resize(size * 2 - 1, {-1, -1e18}); } std::pair<int, long long> get_max(int from, int to) { int x = from + first_leaf(); int y = to + first_leaf(); std::pair<int, long long> ret = {-1, -1e18}; ret = aggregate(ret, data[x]); ret = aggregate(ret, data[y]); while (parent(x) != parent(y)) { if (left(parent(x)) == x) ret = aggregate(ret, data[right(parent(x))]); if (right(parent(y)) == y) ret = aggregate(ret, data[left(parent(y))]); x = parent(x); y = parent(y); } return ret; } void set(int i, long long v) { int node = i + first_leaf(); data[node].second = v; while (node > 0) { node = parent(node); update(node); } } }; int n; std::vector<long long> P; std::vector<long long> R; std::vector<int> single_right; std::vector<int> single_left; void find_single_right() { single_right.resize(n); for (int i = 0; i < n; ++i) { int l = i, r = n - 1, best = i; while (l <= r) { int m = (l + r) / 2; if (P[m] - P[i] <= R[i]) { best = m; l = m + 1; } else { r = m - 1; } } single_right[i] = best; } } void find_single_left() { single_left.resize(n); for (int i = 0; i < n; ++i) { int l = 0, r = i, best = i; while (l <= r) { int m = (l + r) / 2; if (P[i] - P[m] <= R[i]) { best = m; r = m - 1; } else { l = m + 1; } } single_left[i] = best; } } MaxRangeTree tree_of_ranges; // tree of largest ranges int t_root; std::vector<int> t_left; std::vector<int> t_right; int build_tree_func(int l, int r) { int m = tree_of_ranges.get_max(l, r).first; if (m == r) t_right[m] = m; else t_right[m] = build_tree_func(m + 1, r); if (m == l) t_left[m] = m; else t_left[m] = build_tree_func(l, m - 1); return m; } void build_tree() { tree_of_ranges.init(R); t_left.resize(n); t_right.resize(n); t_root = build_tree_func(0, n - 1); } std::vector<int> true_right_vec; std::vector<int> true_left_vec; std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> true_left_right_func(int i, int bl, int br) { std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> l, r; if (t_left[i] != i) l = true_left_right_func(t_left[i], bl, i - 1); if (t_right[i] != i) r = true_left_right_func(t_right[i], i + 1, br); int k; int right = single_right[i]; bool b_right = right > br; right = std::min(right, br); k = i + 1; for (auto [p, b] : r) { if (right < k || b_right) break; right = std::max(right, p.second); b_right = std::max(b_right, b.second); k++; } int left = single_left[i]; bool b_left = left < bl; left = std::max(left, bl); k = i - 1; for (int q = l.size() - 1; q >= 0; --q) { auto [p, b] = l[q]; if (left > k || b_left) break; left = std::min(left, p.first); b_left = std::max(b_left, b.first); k--; } std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> ret; for (auto [p, b] : l) { if (b.second) { ret.push_back({ {std::min(p.first, left), std::max(p.second, right)}, {std::max(b.first, b_left), b_right}, }); } else { ret.push_back({p, b}); } } ret.push_back({{left, right}, {b_left, b_right}}); for (auto [p, b] : r) { if (b.first) { ret.push_back({ {std::min(p.first, left), std::max(p.second, right)}, {b_left, std::max(b.second, b_right)}, }); } else { ret.push_back({p, b}); } } return ret; } void true_left_right() { true_right_vec.resize(n); true_left_vec.resize(n); auto l = true_left_right_func(t_root, 0, n - 1); int i = 0; for (auto [p, b] : l) { true_left_vec[i] = p.first; true_right_vec[i] = p.second; i++; } } inline long long mod(long long x) { x %= MOD; if (x < 0) x+= MOD; return x; } struct TimeRangeTree { std::vector<std::vector<std::pair<int, long long>>> data; // <time, value> inline static int parent(int x) { return (x - 1) / 2; } inline static int left(int x) { return x * 2 + 1; } inline static int right(int x) { return x * 2 + 2; } inline int first_leaf() { return data.size() / 2; } long long value_from_node(int i, int time) { long long v = 0; int l = 0, r = data[i].size() - 1; while (l <= r) { int m = (l + r) / 2; if (data[i][m].first > time) { r = m - 1; } else { l = m + 1; v = data[i][m].second; } } return v; } void init(int n) { int size = 1; while (size < n) size <<= 1; data.resize(size * 2 - 1); } long long get_sum(int from, int to, int time) { int x = from + first_leaf(); int y = to + first_leaf(); long long ret = value_from_node(x, time); if (x != y) ret += value_from_node(y, time); while (parent(x) != parent(y)) { if (left(parent(x)) == x) ret += value_from_node(right(parent(x)), time); if (right(parent(y)) == y) ret += value_from_node(left(parent(y)), time); x = parent(x); y = parent(y); } return mod(ret); } void add(int i, long long v, int time) { int node = i + first_leaf(); v = mod(v); while (true) { if (data[node].empty()) data[node].push_back({time, v}); else if (data[node].back().first == time) data[node].back().second = mod(data[node].back().second + v); else data[node].push_back({time, mod(data[node].back().second + v)}); if (node == 0) break; node = parent(node); } } }; int main() { std::ios::sync_with_stdio(false); std::cin >> n; n++; P.resize(n); R.resize(n); P[0] = -1000000000000000001LL; R[0] = 0; for (int i = 1; i < n; ++i) { std::cin >> P[i] >> R[i]; } find_single_right(); find_single_left(); build_tree(); true_left_right(); TimeRangeTree T; T.init(n); T.add(0, 1, 0); for (int i = 1; i < n; ++i) { T.add(i - 1, -T.get_sum(i - 1, i - 1, i - 1), i); T.add(i, T.get_sum(i - 1, i - 1, i - 1), i); int rr = true_right_vec[i]; int ll = true_left_vec[i]; T.add(rr, -T.get_sum(rr, rr, i - 1), i); T.add(rr, T.get_sum(ll - 1, rr, ll - 1), i); } std::cout << T.get_sum(n - 1, n - 1, n - 1); 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 291 292 293 294 295 296 297 298 299 | #include <iostream> #include <vector> #include <tuple> #include <algorithm> #define MOD 1000000007 #define BALANCE_SEED 973536953 struct MaxRangeTree { std::vector<std::pair<int, long long>> data; inline static int parent(int x) { return (x - 1) / 2; } inline static int left(int x) { return x * 2 + 1; } inline static int right(int x) { return x * 2 + 2; } inline int first_leaf() { return data.size() / 2; } inline std::pair<int, long long> aggregate(std::pair<int, long long> a, std::pair<int, long long> b) { if (a.second > b.second || (a.second == b.second && (a.first ^ BALANCE_SEED) < (b.first ^ BALANCE_SEED))) return a; return b; } inline void update(int node) {data[node] = aggregate(data[left(node)], data[right(node)]); } void init(std::vector<long long> const& initial) { init(initial.size()); for (int i = 0; i < initial.size(); ++i) { data[first_leaf() + i] = {i, initial[i]}; } for (int i = first_leaf() - 1; i >= 0; --i) { update(i); } } void init(int n) { int size = 1; while (size < n) size <<= 1; data.resize(size * 2 - 1, {-1, -1e18}); } std::pair<int, long long> get_max(int from, int to) { int x = from + first_leaf(); int y = to + first_leaf(); std::pair<int, long long> ret = {-1, -1e18}; ret = aggregate(ret, data[x]); ret = aggregate(ret, data[y]); while (parent(x) != parent(y)) { if (left(parent(x)) == x) ret = aggregate(ret, data[right(parent(x))]); if (right(parent(y)) == y) ret = aggregate(ret, data[left(parent(y))]); x = parent(x); y = parent(y); } return ret; } void set(int i, long long v) { int node = i + first_leaf(); data[node].second = v; while (node > 0) { node = parent(node); update(node); } } }; int n; std::vector<long long> P; std::vector<long long> R; std::vector<int> single_right; std::vector<int> single_left; void find_single_right() { single_right.resize(n); for (int i = 0; i < n; ++i) { int l = i, r = n - 1, best = i; while (l <= r) { int m = (l + r) / 2; if (P[m] - P[i] <= R[i]) { best = m; l = m + 1; } else { r = m - 1; } } single_right[i] = best; } } void find_single_left() { single_left.resize(n); for (int i = 0; i < n; ++i) { int l = 0, r = i, best = i; while (l <= r) { int m = (l + r) / 2; if (P[i] - P[m] <= R[i]) { best = m; r = m - 1; } else { l = m + 1; } } single_left[i] = best; } } MaxRangeTree tree_of_ranges; // tree of largest ranges int t_root; std::vector<int> t_left; std::vector<int> t_right; int build_tree_func(int l, int r) { int m = tree_of_ranges.get_max(l, r).first; if (m == r) t_right[m] = m; else t_right[m] = build_tree_func(m + 1, r); if (m == l) t_left[m] = m; else t_left[m] = build_tree_func(l, m - 1); return m; } void build_tree() { tree_of_ranges.init(R); t_left.resize(n); t_right.resize(n); t_root = build_tree_func(0, n - 1); } std::vector<int> true_right_vec; std::vector<int> true_left_vec; std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> true_left_right_func(int i, int bl, int br) { std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> l, r; if (t_left[i] != i) l = true_left_right_func(t_left[i], bl, i - 1); if (t_right[i] != i) r = true_left_right_func(t_right[i], i + 1, br); int k; int right = single_right[i]; bool b_right = right > br; right = std::min(right, br); k = i + 1; for (auto [p, b] : r) { if (right < k || b_right) break; right = std::max(right, p.second); b_right = std::max(b_right, b.second); k++; } int left = single_left[i]; bool b_left = left < bl; left = std::max(left, bl); k = i - 1; for (int q = l.size() - 1; q >= 0; --q) { auto [p, b] = l[q]; if (left > k || b_left) break; left = std::min(left, p.first); b_left = std::max(b_left, b.first); k--; } std::vector<std::pair<std::pair<int, int>, std::pair<bool, bool>>> ret; for (auto [p, b] : l) { if (b.second) { ret.push_back({ {std::min(p.first, left), std::max(p.second, right)}, {std::max(b.first, b_left), b_right}, }); } else { ret.push_back({p, b}); } } ret.push_back({{left, right}, {b_left, b_right}}); for (auto [p, b] : r) { if (b.first) { ret.push_back({ {std::min(p.first, left), std::max(p.second, right)}, {b_left, std::max(b.second, b_right)}, }); } else { ret.push_back({p, b}); } } return ret; } void true_left_right() { true_right_vec.resize(n); true_left_vec.resize(n); auto l = true_left_right_func(t_root, 0, n - 1); int i = 0; for (auto [p, b] : l) { true_left_vec[i] = p.first; true_right_vec[i] = p.second; i++; } } inline long long mod(long long x) { x %= MOD; if (x < 0) x+= MOD; return x; } struct TimeRangeTree { std::vector<std::vector<std::pair<int, long long>>> data; // <time, value> inline static int parent(int x) { return (x - 1) / 2; } inline static int left(int x) { return x * 2 + 1; } inline static int right(int x) { return x * 2 + 2; } inline int first_leaf() { return data.size() / 2; } long long value_from_node(int i, int time) { long long v = 0; int l = 0, r = data[i].size() - 1; while (l <= r) { int m = (l + r) / 2; if (data[i][m].first > time) { r = m - 1; } else { l = m + 1; v = data[i][m].second; } } return v; } void init(int n) { int size = 1; while (size < n) size <<= 1; data.resize(size * 2 - 1); } long long get_sum(int from, int to, int time) { int x = from + first_leaf(); int y = to + first_leaf(); long long ret = value_from_node(x, time); if (x != y) ret += value_from_node(y, time); while (parent(x) != parent(y)) { if (left(parent(x)) == x) ret += value_from_node(right(parent(x)), time); if (right(parent(y)) == y) ret += value_from_node(left(parent(y)), time); x = parent(x); y = parent(y); } return mod(ret); } void add(int i, long long v, int time) { int node = i + first_leaf(); v = mod(v); while (true) { if (data[node].empty()) data[node].push_back({time, v}); else if (data[node].back().first == time) data[node].back().second = mod(data[node].back().second + v); else data[node].push_back({time, mod(data[node].back().second + v)}); if (node == 0) break; node = parent(node); } } }; int main() { std::ios::sync_with_stdio(false); std::cin >> n; n++; P.resize(n); R.resize(n); P[0] = -1000000000000000001LL; R[0] = 0; for (int i = 1; i < n; ++i) { std::cin >> P[i] >> R[i]; } find_single_right(); find_single_left(); build_tree(); true_left_right(); TimeRangeTree T; T.init(n); T.add(0, 1, 0); for (int i = 1; i < n; ++i) { T.add(i - 1, -T.get_sum(i - 1, i - 1, i - 1), i); T.add(i, T.get_sum(i - 1, i - 1, i - 1), i); int rr = true_right_vec[i]; int ll = true_left_vec[i]; T.add(rr, -T.get_sum(rr, rr, i - 1), i); T.add(rr, T.get_sum(ll - 1, rr, ll - 1), i); } std::cout << T.get_sum(n - 1, n - 1, n - 1); return 0; } |