#include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define sz(x) (int)x.size() #define cat(x) cerr << #x << " = " << x << endl #define IOS cin.tie(0); ios_base::sync_with_stdio(0) using ll = long long; using namespace std; const int MOD = 1e9 + 7; const int N = 1 << 19; const int LOG = 19; int add(int a, int b) { if (a + b < 0) return a + b + MOD; if (a + b >= MOD) return a + b - MOD; return a + b; } int n, L[N], dp[N], pref[N], F[N]; ll a[N], r[N], MAX[N][LOG], MIN[N][LOG]; vector<pair<int, int>> vec[N]; ll Max(int l, int r) { int k = L[r - l]; return max(MAX[l][k], MAX[r - (1 << k)][k]); } ll Min(int l, int r) { int k = L[r - l]; return min(MIN[l][k], MIN[r - (1 << k)][k]); } void upd(int x, int delta) { for (x++; x < N; x += x & -x) F[x] = add(F[x], delta); } int prefix(int x) { int res = 0; for (x++; 0 < x; x -= x & -x) res = add(res, F[x]); return res; } int sum(int l, int r) { return add(prefix(r), -prefix(l - 1)); } int main() { for (int i = 2; i < N; ++i) L[i] = L[i / 2] + 1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld%lld", a + i, r + i); MAX[i][0] = a[i] + r[i]; MIN[i][0] = a[i] - r[i]; } for (int j = 0; j + 1 < LOG; ++j) for (int i = 1; i <= n - (1 << j); ++i) { MAX[i][j + 1] = max(MAX[i][j], MAX[i + (1 << j)][j]); MIN[i][j + 1] = min(MIN[i][j], MIN[i + (1 << j)][j]); } a[n + 1] = 3e18; pref[0] = 1; for (int i = 1; i <= n; ++i) { upd(i - 1, pref[i - 1]); pref[i] = add(pref[i - 1], dp[i - 1]); for (auto [x, delta] : vec[i]) upd(x, delta); int l = i + 1, r = n + 1; while (l < r) { int m = l + r >> 1; if (Min(i + 1, m + 1) <= a[i]) r = m; else l = m + 1; } vec[l].pb({i, -pref[i]}); l = 1, r = i + 1; while (l < r) { int m = (l + r) >> 1; if (Max(m, i + 1) >= a[i + 1]) l = m + 1; else r = m; } dp[i] = sum(l - 1, i); } printf("%d\n", add(pref[n], dp[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 | #include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define sz(x) (int)x.size() #define cat(x) cerr << #x << " = " << x << endl #define IOS cin.tie(0); ios_base::sync_with_stdio(0) using ll = long long; using namespace std; const int MOD = 1e9 + 7; const int N = 1 << 19; const int LOG = 19; int add(int a, int b) { if (a + b < 0) return a + b + MOD; if (a + b >= MOD) return a + b - MOD; return a + b; } int n, L[N], dp[N], pref[N], F[N]; ll a[N], r[N], MAX[N][LOG], MIN[N][LOG]; vector<pair<int, int>> vec[N]; ll Max(int l, int r) { int k = L[r - l]; return max(MAX[l][k], MAX[r - (1 << k)][k]); } ll Min(int l, int r) { int k = L[r - l]; return min(MIN[l][k], MIN[r - (1 << k)][k]); } void upd(int x, int delta) { for (x++; x < N; x += x & -x) F[x] = add(F[x], delta); } int prefix(int x) { int res = 0; for (x++; 0 < x; x -= x & -x) res = add(res, F[x]); return res; } int sum(int l, int r) { return add(prefix(r), -prefix(l - 1)); } int main() { for (int i = 2; i < N; ++i) L[i] = L[i / 2] + 1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld%lld", a + i, r + i); MAX[i][0] = a[i] + r[i]; MIN[i][0] = a[i] - r[i]; } for (int j = 0; j + 1 < LOG; ++j) for (int i = 1; i <= n - (1 << j); ++i) { MAX[i][j + 1] = max(MAX[i][j], MAX[i + (1 << j)][j]); MIN[i][j + 1] = min(MIN[i][j], MIN[i + (1 << j)][j]); } a[n + 1] = 3e18; pref[0] = 1; for (int i = 1; i <= n; ++i) { upd(i - 1, pref[i - 1]); pref[i] = add(pref[i - 1], dp[i - 1]); for (auto [x, delta] : vec[i]) upd(x, delta); int l = i + 1, r = n + 1; while (l < r) { int m = l + r >> 1; if (Min(i + 1, m + 1) <= a[i]) r = m; else l = m + 1; } vec[l].pb({i, -pref[i]}); l = 1, r = i + 1; while (l < r) { int m = (l + r) >> 1; if (Max(m, i + 1) >= a[i + 1]) l = m + 1; else r = m; } dp[i] = sum(l - 1, i); } printf("%d\n", add(pref[n], dp[n])); return 0; } |