#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int add(int x, int y) { int ans = x + y; if (ans >= MOD) { ans -= MOD; } return ans; } int mul(int x, int y) { return ((long long) x * y) % MOD; } struct Testcase { int n; vector <long long> pos, rad; vector <int> jumpNo, jumpYes; vector <map<int,pair<int,int>>> cache; void read() { cin >> n; pos.resize(n); rad.resize(n); for (int i = 0; i < n; i++) { cin >> pos[i] >> rad[i]; } } int dpAll(int l, int r) { auto res = dp(l, r); return add(res.first, res.second); } pair <int,int> dp(int l, int r) { if (l == r) { return {1, 1}; } auto it = cache[r].find(l); if (it != cache[r].end()) { return it->second; } int ansNo, ansYes; if (jumpNo[r] >= l) { int pref = dp(l, jumpNo[r]).first; if (jumpNo[r] < r - 1) { int suff = dpAll(jumpNo[r] + 1, r - 1); ansNo = mul(pref, suff); } else { ansNo = pref; } } else { ansNo = dpAll(l, r - 1); } if (jumpYes[r] >= l) { ansYes = dpAll(l, jumpYes[r]); } else { ansYes = 1; } pair <int,int> ans = {ansNo, ansYes}; cache[r][l] = ans; return ans; } int solve() { jumpNo.resize(n, -1); jumpYes.resize(n, -1); vector <pair<long long, int>> optLeft, optRight, optRightmost; for (int i = 0; i < n; i++) { long long leftmost = pos[i] - rad[i]; while (!optLeft.empty() && pos[optLeft.back().second] >= leftmost) { leftmost = min(leftmost, optLeft.back().first); optLeft.pop_back(); } optLeft.push_back({leftmost, i}); jumpYes[i] = lower_bound(pos.begin(), pos.end(), leftmost) - pos.begin() - 1; long long rightmost = pos[i] + rad[i]; while (!optRight.empty() && pos[optRight.back().second] >= leftmost) { rightmost = max(rightmost, optRight.back().first); optRight.pop_back(); } optRight.push_back({rightmost, i}); if (i > 0) { pair <long long, int> e = {pos[i], -1}; auto it = upper_bound(optRightmost.begin(), optRightmost.end(), e, greater <pair<long long,int>> ()); if (it != optRightmost.begin()) { jumpNo[i] = prev(it)->second; } } while (!optRightmost.empty() && optRightmost.back().first <= rightmost) { optRightmost.pop_back(); } optRightmost.push_back({rightmost, i}); } cache.resize(n); return dpAll(0, n - 1); } }; int main() { ios_base::sync_with_stdio(false); Testcase test; test.read(); cout << test.solve() << '\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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int add(int x, int y) { int ans = x + y; if (ans >= MOD) { ans -= MOD; } return ans; } int mul(int x, int y) { return ((long long) x * y) % MOD; } struct Testcase { int n; vector <long long> pos, rad; vector <int> jumpNo, jumpYes; vector <map<int,pair<int,int>>> cache; void read() { cin >> n; pos.resize(n); rad.resize(n); for (int i = 0; i < n; i++) { cin >> pos[i] >> rad[i]; } } int dpAll(int l, int r) { auto res = dp(l, r); return add(res.first, res.second); } pair <int,int> dp(int l, int r) { if (l == r) { return {1, 1}; } auto it = cache[r].find(l); if (it != cache[r].end()) { return it->second; } int ansNo, ansYes; if (jumpNo[r] >= l) { int pref = dp(l, jumpNo[r]).first; if (jumpNo[r] < r - 1) { int suff = dpAll(jumpNo[r] + 1, r - 1); ansNo = mul(pref, suff); } else { ansNo = pref; } } else { ansNo = dpAll(l, r - 1); } if (jumpYes[r] >= l) { ansYes = dpAll(l, jumpYes[r]); } else { ansYes = 1; } pair <int,int> ans = {ansNo, ansYes}; cache[r][l] = ans; return ans; } int solve() { jumpNo.resize(n, -1); jumpYes.resize(n, -1); vector <pair<long long, int>> optLeft, optRight, optRightmost; for (int i = 0; i < n; i++) { long long leftmost = pos[i] - rad[i]; while (!optLeft.empty() && pos[optLeft.back().second] >= leftmost) { leftmost = min(leftmost, optLeft.back().first); optLeft.pop_back(); } optLeft.push_back({leftmost, i}); jumpYes[i] = lower_bound(pos.begin(), pos.end(), leftmost) - pos.begin() - 1; long long rightmost = pos[i] + rad[i]; while (!optRight.empty() && pos[optRight.back().second] >= leftmost) { rightmost = max(rightmost, optRight.back().first); optRight.pop_back(); } optRight.push_back({rightmost, i}); if (i > 0) { pair <long long, int> e = {pos[i], -1}; auto it = upper_bound(optRightmost.begin(), optRightmost.end(), e, greater <pair<long long,int>> ()); if (it != optRightmost.begin()) { jumpNo[i] = prev(it)->second; } } while (!optRightmost.empty() && optRightmost.back().first <= rightmost) { optRightmost.pop_back(); } optRightmost.push_back({rightmost, i}); } cache.resize(n); return dpAll(0, n - 1); } }; int main() { ios_base::sync_with_stdio(false); Testcase test; test.read(); cout << test.solve() << '\n'; return 0; } |