#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int mod = 1e9 + 7; void add_self(int& a, int b) { a += b; if(a >= mod) { a -= mod; } } struct Mine { long long L, posL, posR, R; void read() { long long pos, range; scanf("%lld%lld", &pos, &range); posL = posR = pos; L = pos - range; R = pos + range; } // bool hits(const Mine& he) const { // if(R < he.posL || he.posR < L) { // return false; // } // return true; // } void combineWith(const Mine& he) { L = min(L, he.L); posL = min(posL, he.posL); R = max(R, he.R); posR = max(posR, he.posR); } bool contains(const Mine& he) const { return posL < he.posL && he.posR < posR; } bool hits(const Mine& he) const { if(he.contains(*this)) { return false; } if(R < he.posL || he.posR < L) { return false; } return true; } }; debug & operator << (debug & dd, Mine mine) { dd << "(" << mine.L << ", " << mine.posL << ", " << mine.posR << ", " << mine.R << ")"; return dd; } // struct Node { // long long big; // } int main() { int n; scanf("%d", &n); vector<Mine> offline(n); vector<long long> values; for(int i = 0; i < n; ++i) { offline[i].read(); values.push_back(offline[i].posL); values.push_back(offline[i].R); values.push_back(offline[i].L); } sort(values.begin(), values.end()); values.resize( unique(values.begin(), values.end()) - values.begin() ); int k = values.size(); auto get_id = [&](long long x) { auto it = lower_bound(values.begin(), values.end(), x); assert(it != values.end() && *it == x); return it - values.begin(); }; vector<vector<Mine>> there(k); int base = 1; while(base <= k) { base *= 2; } vector<pair<long long, int>> tree(2 * base, make_pair(LONG_LONG_MIN, -1)); auto update_leaf = [&](int j) { tree[base+j] = (there[j].empty() ? make_pair(LONG_LONG_MIN, -1) : make_pair(there[j].back().posR, j)); for(int x = (base + j) / 2; x >= 1; x /= 2) { tree[x] = max(tree[2*x], tree[2*x+1]); } }; vector<Mine> skipped; // vector<Mine> mines; for(int i = 0; i < n; ++i) { Mine nowe = offline[i]; long long memo_L = nowe.L; while(true) { pair<long long, int> best{LONG_LONG_MIN, -1}; auto consider = [&](int id) { best = max(best, tree[id]); }; int at_least = base + get_id(nowe.posL); consider(at_least); while(at_least > 1) { if(at_least % 2 == 0) { consider(at_least + 1); } at_least /= 2; } /*for(int j = at_least; j < k; ++j) { if(!there[j].empty()) { best = max(best, make_pair(there[j].back().posR, j)); } }*/ /*for(int j = 0; j < (int) mines.size(); ++j) { Mine he = mines[j]; if(he.R >= nowe.posL) { best = max(best, make_pair(he.posR, j)); } }*/ if(best.first < memo_L) { break; } int j = best.second; // mines[j] Mine he = there[j].back(); there[j].pop_back(); update_leaf(j); if(he.posL >= nowe.posL) { skipped.push_back(he); } else { nowe.combineWith(he); } } int id = get_id(nowe.R); there[id].push_back(nowe); update_leaf(id); } vector<Mine> mines = skipped; for(const vector<Mine>& they : there) { mines.insert(mines.end(), they.begin(), they.end()); } sort(mines.begin(), mines.end(), [&](const Mine& a, const Mine& b) { return a.posR < b.posR; }); cerr << "new size = " << mines.size() << endl; // return 0; /* for(int j = (int) mines.size() - 1; j >= 0; --j) { if(nowe.posL <= mines[j].R && memo_L <= mines[j].posR) { if(skip.count(mines[j].posL)) { continue; } if(mines[j].posL >= nowe.posL) { skip.insert(mines[j].posL); continue; } nowe.combineWith(mines[j]); mines.erase(mines.begin() + j); } } mines.push_back(nowe); // debug() << imie(mines.size()); debug() << imie(mines); }*/ n = mines.size(); // sorted by posR vector<pair<int,int>> intervals(n); // vector<pair<long long, int>> his_reach(n); #define tree tree2 vector<int> tree(2 * base); for(int i = n - 1; i >= 0; --i) { int R = i; const long long my_reach = mines[i].R; auto consider = [&](int id) { R = max(R, tree[id]); }; int low = base + get_id(mines[i].posL) + 1; int high = base + get_id(my_reach); if(low <= high) { consider(low); consider(high); while(low + 1 < high) { if(low % 2 == 0) { consider(low + 1); } if(high % 2 == 1) { consider(high - 1); } low /= 2; high /= 2; } } /* for(int j = i + 1; j < n; ++j) { if(mines[i].posL < mines[j].posL && mines[j].posL <= my_reach) { R = max(R, intervals[j].second); // reach = max(reach, mines[j].R); } }*/ intervals[i].second = R; int id = get_id(mines[i].posL); for(int x = base + id; x >= 1; x /= 2) { tree[x] = max(tree[x], R); } } tree = vector<int>(2 * base, INT_MAX); for(int i = 0; i < n; ++i) { int L = i; //, R = i; /*long long reach = mines[i].R; for(int j = i + 1; j < n; ++j) { if(reach >= mines[j].posL && mines[i].posL < mines[j].posL) { R = j; reach = max(reach, mines[j].R); } }*/ auto consider = [&](int id) { L = min(L, tree[id]); }; long long my_reach = mines[i].L; int low = base + get_id(my_reach); int high = base + get_id(mines[i].posR) - 1; if(low <= high) { consider(low); consider(high); while(low + 1 < high) { if(low % 2 == 0) { consider(low + 1); } if(high % 2 == 1) { consider(high - 1); } low /= 2; high /= 2; } } /*for(int j = i - 1; j >= 0; --j) { if(reach <= mines[j].posR && mines[j].posR < mines[i].posR) { L = j; reach = min(reach, mines[j].L); } }*/ intervals[i].first = L; int id = get_id(mines[i].posR); for(int x = base + id; x >= 1; x /= 2) { tree[x] = min(tree[x], L); } } // sort(intervals.begin(), intervals.end()); // intervals.resize( unique(intervals.begin(), intervals.end()) - intervals.begin() ); // debug() << imie(intervals); /* vector<int> reach(n, -1); for(pair<int,int> p : intervals) { reach[p.first] = max(reach[p.first], p.second); // for(int i = p.first; i <= p.second; ++i) { // reach[i] = max(reach[i], p.second); // } } debug() << imie(reach);*/ for(pair<int,int>& p : intervals) { p.first++; p.second++; p.second *= -1; } sort(intervals.begin(), intervals.end()); vector<int> dp(n + 1); // dp[0] = 1; tree = vector<int>(2 * base); auto add = [&](int i, int value) { add_self(dp[i], value); for(int x = base + i; x >= 1; x /= 2) { add_self(tree[x], value); } }; add(0, 1); // debug() << imie(intervals); for(pair<int,int> p : intervals) { p.second *= -1; int added = 0; int i = base + p.second; while(i > 1) { if(i % 2 == 1) { add_self(added, tree[i-1]); } i /= 2; } /*for(int i = 0; i < p.second; ++i) { add_self(dp[p.second], dp[i]); // dp[p.second] += dp[i]; }*/ add(p.second, added); } int answer = 0; for(int x : dp) { add_self(answer, x); // answer += x; } printf("%d\n", answer); /* // set<set<int>> all; int answer = 0; for(int mask = 0; mask < (1 << n); ++mask) { bool ok = true; auto contains = [&](pair<int,int> a, pair<int,int> b) { return a.first <= b.first && b.second <= a.second; }; for(int a = 0; a < n; ++a) { if(mask & (1 << a)) { for(int b = a + 1; b < n; ++b) { if(mask & (1 << b)) { if(contains(intervals[a], intervals[b]) || contains(intervals[b], intervals[a])) { ok = false; } } } } } answer += ok; // set<int> s; // for(int who = 0; who < n; ++who) { // if(mask & (1 << who)) { // for(int i = intervals[who].first; i <= intervals[who].second; ++i) { // s.insert(i); // } // } // } // all.insert(s); // if(s.size() == __builtin_popcount(mask) && !all.count(s)) { // all.insert(s); // debug() << imie(mask) imie(s); // } } // printf("%d\n", (int) all.size()); printf("%d\n", answer);*/ }
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int mod = 1e9 + 7; void add_self(int& a, int b) { a += b; if(a >= mod) { a -= mod; } } struct Mine { long long L, posL, posR, R; void read() { long long pos, range; scanf("%lld%lld", &pos, &range); posL = posR = pos; L = pos - range; R = pos + range; } // bool hits(const Mine& he) const { // if(R < he.posL || he.posR < L) { // return false; // } // return true; // } void combineWith(const Mine& he) { L = min(L, he.L); posL = min(posL, he.posL); R = max(R, he.R); posR = max(posR, he.posR); } bool contains(const Mine& he) const { return posL < he.posL && he.posR < posR; } bool hits(const Mine& he) const { if(he.contains(*this)) { return false; } if(R < he.posL || he.posR < L) { return false; } return true; } }; debug & operator << (debug & dd, Mine mine) { dd << "(" << mine.L << ", " << mine.posL << ", " << mine.posR << ", " << mine.R << ")"; return dd; } // struct Node { // long long big; // } int main() { int n; scanf("%d", &n); vector<Mine> offline(n); vector<long long> values; for(int i = 0; i < n; ++i) { offline[i].read(); values.push_back(offline[i].posL); values.push_back(offline[i].R); values.push_back(offline[i].L); } sort(values.begin(), values.end()); values.resize( unique(values.begin(), values.end()) - values.begin() ); int k = values.size(); auto get_id = [&](long long x) { auto it = lower_bound(values.begin(), values.end(), x); assert(it != values.end() && *it == x); return it - values.begin(); }; vector<vector<Mine>> there(k); int base = 1; while(base <= k) { base *= 2; } vector<pair<long long, int>> tree(2 * base, make_pair(LONG_LONG_MIN, -1)); auto update_leaf = [&](int j) { tree[base+j] = (there[j].empty() ? make_pair(LONG_LONG_MIN, -1) : make_pair(there[j].back().posR, j)); for(int x = (base + j) / 2; x >= 1; x /= 2) { tree[x] = max(tree[2*x], tree[2*x+1]); } }; vector<Mine> skipped; // vector<Mine> mines; for(int i = 0; i < n; ++i) { Mine nowe = offline[i]; long long memo_L = nowe.L; while(true) { pair<long long, int> best{LONG_LONG_MIN, -1}; auto consider = [&](int id) { best = max(best, tree[id]); }; int at_least = base + get_id(nowe.posL); consider(at_least); while(at_least > 1) { if(at_least % 2 == 0) { consider(at_least + 1); } at_least /= 2; } /*for(int j = at_least; j < k; ++j) { if(!there[j].empty()) { best = max(best, make_pair(there[j].back().posR, j)); } }*/ /*for(int j = 0; j < (int) mines.size(); ++j) { Mine he = mines[j]; if(he.R >= nowe.posL) { best = max(best, make_pair(he.posR, j)); } }*/ if(best.first < memo_L) { break; } int j = best.second; // mines[j] Mine he = there[j].back(); there[j].pop_back(); update_leaf(j); if(he.posL >= nowe.posL) { skipped.push_back(he); } else { nowe.combineWith(he); } } int id = get_id(nowe.R); there[id].push_back(nowe); update_leaf(id); } vector<Mine> mines = skipped; for(const vector<Mine>& they : there) { mines.insert(mines.end(), they.begin(), they.end()); } sort(mines.begin(), mines.end(), [&](const Mine& a, const Mine& b) { return a.posR < b.posR; }); cerr << "new size = " << mines.size() << endl; // return 0; /* for(int j = (int) mines.size() - 1; j >= 0; --j) { if(nowe.posL <= mines[j].R && memo_L <= mines[j].posR) { if(skip.count(mines[j].posL)) { continue; } if(mines[j].posL >= nowe.posL) { skip.insert(mines[j].posL); continue; } nowe.combineWith(mines[j]); mines.erase(mines.begin() + j); } } mines.push_back(nowe); // debug() << imie(mines.size()); debug() << imie(mines); }*/ n = mines.size(); // sorted by posR vector<pair<int,int>> intervals(n); // vector<pair<long long, int>> his_reach(n); #define tree tree2 vector<int> tree(2 * base); for(int i = n - 1; i >= 0; --i) { int R = i; const long long my_reach = mines[i].R; auto consider = [&](int id) { R = max(R, tree[id]); }; int low = base + get_id(mines[i].posL) + 1; int high = base + get_id(my_reach); if(low <= high) { consider(low); consider(high); while(low + 1 < high) { if(low % 2 == 0) { consider(low + 1); } if(high % 2 == 1) { consider(high - 1); } low /= 2; high /= 2; } } /* for(int j = i + 1; j < n; ++j) { if(mines[i].posL < mines[j].posL && mines[j].posL <= my_reach) { R = max(R, intervals[j].second); // reach = max(reach, mines[j].R); } }*/ intervals[i].second = R; int id = get_id(mines[i].posL); for(int x = base + id; x >= 1; x /= 2) { tree[x] = max(tree[x], R); } } tree = vector<int>(2 * base, INT_MAX); for(int i = 0; i < n; ++i) { int L = i; //, R = i; /*long long reach = mines[i].R; for(int j = i + 1; j < n; ++j) { if(reach >= mines[j].posL && mines[i].posL < mines[j].posL) { R = j; reach = max(reach, mines[j].R); } }*/ auto consider = [&](int id) { L = min(L, tree[id]); }; long long my_reach = mines[i].L; int low = base + get_id(my_reach); int high = base + get_id(mines[i].posR) - 1; if(low <= high) { consider(low); consider(high); while(low + 1 < high) { if(low % 2 == 0) { consider(low + 1); } if(high % 2 == 1) { consider(high - 1); } low /= 2; high /= 2; } } /*for(int j = i - 1; j >= 0; --j) { if(reach <= mines[j].posR && mines[j].posR < mines[i].posR) { L = j; reach = min(reach, mines[j].L); } }*/ intervals[i].first = L; int id = get_id(mines[i].posR); for(int x = base + id; x >= 1; x /= 2) { tree[x] = min(tree[x], L); } } // sort(intervals.begin(), intervals.end()); // intervals.resize( unique(intervals.begin(), intervals.end()) - intervals.begin() ); // debug() << imie(intervals); /* vector<int> reach(n, -1); for(pair<int,int> p : intervals) { reach[p.first] = max(reach[p.first], p.second); // for(int i = p.first; i <= p.second; ++i) { // reach[i] = max(reach[i], p.second); // } } debug() << imie(reach);*/ for(pair<int,int>& p : intervals) { p.first++; p.second++; p.second *= -1; } sort(intervals.begin(), intervals.end()); vector<int> dp(n + 1); // dp[0] = 1; tree = vector<int>(2 * base); auto add = [&](int i, int value) { add_self(dp[i], value); for(int x = base + i; x >= 1; x /= 2) { add_self(tree[x], value); } }; add(0, 1); // debug() << imie(intervals); for(pair<int,int> p : intervals) { p.second *= -1; int added = 0; int i = base + p.second; while(i > 1) { if(i % 2 == 1) { add_self(added, tree[i-1]); } i /= 2; } /*for(int i = 0; i < p.second; ++i) { add_self(dp[p.second], dp[i]); // dp[p.second] += dp[i]; }*/ add(p.second, added); } int answer = 0; for(int x : dp) { add_self(answer, x); // answer += x; } printf("%d\n", answer); /* // set<set<int>> all; int answer = 0; for(int mask = 0; mask < (1 << n); ++mask) { bool ok = true; auto contains = [&](pair<int,int> a, pair<int,int> b) { return a.first <= b.first && b.second <= a.second; }; for(int a = 0; a < n; ++a) { if(mask & (1 << a)) { for(int b = a + 1; b < n; ++b) { if(mask & (1 << b)) { if(contains(intervals[a], intervals[b]) || contains(intervals[b], intervals[a])) { ok = false; } } } } } answer += ok; // set<int> s; // for(int who = 0; who < n; ++who) { // if(mask & (1 << who)) { // for(int i = intervals[who].first; i <= intervals[who].second; ++i) { // s.insert(i); // } // } // } // all.insert(s); // if(s.size() == __builtin_popcount(mask) && !all.count(s)) { // all.insert(s); // debug() << imie(mask) imie(s); // } } // printf("%d\n", (int) all.size()); printf("%d\n", answer);*/ } |