#include <bits/stdc++.h> using namespace std; typedef long long int LL; const int MAXN = 300005; const int MAX_TREE = 1 << 19; const int MAX_LOG = 19; const LL M = 1000000007LL; int n; LL A[MAXN], R[MAXN]; int left_idx[MAXN], right_idx[MAXN], rleft_idx[MAXN], rright_idx[MAXN]; int min_tree[2 * MAX_TREE], max_tree[2 * MAX_TREE]; LL tree[MAX_TREE]; unordered_map<LL, LL> S, S_sum; vector<int> needed_S[MAXN], needed_S_sum[MAXN]; inline LL state(int x, int y) { return (LL)x * (n+2) + y; } inline LL get_S(int x, int y) { if (S.count(state(x, y))) return S[state(x, y)]; return ((x == 0) && (y == 0)) ? 1 : 0; } inline LL get_S_sum(int x, int y) { if (S_sum.count(state(x, y))) return S_sum[state(x, y)]; return (x == 0) ? 1 : 0; } inline void set_S(int x, int y, LL v) { S[state(x, y)] = v; } inline void set_S_sum(int x, int y, LL v) { S_sum[state(x, y)] = v; } inline int magic(int x) { return x & (-x); } inline LL tree_query(int x) { x++; LL result = 0; while (x > 0) { result = (result + tree[x]) % M; x -= magic(x); } return result; } inline void tree_update(int x, LL v) { LL delta = (v - (tree_query(x) - tree_query(x-1)) + 2 * M) % M; x++; while (x < MAX_TREE) { tree[x] = (tree[x] + delta) % M; x += magic(x); } } inline void min_tree_update(int x, int y) { x++; x += MAX_TREE; while (x > 0) { min_tree[x] = min(min_tree[x], y); x >>= 1; } } inline int min_tree_query(int f, int t) { f++; t++; f += MAX_TREE-1; t += MAX_TREE+1; int result = n+1; while (f / 2 != t / 2) { if ((f & 1) == 0) result = min(result, min_tree[f + 1]); if ((t & 1) == 1) result = min(result, min_tree[t - 1]); f >>= 1; t >>= 1; } return result; } inline void max_tree_update(int x, int y) { x++; x += MAX_TREE; while (x > 0) { max_tree[x] = max(max_tree[x], y); x >>= 1; } } inline int max_tree_query(int f, int t) { f++; t++; f += MAX_TREE-1; t += MAX_TREE+1; int result = 0; while (f / 2 != t / 2) { if ((f & 1) == 0) result = max(result, max_tree[f + 1]); if ((t & 1) == 1) result = max(result, max_tree[t - 1]); f >>= 1; t >>= 1; } return result; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> A[i] >> R[i]; for (int i = 1; i <= n; i++) { int from_left = 1, to_left = i; while (from_left < to_left) { int mid_left = (from_left + to_left) / 2; if (A[i] - A[mid_left] <= R[i]) to_left = mid_left; else from_left = mid_left + 1; } left_idx[i] = from_left; int from_right = i, to_right = n; while (from_right < to_right) { int mid_right = (from_right + to_right + 1) / 2; if (A[mid_right] - A[i] <= R[i]) from_right = mid_right; else to_right = mid_right - 1; } right_idx[i] = from_right; } for (int i = 0; i < 2*MAX_TREE; i++) { min_tree[i] = n+1; max_tree[i] = 0; } for (int i = 1; i <= n; i++) { rleft_idx[i] = left_idx[i]; rright_idx[i] = right_idx[i]; min_tree_update(i, rleft_idx[i]); max_tree_update(i, rright_idx[i]); } for (int lvl = 0; lvl < MAX_LOG; lvl++) { bool was_updated = false; for (int i = 1; i <= n; i++) { int new_rleft_idx = min_tree_query(rleft_idx[i], rright_idx[i]); if (new_rleft_idx != rleft_idx[i]) { rleft_idx[i] = new_rleft_idx; min_tree_update(i, new_rleft_idx); was_updated = true; } int new_rright_idx = max_tree_query(rleft_idx[i], rright_idx[i]); if (new_rright_idx != rright_idx[i]) { rright_idx[i] = new_rright_idx; max_tree_update(i, new_rright_idx); was_updated = true; } } if (!was_updated) break; } for (int i = 1; i <= n; i++) needed_S[rleft_idx[i]-1].push_back(rright_idx[i]); for (int i = 1; i <= n; i++) needed_S_sum[rleft_idx[i]-1].push_back(i-1); /* for (int i = 1; i <= n; i++) cerr << "i = " << i << ", rleft[i] = " << rleft_idx[i] << ", rright[i] = " << rright_idx[i] << "\n"; */ tree_update(0, 1); for (int x = 1; x <= n; x++) { int y = rright_idx[x]; LL val_xy = get_S(rleft_idx[x]-1, y); LL additional_add = get_S_sum(rleft_idx[x]-1, x-1); val_xy = (val_xy + additional_add) % M; tree_update(y, val_xy); //cerr << "computed S[" << x << "][" << y << "] = " << val_xy << "\n"; //cerr << "used " << rleft_idx[x]-1 << ", " << y << "\n"; //cerr << "used sum " << rleft_idx[x]-1 << ", " << x-1 << "\n"; for (int z : needed_S[x]) set_S(x, z, tree_query(z) - tree_query(z-1)); for (int z : needed_S_sum[x]) set_S_sum(x, z, tree_query(z)); /* for (int z = 0; z <= n; z++) { LL v = tree_query(z) - tree_query(z-1); if (v > 0) cerr << "S[" << x << "][" << z << "] = " << v << "\n"; } */ } /* for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) if (S[x][y]) cerr << "S[" << x << "][" << y << "] = " << S[x][y] << "\n"; */ LL result = tree_query(n); cout << result << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; const int MAXN = 300005; const int MAX_TREE = 1 << 19; const int MAX_LOG = 19; const LL M = 1000000007LL; int n; LL A[MAXN], R[MAXN]; int left_idx[MAXN], right_idx[MAXN], rleft_idx[MAXN], rright_idx[MAXN]; int min_tree[2 * MAX_TREE], max_tree[2 * MAX_TREE]; LL tree[MAX_TREE]; unordered_map<LL, LL> S, S_sum; vector<int> needed_S[MAXN], needed_S_sum[MAXN]; inline LL state(int x, int y) { return (LL)x * (n+2) + y; } inline LL get_S(int x, int y) { if (S.count(state(x, y))) return S[state(x, y)]; return ((x == 0) && (y == 0)) ? 1 : 0; } inline LL get_S_sum(int x, int y) { if (S_sum.count(state(x, y))) return S_sum[state(x, y)]; return (x == 0) ? 1 : 0; } inline void set_S(int x, int y, LL v) { S[state(x, y)] = v; } inline void set_S_sum(int x, int y, LL v) { S_sum[state(x, y)] = v; } inline int magic(int x) { return x & (-x); } inline LL tree_query(int x) { x++; LL result = 0; while (x > 0) { result = (result + tree[x]) % M; x -= magic(x); } return result; } inline void tree_update(int x, LL v) { LL delta = (v - (tree_query(x) - tree_query(x-1)) + 2 * M) % M; x++; while (x < MAX_TREE) { tree[x] = (tree[x] + delta) % M; x += magic(x); } } inline void min_tree_update(int x, int y) { x++; x += MAX_TREE; while (x > 0) { min_tree[x] = min(min_tree[x], y); x >>= 1; } } inline int min_tree_query(int f, int t) { f++; t++; f += MAX_TREE-1; t += MAX_TREE+1; int result = n+1; while (f / 2 != t / 2) { if ((f & 1) == 0) result = min(result, min_tree[f + 1]); if ((t & 1) == 1) result = min(result, min_tree[t - 1]); f >>= 1; t >>= 1; } return result; } inline void max_tree_update(int x, int y) { x++; x += MAX_TREE; while (x > 0) { max_tree[x] = max(max_tree[x], y); x >>= 1; } } inline int max_tree_query(int f, int t) { f++; t++; f += MAX_TREE-1; t += MAX_TREE+1; int result = 0; while (f / 2 != t / 2) { if ((f & 1) == 0) result = max(result, max_tree[f + 1]); if ((t & 1) == 1) result = max(result, max_tree[t - 1]); f >>= 1; t >>= 1; } return result; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> A[i] >> R[i]; for (int i = 1; i <= n; i++) { int from_left = 1, to_left = i; while (from_left < to_left) { int mid_left = (from_left + to_left) / 2; if (A[i] - A[mid_left] <= R[i]) to_left = mid_left; else from_left = mid_left + 1; } left_idx[i] = from_left; int from_right = i, to_right = n; while (from_right < to_right) { int mid_right = (from_right + to_right + 1) / 2; if (A[mid_right] - A[i] <= R[i]) from_right = mid_right; else to_right = mid_right - 1; } right_idx[i] = from_right; } for (int i = 0; i < 2*MAX_TREE; i++) { min_tree[i] = n+1; max_tree[i] = 0; } for (int i = 1; i <= n; i++) { rleft_idx[i] = left_idx[i]; rright_idx[i] = right_idx[i]; min_tree_update(i, rleft_idx[i]); max_tree_update(i, rright_idx[i]); } for (int lvl = 0; lvl < MAX_LOG; lvl++) { bool was_updated = false; for (int i = 1; i <= n; i++) { int new_rleft_idx = min_tree_query(rleft_idx[i], rright_idx[i]); if (new_rleft_idx != rleft_idx[i]) { rleft_idx[i] = new_rleft_idx; min_tree_update(i, new_rleft_idx); was_updated = true; } int new_rright_idx = max_tree_query(rleft_idx[i], rright_idx[i]); if (new_rright_idx != rright_idx[i]) { rright_idx[i] = new_rright_idx; max_tree_update(i, new_rright_idx); was_updated = true; } } if (!was_updated) break; } for (int i = 1; i <= n; i++) needed_S[rleft_idx[i]-1].push_back(rright_idx[i]); for (int i = 1; i <= n; i++) needed_S_sum[rleft_idx[i]-1].push_back(i-1); /* for (int i = 1; i <= n; i++) cerr << "i = " << i << ", rleft[i] = " << rleft_idx[i] << ", rright[i] = " << rright_idx[i] << "\n"; */ tree_update(0, 1); for (int x = 1; x <= n; x++) { int y = rright_idx[x]; LL val_xy = get_S(rleft_idx[x]-1, y); LL additional_add = get_S_sum(rleft_idx[x]-1, x-1); val_xy = (val_xy + additional_add) % M; tree_update(y, val_xy); //cerr << "computed S[" << x << "][" << y << "] = " << val_xy << "\n"; //cerr << "used " << rleft_idx[x]-1 << ", " << y << "\n"; //cerr << "used sum " << rleft_idx[x]-1 << ", " << x-1 << "\n"; for (int z : needed_S[x]) set_S(x, z, tree_query(z) - tree_query(z-1)); for (int z : needed_S_sum[x]) set_S_sum(x, z, tree_query(z)); /* for (int z = 0; z <= n; z++) { LL v = tree_query(z) - tree_query(z-1); if (v > 0) cerr << "S[" << x << "][" << z << "] = " << v << "\n"; } */ } /* for (int x = 0; x <= n; x++) for (int y = 0; y <= n; y++) if (S[x][y]) cerr << "S[" << x << "][" << y << "] = " << S[x][y] << "\n"; */ LL result = tree_query(n); cout << result << "\n"; return 0; } |