#include<iostream> #include<queue> #include<map> #include<vector> #include<algorithm> #include<set> #define N 300001 #define B 1048576 #define mp make_pair #define fi first #define se second using namespace std; long long odl[N]; long long r[N]; long long rleft[N]; long long rright[N]; int n; long long t[B * 2 + 1]; int base; const long long min_inf = 2 * (-1e18) - 1; const long long max_inf = 2 * 1e18 + 1; const long long mod = 1e9+7; map<pair<long long, long long>, int> last_ind; priority_queue<pair<pair<long long, long long>, long long> > q; map<long long, int> long_to_ind; vector<long long> vals; void insert_left(int a) { long long val = rleft[a]; a += base; t[a] = val; a >>= 1; while (a > 0) { t[a] = min(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } long long find_left(int a, int b) { a += base; b += base; long long res = min(t[a], t[b]); while ((a >> 1) != (b >> 1)) { if (a % 2 == 0) { res = min(res, t[a + 1]); } if (b % 2 == 1) { res = min(res, t[b - 1]); } a >>= 1; b >>= 1; } return res; } void insert_right(int a) { long long val = rright[a]; a += base; t[a] = val; a >>= 1; while (a > 0) { t[a] = max(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } long long find_right(int a, int b) { a += base; b += base; long long res = max(t[a], t[b]); while ((a >> 1) != (b >> 1)) { if (a % 2 == 0) { res = max(res, t[a + 1]); } if (b % 2 == 1) { res = max(res, t[b - 1]); } a >>= 1; b >>= 1; } return res; } int find_left_in_range_index(int curr_ind) { long long left_range = odl[curr_ind] - r[curr_ind]; int p = 0; int k = curr_ind; int m; while (p < k) { m = (p + k) >> 1; if (odl[m] < left_range) { p = m + 1; } else { k = m; } } return p; } int find_right_in_range_index(int curr_ind) { long long right_range = odl[curr_ind] + r[curr_ind]; int p = curr_ind; int k = n - 1; int m; while (p < k) { m = ((p + k) >> 1) + (p + k) % 2; if (odl[m] > right_range) { k = m - 1; } else { p = m; } } return p; } void fill_left() { fill(t, t + base * 2 + 1, max_inf); rleft[0] = odl[0] - r[0]; insert_left(0); for (int i = 1; i < n; i++) { int ind = find_left_in_range_index(i); rleft[i] = min(odl[i] - r[i], find_left(ind, i)); insert_left(i); } bool change = true; while(change) { change = false; for (int i = 0; i < n; i++) { int ind_left = find_left_in_range_index(i); int ind_right = find_right_in_range_index(i); long long new_val = find_left(ind_left, ind_right); if (rleft[i] != new_val) { rleft[i] = new_val; insert_left(i); change = true; } } } } void fill_map_and_set_base() { for (int i = 0; i < n; i++) { vals.push_back(rleft[i]); vals.push_back(odl[i]); } sort(vals.begin(), vals.end()); int ind = 0; long_to_ind[min_inf] = ind; ind++; long_to_ind[vals[0]] = ind; for (int i = 1; i < vals.size(); i++) { if (vals[i] != vals[i - 1]) { ind++; long_to_ind[vals[i]] = ind; } } vals.clear(); for (base = 8; base <= ind; base *= 2) {} } void fill_right() { fill(t, t + base * 2 + 1, min_inf); rright[n - 1] = odl[n - 1] + r[n - 1]; insert_right(n - 1); for (int i = n - 2; i >= 0; i--) { int ind = find_right_in_range_index(i); rright[i] = max(odl[i] + r[i], find_right(i, ind)); insert_right(i); } bool change = true; while(change) { change = false; for (int i = n - 1; i >= 0; i--) { int ind_left = find_left_in_range_index(i); int ind_right = find_right_in_range_index(i); long long new_val = find_right(ind_left, ind_right); if (rright[i] != new_val) { rright[i] = new_val; insert_right(i); change = true; } } } } void tree_add(long long odli, long long val) { int a = long_to_ind[odli]; a += base; while(a > 0) { t[a] = (t[a] + val) % mod; a >>= 1; } } long long sum_before(long long left_r) { int a = long_to_ind[left_r]; a += base; long long res = 0; while(a > 1) { if (a % 2 == 1) { res = (t[a - 1] + res) % mod; } a >>= 1; } return res; } void queue_to_vec(long long right_limit) { while(!q.empty() && (-1) * q.top().fi.fi < right_limit) { int i = q.top().fi.se; long long res = q.top().se; tree_add(odl[i], res); q.pop(); } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> odl[i] >> r[i]; } for (base = 8; base <= n; base *= 2) {} fill_left(); fill_right(); for (int i = 0; i < n; i++) { pair<long long, long long> x = mp(rleft[i], rright[i]); last_ind[x] = i; } fill_map_and_set_base(); fill(t, t + base * 2 + 1, 0); tree_add(min_inf, 1); for (int i = 0; i < n; i++) { queue_to_vec(odl[i]); pair<long long, long long> x = mp(rleft[i], rright[i]); if (last_ind[x] == i) { q.push(mp(mp(-rright[i], i), sum_before(rleft[i]))); } } queue_to_vec(max_inf); cout << t[1] << endl; 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 | #include<iostream> #include<queue> #include<map> #include<vector> #include<algorithm> #include<set> #define N 300001 #define B 1048576 #define mp make_pair #define fi first #define se second using namespace std; long long odl[N]; long long r[N]; long long rleft[N]; long long rright[N]; int n; long long t[B * 2 + 1]; int base; const long long min_inf = 2 * (-1e18) - 1; const long long max_inf = 2 * 1e18 + 1; const long long mod = 1e9+7; map<pair<long long, long long>, int> last_ind; priority_queue<pair<pair<long long, long long>, long long> > q; map<long long, int> long_to_ind; vector<long long> vals; void insert_left(int a) { long long val = rleft[a]; a += base; t[a] = val; a >>= 1; while (a > 0) { t[a] = min(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } long long find_left(int a, int b) { a += base; b += base; long long res = min(t[a], t[b]); while ((a >> 1) != (b >> 1)) { if (a % 2 == 0) { res = min(res, t[a + 1]); } if (b % 2 == 1) { res = min(res, t[b - 1]); } a >>= 1; b >>= 1; } return res; } void insert_right(int a) { long long val = rright[a]; a += base; t[a] = val; a >>= 1; while (a > 0) { t[a] = max(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } long long find_right(int a, int b) { a += base; b += base; long long res = max(t[a], t[b]); while ((a >> 1) != (b >> 1)) { if (a % 2 == 0) { res = max(res, t[a + 1]); } if (b % 2 == 1) { res = max(res, t[b - 1]); } a >>= 1; b >>= 1; } return res; } int find_left_in_range_index(int curr_ind) { long long left_range = odl[curr_ind] - r[curr_ind]; int p = 0; int k = curr_ind; int m; while (p < k) { m = (p + k) >> 1; if (odl[m] < left_range) { p = m + 1; } else { k = m; } } return p; } int find_right_in_range_index(int curr_ind) { long long right_range = odl[curr_ind] + r[curr_ind]; int p = curr_ind; int k = n - 1; int m; while (p < k) { m = ((p + k) >> 1) + (p + k) % 2; if (odl[m] > right_range) { k = m - 1; } else { p = m; } } return p; } void fill_left() { fill(t, t + base * 2 + 1, max_inf); rleft[0] = odl[0] - r[0]; insert_left(0); for (int i = 1; i < n; i++) { int ind = find_left_in_range_index(i); rleft[i] = min(odl[i] - r[i], find_left(ind, i)); insert_left(i); } bool change = true; while(change) { change = false; for (int i = 0; i < n; i++) { int ind_left = find_left_in_range_index(i); int ind_right = find_right_in_range_index(i); long long new_val = find_left(ind_left, ind_right); if (rleft[i] != new_val) { rleft[i] = new_val; insert_left(i); change = true; } } } } void fill_map_and_set_base() { for (int i = 0; i < n; i++) { vals.push_back(rleft[i]); vals.push_back(odl[i]); } sort(vals.begin(), vals.end()); int ind = 0; long_to_ind[min_inf] = ind; ind++; long_to_ind[vals[0]] = ind; for (int i = 1; i < vals.size(); i++) { if (vals[i] != vals[i - 1]) { ind++; long_to_ind[vals[i]] = ind; } } vals.clear(); for (base = 8; base <= ind; base *= 2) {} } void fill_right() { fill(t, t + base * 2 + 1, min_inf); rright[n - 1] = odl[n - 1] + r[n - 1]; insert_right(n - 1); for (int i = n - 2; i >= 0; i--) { int ind = find_right_in_range_index(i); rright[i] = max(odl[i] + r[i], find_right(i, ind)); insert_right(i); } bool change = true; while(change) { change = false; for (int i = n - 1; i >= 0; i--) { int ind_left = find_left_in_range_index(i); int ind_right = find_right_in_range_index(i); long long new_val = find_right(ind_left, ind_right); if (rright[i] != new_val) { rright[i] = new_val; insert_right(i); change = true; } } } } void tree_add(long long odli, long long val) { int a = long_to_ind[odli]; a += base; while(a > 0) { t[a] = (t[a] + val) % mod; a >>= 1; } } long long sum_before(long long left_r) { int a = long_to_ind[left_r]; a += base; long long res = 0; while(a > 1) { if (a % 2 == 1) { res = (t[a - 1] + res) % mod; } a >>= 1; } return res; } void queue_to_vec(long long right_limit) { while(!q.empty() && (-1) * q.top().fi.fi < right_limit) { int i = q.top().fi.se; long long res = q.top().se; tree_add(odl[i], res); q.pop(); } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> odl[i] >> r[i]; } for (base = 8; base <= n; base *= 2) {} fill_left(); fill_right(); for (int i = 0; i < n; i++) { pair<long long, long long> x = mp(rleft[i], rright[i]); last_ind[x] = i; } fill_map_and_set_base(); fill(t, t + base * 2 + 1, 0); tree_add(min_inf, 1); for (int i = 0; i < n; i++) { queue_to_vec(odl[i]); pair<long long, long long> x = mp(rleft[i], rright[i]); if (last_ind[x] == i) { q.push(mp(mp(-rright[i], i), sum_before(rleft[i]))); } } queue_to_vec(max_inf); cout << t[1] << endl; return 0; } |