#include <iostream> #include <vector> #include <algorithm> #include <map> #include <cassert> using namespace std; const int N = 300005, MOD = 1e9+7, C = 1<<19; int n; long long a[N], r[N]; int direct_beg[N], direct_end[N]; void rescale() { for (int i=0; i<n; i++) { direct_beg[i] = lower_bound(a, a+n, a[i]-r[i]) - a; direct_end[i] = upper_bound(a, a+n, a[i]+r[i]) - a - 1; } } pair<int, int> mintree[2*C], maxtree[2*C]; int min_direct_beg(int a, int b) { a += C; b += C; auto res = min(mintree[a], mintree[b]); while (a/2 != b/2) { if (a%2 == 0) res = min(res, mintree[a+1]); if (b%2) res = min(res, mintree[b-1]); a /= 2; b /= 2; } return res.second; } int max_direct_end(int a, int b) { a += C; b += C; auto res = max(maxtree[a], maxtree[b]); while (a/2 != b/2) { if (a%2 == 0) res = max(res, maxtree[a+1]); if (b%2) res = max(res, maxtree[b-1]); a /= 2; b /= 2; } return res.second; } vector<int> v[N]; void build_graph() { for (int i=0; i<n; i++) mintree[C+i] = {direct_beg[i], i}; for (int i=0; i<n; i++) maxtree[C+i] = {direct_end[i], i}; for (int i=C-1; i>0; i--) mintree[i] = min(mintree[2*i], mintree[2*i+1]); for (int i=C-1; i>0; i--) maxtree[i] = max(maxtree[2*i], maxtree[2*i+1]); for (int i=0; i<n; i++) { int beg = direct_beg[i], end = direct_end[i]; int left = min_direct_beg(beg, end); int right = max_direct_end(beg, end); if (left != i) v[i].push_back(left); if (right != i && right != left) v[i].push_back(right); } } int indirect_beg[N], indirect_end[N]; vector<int> vertices, scc, vr[N]; int vis[N]; bool finished[N]; void dfs_rev(int a) { vis[a] = 1; for (int b: vr[a]) if (vis[b] != 1) dfs_rev(b); vertices.push_back(a); } void dfs(int a) { vis[a] = 2; scc.push_back(a); for (int b: v[a]) if (vis[b] != 2) dfs(b); } void calc_scc_ranges() { for (int i=0; i<n; i++) for (int j: v[i]) vr[j].push_back(i); for (int i=0; i<n; i++) if (vis[i] != 1) dfs_rev(i); reverse(vertices.begin(), vertices.end()); for (int x: vertices) { if (vis[x] == 2) continue; dfs(x); int beg = direct_beg[x], end = direct_end[x]; for (int y: scc) { beg = min(beg, direct_beg[y]); end = max(end, direct_end[y]); for (int z: v[y]) { if (!finished[z]) continue; beg = min(beg, indirect_beg[z]); end = max(end, indirect_end[z]); } } for (int y: scc) { indirect_beg[y] = beg; indirect_end[y] = end; finished[y] = true; } scc.clear(); } } int ftree[N]; void add(int x, int val) { x += 2; while (x < N) { ftree[x] = (ftree[x] + val) % MOD; x += x&(-x); } } int prefsum(int x) { x += 2; long long res = 0; while (x) { res += ftree[x]; x -= x&(-x); } return res % MOD; } int ind[N]; int dp() { for (int i=0; i<n; i++) ind[i] = i; sort(ind, ind+n, [&](int a, int b) { return indirect_beg[a] < indirect_beg[b] || (indirect_beg[a] == indirect_beg[b] && indirect_end[a] > indirect_end[b]); }); add(-1, 1); for (int i=0; i<n; i++) { int x = ind[i]; if (i > 0 && indirect_beg[x] == indirect_beg[ind[i-1]] && indirect_end[x] == indirect_end[ind[i-1]]) continue; add(indirect_end[x], prefsum(indirect_end[x]-1)); } return prefsum(n); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=0; i<n; i++) cin >> a[i] >> r[i]; rescale(); build_graph(); calc_scc_ranges(); cout << dp() << "\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 | #include <iostream> #include <vector> #include <algorithm> #include <map> #include <cassert> using namespace std; const int N = 300005, MOD = 1e9+7, C = 1<<19; int n; long long a[N], r[N]; int direct_beg[N], direct_end[N]; void rescale() { for (int i=0; i<n; i++) { direct_beg[i] = lower_bound(a, a+n, a[i]-r[i]) - a; direct_end[i] = upper_bound(a, a+n, a[i]+r[i]) - a - 1; } } pair<int, int> mintree[2*C], maxtree[2*C]; int min_direct_beg(int a, int b) { a += C; b += C; auto res = min(mintree[a], mintree[b]); while (a/2 != b/2) { if (a%2 == 0) res = min(res, mintree[a+1]); if (b%2) res = min(res, mintree[b-1]); a /= 2; b /= 2; } return res.second; } int max_direct_end(int a, int b) { a += C; b += C; auto res = max(maxtree[a], maxtree[b]); while (a/2 != b/2) { if (a%2 == 0) res = max(res, maxtree[a+1]); if (b%2) res = max(res, maxtree[b-1]); a /= 2; b /= 2; } return res.second; } vector<int> v[N]; void build_graph() { for (int i=0; i<n; i++) mintree[C+i] = {direct_beg[i], i}; for (int i=0; i<n; i++) maxtree[C+i] = {direct_end[i], i}; for (int i=C-1; i>0; i--) mintree[i] = min(mintree[2*i], mintree[2*i+1]); for (int i=C-1; i>0; i--) maxtree[i] = max(maxtree[2*i], maxtree[2*i+1]); for (int i=0; i<n; i++) { int beg = direct_beg[i], end = direct_end[i]; int left = min_direct_beg(beg, end); int right = max_direct_end(beg, end); if (left != i) v[i].push_back(left); if (right != i && right != left) v[i].push_back(right); } } int indirect_beg[N], indirect_end[N]; vector<int> vertices, scc, vr[N]; int vis[N]; bool finished[N]; void dfs_rev(int a) { vis[a] = 1; for (int b: vr[a]) if (vis[b] != 1) dfs_rev(b); vertices.push_back(a); } void dfs(int a) { vis[a] = 2; scc.push_back(a); for (int b: v[a]) if (vis[b] != 2) dfs(b); } void calc_scc_ranges() { for (int i=0; i<n; i++) for (int j: v[i]) vr[j].push_back(i); for (int i=0; i<n; i++) if (vis[i] != 1) dfs_rev(i); reverse(vertices.begin(), vertices.end()); for (int x: vertices) { if (vis[x] == 2) continue; dfs(x); int beg = direct_beg[x], end = direct_end[x]; for (int y: scc) { beg = min(beg, direct_beg[y]); end = max(end, direct_end[y]); for (int z: v[y]) { if (!finished[z]) continue; beg = min(beg, indirect_beg[z]); end = max(end, indirect_end[z]); } } for (int y: scc) { indirect_beg[y] = beg; indirect_end[y] = end; finished[y] = true; } scc.clear(); } } int ftree[N]; void add(int x, int val) { x += 2; while (x < N) { ftree[x] = (ftree[x] + val) % MOD; x += x&(-x); } } int prefsum(int x) { x += 2; long long res = 0; while (x) { res += ftree[x]; x -= x&(-x); } return res % MOD; } int ind[N]; int dp() { for (int i=0; i<n; i++) ind[i] = i; sort(ind, ind+n, [&](int a, int b) { return indirect_beg[a] < indirect_beg[b] || (indirect_beg[a] == indirect_beg[b] && indirect_end[a] > indirect_end[b]); }); add(-1, 1); for (int i=0; i<n; i++) { int x = ind[i]; if (i > 0 && indirect_beg[x] == indirect_beg[ind[i-1]] && indirect_end[x] == indirect_end[ind[i-1]]) continue; add(indirect_end[x], prefsum(indirect_end[x]-1)); } return prefsum(n); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=0; i<n; i++) cin >> a[i] >> r[i]; rescale(); build_graph(); calc_scc_ranges(); cout << dp() << "\n"; return 0; } |