#include <iostream> #include <vector> #include <algorithm> #include <list> #include <stack> #include <queue> #include <unordered_map> struct Mine { long long pos; int directLeftStriking = -1, directRightStriking = -1; int nearestLeftActivator = -1, nearestRightActivator = -1; int globalLeftStriking = -1, globalRightStriking = -1; int globallyNearestLeftActivator = -1, globallyNearestRightActivator = -1; }; #define DIVISOR 1000000007 namespace std { template<> struct hash<std::pair<int, int>> { std::size_t operator()(std::pair<int, int> const & p) const noexcept { return std::hash<int>{}(p.first) ^ std::hash<int>{}(p.second); } }; } std::unordered_map<std::pair<int, int>, int> cache{}; long long countPossibilities(const std::vector<Mine> &mines, int startPos, int endPos) { if (startPos > endPos) return 1; auto cachedPossibilities = cache.find(std::make_pair(startPos, endPos)); if (cachedPossibilities != cache.end()) return cachedPossibilities->second; long long result = countPossibilities(mines, mines[startPos].globalRightStriking + 1, endPos); long long notActivatedResult = 1; int startPosIter = startPos; while (mines[startPosIter].globallyNearestRightActivator != -1) { notActivatedResult *= countPossibilities(mines, startPosIter + 1, mines[startPosIter].globallyNearestRightActivator - 1); notActivatedResult %= DIVISOR; startPosIter = mines[startPosIter].globallyNearestRightActivator; } notActivatedResult *= countPossibilities(mines, startPosIter + 1, endPos); notActivatedResult %= DIVISOR; result += notActivatedResult; result %= DIVISOR; cache[std::make_pair(startPos, endPos)] = result; return result; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<Mine> mines; { mines.resize(n); std::vector<long long> positions, strikes; positions.resize(n); strikes.resize(n); for (int i = 0; i < n; ++i) { std::cin >> positions[i] >> strikes[i]; } for (int i = 0; i < n; ++i) { mines[i].directLeftStriking = std::distance(positions.begin(), std::lower_bound(positions.begin(), positions.end(), positions[i] - strikes[i])); mines[i].directRightStriking = std::distance(positions.begin(), std::upper_bound(positions.begin(), positions.end(), positions[i] + strikes[i])) - 1; } } { std::stack<int> rightActivatorsHelper; for (int i = 0; i < n; ++i) { while (not rightActivatorsHelper.empty()) { int top = rightActivatorsHelper.top(); if (top >= mines[i].directLeftStriking) { rightActivatorsHelper.pop(); mines[top].nearestRightActivator = i; } else break; } rightActivatorsHelper.push(i); } } { std::stack<int> leftActivatorsHelper; for (int i = n - 1; i > -1; --i) { while (not leftActivatorsHelper.empty()) { int top = leftActivatorsHelper.top(); if (top <= mines[i].directRightStriking) { leftActivatorsHelper.pop(); mines[top].nearestLeftActivator = i; } else break; } leftActivatorsHelper.push(i); } } { std::vector<int> rightStrikingOrdered; rightStrikingOrdered.reserve(n); for (int i = 0; i < n; ++i) { rightStrikingOrdered.push_back(i); } std::sort(rightStrikingOrdered.begin(), rightStrikingOrdered.end(), [&mines](int i1, int i2) { return mines[i1].directRightStriking < mines[i2].directRightStriking; }); for (int i = n - 1; i > -1; --i) { if (mines[i].globalRightStriking != -1) { continue; } mines[i].globalRightStriking = i; std::queue<int> bfsHelper; bfsHelper.push(i); while (not bfsHelper.empty()) { int top = bfsHelper.front(); bfsHelper.pop(); auto rightActivator = mines[top].nearestRightActivator; if (rightActivator != -1 and mines[rightActivator].globalRightStriking == -1) { mines[rightActivator].globalRightStriking = i; bfsHelper.push(rightActivator); } auto leftActivator = mines[top].nearestLeftActivator; if (leftActivator != -1 and mines[leftActivator].globalRightStriking == -1) { mines[leftActivator].globalRightStriking = i; bfsHelper.push(leftActivator); } } } } { std::vector<int> leftStrikingOrdered; leftStrikingOrdered.reserve(n); for (int i = 0; i < n; ++i) { leftStrikingOrdered.push_back(i); } std::sort(leftStrikingOrdered.begin(), leftStrikingOrdered.end(), [&mines](int i1, int i2) { return mines[i1].directLeftStriking < mines[i2].directLeftStriking; }); for (int i = 0; i < n; ++i) { if (mines[i].globalLeftStriking != -1) { continue; } mines[i].globalLeftStriking = i; std::queue<int> bfsHelper; bfsHelper.push(i); while (not bfsHelper.empty()) { int top = bfsHelper.front(); bfsHelper.pop(); auto rightActivator = mines[top].nearestRightActivator; if (rightActivator != -1 and mines[rightActivator].globalLeftStriking == -1) { mines[rightActivator].globalLeftStriking = i; bfsHelper.push(rightActivator); } auto leftActivator = mines[top].nearestLeftActivator; if (leftActivator != -1 and mines[leftActivator].globalLeftStriking == -1) { mines[leftActivator].globalLeftStriking = i; bfsHelper.push(leftActivator); } } } } { std::stack<int> rightActivatorsHelper; for (int i = 0; i < n; ++i) { while (not rightActivatorsHelper.empty()) { int top = rightActivatorsHelper.top(); if (top >= mines[i].globalLeftStriking) { rightActivatorsHelper.pop(); mines[top].globallyNearestRightActivator = i; } else break; } rightActivatorsHelper.push(i); } } { std::stack<int> leftActivatorsHelper; for (int i = n - 1; i > -1; --i) { while (not leftActivatorsHelper.empty()) { int top = leftActivatorsHelper.top(); if (top <= mines[i].globalRightStriking) { leftActivatorsHelper.pop(); mines[top].globallyNearestLeftActivator = i; } else break; } leftActivatorsHelper.push(i); } } std::vector<std::pair<int, int>> separatedGroups; int groupStart = 0, maxRelated = 0; for (int i = 0; i < n; ++i) { if (mines[i].globalRightStriking > maxRelated) maxRelated = mines[i].globalRightStriking; if (mines[i].globallyNearestRightActivator > maxRelated) maxRelated = mines[i].globallyNearestRightActivator; if (maxRelated == i) { separatedGroups.emplace_back(groupStart, i); groupStart = i + 1; } } long long result = 1; for (auto group : separatedGroups) { result *= countPossibilities(mines, group.first, group.second); result %= DIVISOR; cache.clear(); } std::cout << result << std::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 | #include <iostream> #include <vector> #include <algorithm> #include <list> #include <stack> #include <queue> #include <unordered_map> struct Mine { long long pos; int directLeftStriking = -1, directRightStriking = -1; int nearestLeftActivator = -1, nearestRightActivator = -1; int globalLeftStriking = -1, globalRightStriking = -1; int globallyNearestLeftActivator = -1, globallyNearestRightActivator = -1; }; #define DIVISOR 1000000007 namespace std { template<> struct hash<std::pair<int, int>> { std::size_t operator()(std::pair<int, int> const & p) const noexcept { return std::hash<int>{}(p.first) ^ std::hash<int>{}(p.second); } }; } std::unordered_map<std::pair<int, int>, int> cache{}; long long countPossibilities(const std::vector<Mine> &mines, int startPos, int endPos) { if (startPos > endPos) return 1; auto cachedPossibilities = cache.find(std::make_pair(startPos, endPos)); if (cachedPossibilities != cache.end()) return cachedPossibilities->second; long long result = countPossibilities(mines, mines[startPos].globalRightStriking + 1, endPos); long long notActivatedResult = 1; int startPosIter = startPos; while (mines[startPosIter].globallyNearestRightActivator != -1) { notActivatedResult *= countPossibilities(mines, startPosIter + 1, mines[startPosIter].globallyNearestRightActivator - 1); notActivatedResult %= DIVISOR; startPosIter = mines[startPosIter].globallyNearestRightActivator; } notActivatedResult *= countPossibilities(mines, startPosIter + 1, endPos); notActivatedResult %= DIVISOR; result += notActivatedResult; result %= DIVISOR; cache[std::make_pair(startPos, endPos)] = result; return result; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<Mine> mines; { mines.resize(n); std::vector<long long> positions, strikes; positions.resize(n); strikes.resize(n); for (int i = 0; i < n; ++i) { std::cin >> positions[i] >> strikes[i]; } for (int i = 0; i < n; ++i) { mines[i].directLeftStriking = std::distance(positions.begin(), std::lower_bound(positions.begin(), positions.end(), positions[i] - strikes[i])); mines[i].directRightStriking = std::distance(positions.begin(), std::upper_bound(positions.begin(), positions.end(), positions[i] + strikes[i])) - 1; } } { std::stack<int> rightActivatorsHelper; for (int i = 0; i < n; ++i) { while (not rightActivatorsHelper.empty()) { int top = rightActivatorsHelper.top(); if (top >= mines[i].directLeftStriking) { rightActivatorsHelper.pop(); mines[top].nearestRightActivator = i; } else break; } rightActivatorsHelper.push(i); } } { std::stack<int> leftActivatorsHelper; for (int i = n - 1; i > -1; --i) { while (not leftActivatorsHelper.empty()) { int top = leftActivatorsHelper.top(); if (top <= mines[i].directRightStriking) { leftActivatorsHelper.pop(); mines[top].nearestLeftActivator = i; } else break; } leftActivatorsHelper.push(i); } } { std::vector<int> rightStrikingOrdered; rightStrikingOrdered.reserve(n); for (int i = 0; i < n; ++i) { rightStrikingOrdered.push_back(i); } std::sort(rightStrikingOrdered.begin(), rightStrikingOrdered.end(), [&mines](int i1, int i2) { return mines[i1].directRightStriking < mines[i2].directRightStriking; }); for (int i = n - 1; i > -1; --i) { if (mines[i].globalRightStriking != -1) { continue; } mines[i].globalRightStriking = i; std::queue<int> bfsHelper; bfsHelper.push(i); while (not bfsHelper.empty()) { int top = bfsHelper.front(); bfsHelper.pop(); auto rightActivator = mines[top].nearestRightActivator; if (rightActivator != -1 and mines[rightActivator].globalRightStriking == -1) { mines[rightActivator].globalRightStriking = i; bfsHelper.push(rightActivator); } auto leftActivator = mines[top].nearestLeftActivator; if (leftActivator != -1 and mines[leftActivator].globalRightStriking == -1) { mines[leftActivator].globalRightStriking = i; bfsHelper.push(leftActivator); } } } } { std::vector<int> leftStrikingOrdered; leftStrikingOrdered.reserve(n); for (int i = 0; i < n; ++i) { leftStrikingOrdered.push_back(i); } std::sort(leftStrikingOrdered.begin(), leftStrikingOrdered.end(), [&mines](int i1, int i2) { return mines[i1].directLeftStriking < mines[i2].directLeftStriking; }); for (int i = 0; i < n; ++i) { if (mines[i].globalLeftStriking != -1) { continue; } mines[i].globalLeftStriking = i; std::queue<int> bfsHelper; bfsHelper.push(i); while (not bfsHelper.empty()) { int top = bfsHelper.front(); bfsHelper.pop(); auto rightActivator = mines[top].nearestRightActivator; if (rightActivator != -1 and mines[rightActivator].globalLeftStriking == -1) { mines[rightActivator].globalLeftStriking = i; bfsHelper.push(rightActivator); } auto leftActivator = mines[top].nearestLeftActivator; if (leftActivator != -1 and mines[leftActivator].globalLeftStriking == -1) { mines[leftActivator].globalLeftStriking = i; bfsHelper.push(leftActivator); } } } } { std::stack<int> rightActivatorsHelper; for (int i = 0; i < n; ++i) { while (not rightActivatorsHelper.empty()) { int top = rightActivatorsHelper.top(); if (top >= mines[i].globalLeftStriking) { rightActivatorsHelper.pop(); mines[top].globallyNearestRightActivator = i; } else break; } rightActivatorsHelper.push(i); } } { std::stack<int> leftActivatorsHelper; for (int i = n - 1; i > -1; --i) { while (not leftActivatorsHelper.empty()) { int top = leftActivatorsHelper.top(); if (top <= mines[i].globalRightStriking) { leftActivatorsHelper.pop(); mines[top].globallyNearestLeftActivator = i; } else break; } leftActivatorsHelper.push(i); } } std::vector<std::pair<int, int>> separatedGroups; int groupStart = 0, maxRelated = 0; for (int i = 0; i < n; ++i) { if (mines[i].globalRightStriking > maxRelated) maxRelated = mines[i].globalRightStriking; if (mines[i].globallyNearestRightActivator > maxRelated) maxRelated = mines[i].globallyNearestRightActivator; if (maxRelated == i) { separatedGroups.emplace_back(groupStart, i); groupStart = i + 1; } } long long result = 1; for (auto group : separatedGroups) { result *= countPossibilities(mines, group.first, group.second); result %= DIVISOR; cache.clear(); } std::cout << result << std::endl; return 0; } |