#include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Pll = pair<ll, ll>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const ll inf = 4e18; const int mod = 1e9 + 7; struct MinTree { int base = 1; vector<int> mini; MinTree(int n) { while (base < n + 2) base *= 2; mini.resize(2*base, base); } void insert(int a, int c) { a += base; mini[a] = min(mini[a], c); a /= 2; while (a >= 1) { mini[a] = min(mini[2*a], mini[2*a+1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = min(mini[a], mini[b]); while (a/2 != b/2) { if (a % 2 == 0) res = min(res, mini[a+1]); if (b % 2 == 1) res = min(res, mini[b-1]); a /= 2; b /= 2; } return res; } }; struct MaxTree { int base = 1; vector<int> maxi; MaxTree(int n) { while (base < n + 2) base *= 2; maxi.resize(2*base); } void insert(int a, int c) { a += base; maxi[a] = max(maxi[a], c); a /= 2; while (a >= 1) { maxi[a] = max(maxi[2*a], maxi[2*a+1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = max(maxi[a], maxi[b]); while (a/2 != b/2) { if (a % 2 == 0) res = max(res, maxi[a+1]); if (b % 2 == 1) res = max(res, maxi[b-1]); a /= 2; b /= 2; } return res; } }; struct Tree { int base = 1; vector<int> sum, lazy; Tree(int n) { while (base < n + 2) base *= 2; sum.resize(2*base); lazy.resize(2*base, -1); } void apply(int a, int len, int val) { sum[a] = int(((ll)len * val) % mod); lazy[a] = val; } void push(int a, int len) { if (lazy[a] != -1) { apply(2*a, len/2, lazy[a]); apply(2*a+1, len/2, lazy[a]); lazy[a] = -1; } } void update(int a) { sum[a] = (sum[2*a] + sum[2*a + 1]) % mod; } void setVal(int a, int nl, int nr, int ql, int qr, int val) { if (nr < ql || qr < nl) return; if (ql <= nl && nr <= qr) { apply(a, nr - nl + 1, val); return; } int mid = (nl + nr) / 2; push(a, nr - nl + 1); setVal(2*a, nl, mid, ql, qr, val); setVal(2*a+1, mid+1, nr, ql, qr, val); update(a); } void set(int a, int b, int val) { setVal(1, 0, base-1, a, b, val); } int countSum(int a, int nl, int nr, int ql, int qr) { if (nr < ql || qr < nl) return 0; if (ql <= nl && nr <= qr) { return sum[a]; } int mid = (nl + nr) / 2; push(a, nr - nl + 1); int res = countSum(2*a, nl, mid, ql, qr); res += countSum(2*a+1, mid+1, nr, ql, qr); res %= mod; update(a); return res; } int query(int a, int b) { return countSum(1, 0, base-1, a, b); } }; struct Stack { stack<Pii> S; void insert(Pii p) { while (!S.empty() && S.top() < p) { S.pop(); } S.push(p); } int get(int i) { while (!S.empty() && S.top().first <= i) { S.pop(); } if (S.empty()) { return 0; } else { return S.top().y; } } }; int n; vector<Pii> getBasicIntervals(const vector<Pll> & V) { vector<Pii> intervals(sz(V)); for (int i = 1; i <= n; i++) { ll x = V[i].x, r = V[i].y; intervals[i].x = int(lower_bound(all(V), Pll(x-r, -1)) - V.begin()); intervals[i].y = int(lower_bound(all(V), Pll(x+r+1, -1)) - V.begin()) - 1; } return intervals; } vector<Pii> getIntervals(const vector<Pll> & V) { vector<Pii> intervals = getBasicIntervals(V); MinTree tree1(n+2); MaxTree tree2(n+2); for (int i = 1; i <= n; i++) { intervals[i].x = min(intervals[i].x, tree1.query(intervals[i].x, i)); tree1.insert(i, intervals[i].x); } for (int i = n ; i >= 1; i--) { intervals[i].y = max(intervals[i].y, tree2.query(i, intervals[i].y)); tree2.insert(i, intervals[i].y); } return intervals; } void solve() { cin >> n; vector<pair<ll,ll>> IN; IN.push_back({-inf, 0}); for (int i = 0; i < n; i++) { ll x, r; cin >> x >> r; IN.push_back({x, r}); } IN.push_back({inf, 0}); vector<Pii> V = getIntervals(IN); vector<int> dp(n+1); dp[0] = 1; Tree tree(n+2); tree.set(0, 1, dp[0]); Stack S; for (int i = 1; i <= n; i++) { int l = V[i].x, r = V[i].y; if (l <= i-1) { tree.set(l, i-1, 0); } S.insert({r, i}); int x = S.get(i); dp[i] = tree.query(x, i); tree.set(i+1, i+1, dp[i]); } cout << dp[n] << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } }
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 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Pll = pair<ll, ll>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const ll inf = 4e18; const int mod = 1e9 + 7; struct MinTree { int base = 1; vector<int> mini; MinTree(int n) { while (base < n + 2) base *= 2; mini.resize(2*base, base); } void insert(int a, int c) { a += base; mini[a] = min(mini[a], c); a /= 2; while (a >= 1) { mini[a] = min(mini[2*a], mini[2*a+1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = min(mini[a], mini[b]); while (a/2 != b/2) { if (a % 2 == 0) res = min(res, mini[a+1]); if (b % 2 == 1) res = min(res, mini[b-1]); a /= 2; b /= 2; } return res; } }; struct MaxTree { int base = 1; vector<int> maxi; MaxTree(int n) { while (base < n + 2) base *= 2; maxi.resize(2*base); } void insert(int a, int c) { a += base; maxi[a] = max(maxi[a], c); a /= 2; while (a >= 1) { maxi[a] = max(maxi[2*a], maxi[2*a+1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = max(maxi[a], maxi[b]); while (a/2 != b/2) { if (a % 2 == 0) res = max(res, maxi[a+1]); if (b % 2 == 1) res = max(res, maxi[b-1]); a /= 2; b /= 2; } return res; } }; struct Tree { int base = 1; vector<int> sum, lazy; Tree(int n) { while (base < n + 2) base *= 2; sum.resize(2*base); lazy.resize(2*base, -1); } void apply(int a, int len, int val) { sum[a] = int(((ll)len * val) % mod); lazy[a] = val; } void push(int a, int len) { if (lazy[a] != -1) { apply(2*a, len/2, lazy[a]); apply(2*a+1, len/2, lazy[a]); lazy[a] = -1; } } void update(int a) { sum[a] = (sum[2*a] + sum[2*a + 1]) % mod; } void setVal(int a, int nl, int nr, int ql, int qr, int val) { if (nr < ql || qr < nl) return; if (ql <= nl && nr <= qr) { apply(a, nr - nl + 1, val); return; } int mid = (nl + nr) / 2; push(a, nr - nl + 1); setVal(2*a, nl, mid, ql, qr, val); setVal(2*a+1, mid+1, nr, ql, qr, val); update(a); } void set(int a, int b, int val) { setVal(1, 0, base-1, a, b, val); } int countSum(int a, int nl, int nr, int ql, int qr) { if (nr < ql || qr < nl) return 0; if (ql <= nl && nr <= qr) { return sum[a]; } int mid = (nl + nr) / 2; push(a, nr - nl + 1); int res = countSum(2*a, nl, mid, ql, qr); res += countSum(2*a+1, mid+1, nr, ql, qr); res %= mod; update(a); return res; } int query(int a, int b) { return countSum(1, 0, base-1, a, b); } }; struct Stack { stack<Pii> S; void insert(Pii p) { while (!S.empty() && S.top() < p) { S.pop(); } S.push(p); } int get(int i) { while (!S.empty() && S.top().first <= i) { S.pop(); } if (S.empty()) { return 0; } else { return S.top().y; } } }; int n; vector<Pii> getBasicIntervals(const vector<Pll> & V) { vector<Pii> intervals(sz(V)); for (int i = 1; i <= n; i++) { ll x = V[i].x, r = V[i].y; intervals[i].x = int(lower_bound(all(V), Pll(x-r, -1)) - V.begin()); intervals[i].y = int(lower_bound(all(V), Pll(x+r+1, -1)) - V.begin()) - 1; } return intervals; } vector<Pii> getIntervals(const vector<Pll> & V) { vector<Pii> intervals = getBasicIntervals(V); MinTree tree1(n+2); MaxTree tree2(n+2); for (int i = 1; i <= n; i++) { intervals[i].x = min(intervals[i].x, tree1.query(intervals[i].x, i)); tree1.insert(i, intervals[i].x); } for (int i = n ; i >= 1; i--) { intervals[i].y = max(intervals[i].y, tree2.query(i, intervals[i].y)); tree2.insert(i, intervals[i].y); } return intervals; } void solve() { cin >> n; vector<pair<ll,ll>> IN; IN.push_back({-inf, 0}); for (int i = 0; i < n; i++) { ll x, r; cin >> x >> r; IN.push_back({x, r}); } IN.push_back({inf, 0}); vector<Pii> V = getIntervals(IN); vector<int> dp(n+1); dp[0] = 1; Tree tree(n+2); tree.set(0, 1, dp[0]); Stack S; for (int i = 1; i <= n; i++) { int l = V[i].x, r = V[i].y; if (l <= i-1) { tree.set(l, i-1, 0); } S.insert({r, i}); int x = S.get(i); dp[i] = tree.query(x, i); tree.set(i+1, i+1, dp[i]); } cout << dp[n] << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } } |