#include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define MOD 1000000007 #define INFTY 2000000000 int main() { int i, j, n; vector<II> v_center, v_right; cin >> n; for (i=0; i<n; i++) { int a,r; cin >> a >> r; v_center.push_back(II(a,r)); v_right.push_back(II(a+r,r)); } sort(v_right.begin(),v_right.end()); vector<int> t_center(n,0), t_right(n,0); t_center[0] = t_right[0] = 2; // pusty i {0}. int i_right = 1; for (int i_center=1; i_center<n; i_center++) { while (i_right < n && v_right[i_right].first < v_center[i_center].first) { auto lower = lower_bound (v_right.begin(), v_right.end(), II(v_right[i_right].first-v_right[i_right].second, -INFTY)); j = distance(v_right.begin(),lower) - 1; if (j >= 0) t_right[i_right] += t_right[j]; else t_right[i_right] += 1; t_right[i_right] %= MOD; lower = lower_bound (v_center.begin(), v_center.end(), II(v_right[i_right].first-2*v_right[i_right].second, -INFTY)); j = distance(v_center.begin(),lower) - 1; if (j >= 0) t_right[i_right] += t_center[j]; else t_right[i_right] += 1; t_right[i_right] %= MOD; i_right++; } auto lower = lower_bound (v_right.begin(), v_right.end(), II(v_center[i_center].first, -INFTY)); j = distance(v_right.begin(),lower) - 1; if (j >= 0) t_center[i_center] += t_right[j]; else t_center[i_center] += 1; t_center[i_center] %= MOD; lower = lower_bound (v_center.begin(), v_center.end(), II(v_center[i_center].first-v_center[i_center].second, -INFTY)); j = distance(v_center.begin(),lower) - 1; if (j >= 0) t_center[i_center] += t_center[j]; else t_center[i_center] += 1; t_center[i_center] %= MOD; } cout << t_center[n-1] << "\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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define MOD 1000000007 #define INFTY 2000000000 int main() { int i, j, n; vector<II> v_center, v_right; cin >> n; for (i=0; i<n; i++) { int a,r; cin >> a >> r; v_center.push_back(II(a,r)); v_right.push_back(II(a+r,r)); } sort(v_right.begin(),v_right.end()); vector<int> t_center(n,0), t_right(n,0); t_center[0] = t_right[0] = 2; // pusty i {0}. int i_right = 1; for (int i_center=1; i_center<n; i_center++) { while (i_right < n && v_right[i_right].first < v_center[i_center].first) { auto lower = lower_bound (v_right.begin(), v_right.end(), II(v_right[i_right].first-v_right[i_right].second, -INFTY)); j = distance(v_right.begin(),lower) - 1; if (j >= 0) t_right[i_right] += t_right[j]; else t_right[i_right] += 1; t_right[i_right] %= MOD; lower = lower_bound (v_center.begin(), v_center.end(), II(v_right[i_right].first-2*v_right[i_right].second, -INFTY)); j = distance(v_center.begin(),lower) - 1; if (j >= 0) t_right[i_right] += t_center[j]; else t_right[i_right] += 1; t_right[i_right] %= MOD; i_right++; } auto lower = lower_bound (v_right.begin(), v_right.end(), II(v_center[i_center].first, -INFTY)); j = distance(v_right.begin(),lower) - 1; if (j >= 0) t_center[i_center] += t_right[j]; else t_center[i_center] += 1; t_center[i_center] %= MOD; lower = lower_bound (v_center.begin(), v_center.end(), II(v_center[i_center].first-v_center[i_center].second, -INFTY)); j = distance(v_center.begin(),lower) - 1; if (j >= 0) t_center[i_center] += t_center[j]; else t_center[i_center] += 1; t_center[i_center] %= MOD; } cout << t_center[n-1] << "\n"; return 0; } |