#include <iostream> #include <vector> #include <queue> using namespace std; struct Circle { unsigned long long a; unsigned long long r; Circle (unsigned long long a0, unsigned long long r0) { a = a0; r = r0; } }; vector<Circle> circles; vector<unsigned long long> solutionsWith; vector<unsigned long long> solutionsWithout; const unsigned long long p = 1000000007; vector<int> nearestLeftReaching; vector<vector<int>> reachingTmp; priority_queue<int> lefts; int binSearch(int start, int end, unsigned long long val) { if (start == end) { return start; } int mid = (start + end + 1) / 2; if (circles[mid].a <= val) { return binSearch(mid, end, val); } else { return binSearch(start, mid - 1, val); } } void calculateNearestLeftReaching() { reachingTmp.resize(circles.size()); for (unsigned i = 0; i < circles.size(); ++i) { unsigned j = binSearch(i, circles.size() - 1, circles[i].a + circles[i].r); if (j > i) { reachingTmp[j].push_back(i); } } for (int i = circles.size() - 1; i >= 0; --i) { while (!reachingTmp[i].empty()) { lefts.push(reachingTmp[i].back()); reachingTmp[i].pop_back(); } if (!lefts.empty() && lefts.top() == i) { lefts.pop(); } if (lefts.empty()) { nearestLeftReaching[i] = -1; } else { nearestLeftReaching[i] = lefts.top(); } } } int getNearestLeftReachingBrute(int pos) { for (int i = pos - 1; i >= 0; --i) { if (circles[pos].a - circles[i].a <= circles[i].r) { return i; } } return -1; } int getNearestLeftReaching(int pos) { return nearestLeftReaching[pos]; } int getFurthestLeft(int pos) { unsigned long long leftDist; if (circles[pos].a < circles[pos].r) { return -1; } else { leftDist = circles[pos].a - circles[pos].r; } for (int i = pos - 1; i >= 0; --i) { if (circles[i].a < leftDist) { return i; } if (circles[i].a < circles[i].r) { return -1; } else if (leftDist > circles[i].a - circles[i].r) { leftDist = circles[i].a - circles[i].r; } } return -1; } int main() { ios_base::sync_with_stdio(0); int n; unsigned long long a, r; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; cin >> r; circles.push_back(Circle(a, r)); solutionsWith.push_back(0); solutionsWithout.push_back(0); nearestLeftReaching.push_back(-1); } calculateNearestLeftReaching(); solutionsWith[0] = 1; solutionsWithout[0] = 1; for (int i = 1; i < n; ++i) { int j = getFurthestLeft(i); if (j < 0) { solutionsWith[i] = 1; } else { solutionsWith[i] = (solutionsWith[j] + solutionsWithout[j]) % p; } int k = getNearestLeftReaching(i); if (k < 0) { solutionsWithout[i] = (solutionsWith[i - 1] + solutionsWithout[i - 1]) % p; } else { solutionsWithout[i] = (p + solutionsWith[i - 1] + solutionsWithout[i - 1] - solutionsWith[k]) % p; } } cout << (solutionsWith[n - 1] + solutionsWithout[n - 1]) % p << endl; /* cout << endl << endl; for (int i = 0; i < n; ++i) { cout << getNearestLeftReaching(i) << " "; } cout << 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 | #include <iostream> #include <vector> #include <queue> using namespace std; struct Circle { unsigned long long a; unsigned long long r; Circle (unsigned long long a0, unsigned long long r0) { a = a0; r = r0; } }; vector<Circle> circles; vector<unsigned long long> solutionsWith; vector<unsigned long long> solutionsWithout; const unsigned long long p = 1000000007; vector<int> nearestLeftReaching; vector<vector<int>> reachingTmp; priority_queue<int> lefts; int binSearch(int start, int end, unsigned long long val) { if (start == end) { return start; } int mid = (start + end + 1) / 2; if (circles[mid].a <= val) { return binSearch(mid, end, val); } else { return binSearch(start, mid - 1, val); } } void calculateNearestLeftReaching() { reachingTmp.resize(circles.size()); for (unsigned i = 0; i < circles.size(); ++i) { unsigned j = binSearch(i, circles.size() - 1, circles[i].a + circles[i].r); if (j > i) { reachingTmp[j].push_back(i); } } for (int i = circles.size() - 1; i >= 0; --i) { while (!reachingTmp[i].empty()) { lefts.push(reachingTmp[i].back()); reachingTmp[i].pop_back(); } if (!lefts.empty() && lefts.top() == i) { lefts.pop(); } if (lefts.empty()) { nearestLeftReaching[i] = -1; } else { nearestLeftReaching[i] = lefts.top(); } } } int getNearestLeftReachingBrute(int pos) { for (int i = pos - 1; i >= 0; --i) { if (circles[pos].a - circles[i].a <= circles[i].r) { return i; } } return -1; } int getNearestLeftReaching(int pos) { return nearestLeftReaching[pos]; } int getFurthestLeft(int pos) { unsigned long long leftDist; if (circles[pos].a < circles[pos].r) { return -1; } else { leftDist = circles[pos].a - circles[pos].r; } for (int i = pos - 1; i >= 0; --i) { if (circles[i].a < leftDist) { return i; } if (circles[i].a < circles[i].r) { return -1; } else if (leftDist > circles[i].a - circles[i].r) { leftDist = circles[i].a - circles[i].r; } } return -1; } int main() { ios_base::sync_with_stdio(0); int n; unsigned long long a, r; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; cin >> r; circles.push_back(Circle(a, r)); solutionsWith.push_back(0); solutionsWithout.push_back(0); nearestLeftReaching.push_back(-1); } calculateNearestLeftReaching(); solutionsWith[0] = 1; solutionsWithout[0] = 1; for (int i = 1; i < n; ++i) { int j = getFurthestLeft(i); if (j < 0) { solutionsWith[i] = 1; } else { solutionsWith[i] = (solutionsWith[j] + solutionsWithout[j]) % p; } int k = getNearestLeftReaching(i); if (k < 0) { solutionsWithout[i] = (solutionsWith[i - 1] + solutionsWithout[i - 1]) % p; } else { solutionsWithout[i] = (p + solutionsWith[i - 1] + solutionsWithout[i - 1] - solutionsWith[k]) % p; } } cout << (solutionsWith[n - 1] + solutionsWithout[n - 1]) % p << endl; /* cout << endl << endl; for (int i = 0; i < n; ++i) { cout << getNearestLeftReaching(i) << " "; } cout << endl;*/ return 0; } |