#include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int mod = 1e9 + 7, nax = 300*1000+10, INF = 1e9; int n; ll pos[nax], r[nax]; pi prz[nax]; int dp[nax], dp2[nax], R; int T[(1 << 20)]; void upd(int a, int x) { a += R; T[a] = x; while(a > 1) { a/=2; T[a] = (T[2*a]+ T[2*a+1]) % mod; } } int qr(int a, int b) { if(a > b) return 0; a += R; b += R; int w = T[a]; if(a != b) w = (w + T[b]) % mod; while(a/2!=b/2) { if(a%2==0) w = (w + T[a+1]) % mod; if(b%2==1) w = (w + T[b-1]) % mod; a/=2; b/=2; } return w; } vi active; vector<pi>border; int main() {_ cin >> n; for(int i = 1; i <= n; ++i) { cin >> pos[i] >> r[i]; } for(int i = 1; i <= n; ++i) { int low = i, high = n, mid; while(low <= high) { mid = (low + high) / 2; if(pos[mid] - pos[i] <= r[i]) { low = mid + 1; } else { high = mid - 1; } } int b = low - 1; low = 1; high = i; while(low <= high) { mid = (low + high)/2; if(pos[i] - pos[mid] <= r[i]) { high = mid - 1; } else { low = mid + 1; } } int a = high + 1; prz[i] = {a, b}; } R = 1; while(R <= n) R *= 2; dp[0] = dp2[0] = 1; int ans = 1; vector<pi>lft = {}; for(int i = 1; i <= n; ++i) { while((int)border.size() > 0 && border.back().ND <= i) { border.pop_back(); } if(prz[i].ND > i) { border.emplace_back(i, prz[i].ND); } int up_to = 0; if((int)border.size() > 0) { up_to = border.back().ST + 1; } while((int)lft.size() > 0 && lft.back().ST > prz[i].ST) { lft.pop_back(); } int d = 1; if((int)lft.size()>0) d = lft.back().ND+1; lft.emplace_back(prz[i].ST, i); while((int)active.size() > 0 && active.back() >= d) { upd(active.back(), 0); active.pop_back(); } if(d <= prz[i].ST) { upd(prz[i].ST, (prz[i].ST == 1 ? 1 : dp2[prz[i].ST - 2])); active.PB(prz[i].ST); } int w = qr(up_to, i); dp[i] = w; dp2[i] = (dp[i] + dp2[i - 1]) % mod; ans = (ans + dp[i]) % mod; } cout << ans; }
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 | #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int mod = 1e9 + 7, nax = 300*1000+10, INF = 1e9; int n; ll pos[nax], r[nax]; pi prz[nax]; int dp[nax], dp2[nax], R; int T[(1 << 20)]; void upd(int a, int x) { a += R; T[a] = x; while(a > 1) { a/=2; T[a] = (T[2*a]+ T[2*a+1]) % mod; } } int qr(int a, int b) { if(a > b) return 0; a += R; b += R; int w = T[a]; if(a != b) w = (w + T[b]) % mod; while(a/2!=b/2) { if(a%2==0) w = (w + T[a+1]) % mod; if(b%2==1) w = (w + T[b-1]) % mod; a/=2; b/=2; } return w; } vi active; vector<pi>border; int main() {_ cin >> n; for(int i = 1; i <= n; ++i) { cin >> pos[i] >> r[i]; } for(int i = 1; i <= n; ++i) { int low = i, high = n, mid; while(low <= high) { mid = (low + high) / 2; if(pos[mid] - pos[i] <= r[i]) { low = mid + 1; } else { high = mid - 1; } } int b = low - 1; low = 1; high = i; while(low <= high) { mid = (low + high)/2; if(pos[i] - pos[mid] <= r[i]) { high = mid - 1; } else { low = mid + 1; } } int a = high + 1; prz[i] = {a, b}; } R = 1; while(R <= n) R *= 2; dp[0] = dp2[0] = 1; int ans = 1; vector<pi>lft = {}; for(int i = 1; i <= n; ++i) { while((int)border.size() > 0 && border.back().ND <= i) { border.pop_back(); } if(prz[i].ND > i) { border.emplace_back(i, prz[i].ND); } int up_to = 0; if((int)border.size() > 0) { up_to = border.back().ST + 1; } while((int)lft.size() > 0 && lft.back().ST > prz[i].ST) { lft.pop_back(); } int d = 1; if((int)lft.size()>0) d = lft.back().ND+1; lft.emplace_back(prz[i].ST, i); while((int)active.size() > 0 && active.back() >= d) { upd(active.back(), 0); active.pop_back(); } if(d <= prz[i].ST) { upd(prz[i].ST, (prz[i].ST == 1 ? 1 : dp2[prz[i].ST - 2])); active.PB(prz[i].ST); } int w = qr(up_to, i); dp[i] = w; dp2[i] = (dp[i] + dp2[i - 1]) % mod; ans = (ans + dp[i]) % mod; } cout << ans; } |