#include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vll vector<ll> #define vvi vector<vi> #define vvll vector<vll> #define vvvll vector<vvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; ll INF = LONG_LONG_MAX; ll mod = 1000000000 + 7; template <typename T, typename L> void read(vector<T> & _data, L & _size, bool _shift) { _data.resize(_size + (ll)_shift); for (ll i = (ll)_shift; i < _size + (ll)_shift; i++) cin >> _data[i]; } template <typename T, typename L> void read(vector<vector<T>> & _data, L & _rows, L & _cols, bool _shiftRows, bool _shiftCols) { _data.resize(_rows + (ll)_shiftRows); for (ll i = 0; i < _rows + (ll)_shiftRows; i++) _data[i].resize(_cols + (ll)_shiftCols); for (ll i = (ll)_shiftRows; i < _rows + (ll)_shiftRows; i++) for (ll j = (ll)_shiftCols; j < _cols + (ll)_shiftCols; j++) cin >> _data[i][j]; } template <typename T> void write(vector<T> & _data, bool _shift) { for (ll i = (ll)_shift; i < _data.size(); i++) cout << _data[i] << " "; cout << endl; } //TODO: SOLUTION ll n, m; vll trgen; vll A, R; vpll S; void mktrgen(ll init) { m = 1; while (m < n + 1) m *= 2; trgen = vll(2 * m, init); } void updmin(ll x, ll y) { x += m; trgen[x] = y; x /= 2; while (x > 0) { trgen[x] = min(trgen[2 * x], trgen[2 * x + 1]); x /= 2; } } void updmax(ll x, ll y) { x += m; trgen[x] = y; x /= 2; while (x > 0) { trgen[x] = max(trgen[2 * x], trgen[2 * x + 1]); x /= 2; } } void updsum(ll x, ll y) { x += m; trgen[x] = y; trgen[x] = trgen[x] % mod; x /= 2; while (x > 0) { trgen[x] = trgen[2 * x] + trgen[2 * x + 1]; trgen[x] = trgen[x] % mod; x /= 2; } } ll getmin(ll l, ll r) { l += m; r += m; ll mini = INF; while (l <= r) { if (l % 2 == 1) { mini = min(mini, trgen[l]); l++; } if (r % 2 == 0) { mini = min(mini, trgen[r]); r--; } l /= 2; r /= 2; } return mini; } ll getmax(ll l, ll r) { l += m; r += m; ll mini = 0; while (l <= r) { if (l % 2 == 1) { mini = max(mini, trgen[l]); l++; } if (r % 2 == 0) { mini = max(mini, trgen[r]); r--; } l /= 2; r /= 2; } return mini; } ll getsum(ll l, ll r) { l += m; r += m; ll mini = 0; while (l <= r) { if (l % 2 == 1) { mini += trgen[l]; mini = mini % mod; l++; } if (r % 2 == 0) { mini += trgen[r]; mini = mini % mod; r--; } l /= 2; r /= 2; } mini = mini % mod; return mini; } void solve() { cin >> n; A = vll(n + 1); R = vll(n + 1); for (int i = 1; i <= n; i++) { cin >> A[i] >> R[i]; } A[0] = -INF; R[0] = 0; S = vpll(n); for (int i = 1; i <= n; i++) { S[i-1] = {R[i], i}; } sort(S.begin(), S.end(), greater<>()); vi nxt(n + 1), prv(n + 1); vi lbl(n + 1), lbr(n + 1); mktrgen(INF); for (int i = 1; i <= n; i++) { updmin(i, i); int k = lower_bound(A.begin(), A.end(), A[i] - R[i]) - A.begin(); prv[i] = getmin(k, i); updmin(i, prv[i]); lbl[i] = k; } for (int i = 0; i < n; i++) { int j = S[i].second; int r = R[j]; int a = A[j]; int k1 = lbl[j]; int k2 = lower_bound(A.begin(), A.end(), A[j] + R[j] + 1) - A.begin() - 1; prv[j] = getmin(k1, k2); updmin(j, prv[j]); lbr[j] = k2; } mktrgen(0); for (int i = n; i >= 1; i--) { updmax(i, i); int k = lbr[i]; nxt[i] = getmax(i, k); updmax(i, nxt[i]); } for (int i = 0; i < n; i++) { int j = S[i].second; int r = R[j]; int a = A[j]; int k1 = lbl[j]; int k2 = lbr[j]; nxt[j] = getmax(k1, k2); updmax(j, nxt[j]); } mktrgen(0); vll dp(n + 1, 0); dp[0] = 1; updsum(0, 1); for (int i = 1; i <= n; i++) { ll x = dp[prv[i] - 1]; ll y = getsum(0, i - 1); ll z = getsum(nxt[i] + 1, n); dp[i] = x + y; dp[i] = dp[i] % mod; updsum(nxt[i], x - z); } cout << dp[n] << endl; } int main() { ios::sync_with_stdio(false); // TODO: Set value of this variable manually bool _multipleTestCases = false; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { solve(); } 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 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vll vector<ll> #define vvi vector<vi> #define vvll vector<vll> #define vvvll vector<vvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; ll INF = LONG_LONG_MAX; ll mod = 1000000000 + 7; template <typename T, typename L> void read(vector<T> & _data, L & _size, bool _shift) { _data.resize(_size + (ll)_shift); for (ll i = (ll)_shift; i < _size + (ll)_shift; i++) cin >> _data[i]; } template <typename T, typename L> void read(vector<vector<T>> & _data, L & _rows, L & _cols, bool _shiftRows, bool _shiftCols) { _data.resize(_rows + (ll)_shiftRows); for (ll i = 0; i < _rows + (ll)_shiftRows; i++) _data[i].resize(_cols + (ll)_shiftCols); for (ll i = (ll)_shiftRows; i < _rows + (ll)_shiftRows; i++) for (ll j = (ll)_shiftCols; j < _cols + (ll)_shiftCols; j++) cin >> _data[i][j]; } template <typename T> void write(vector<T> & _data, bool _shift) { for (ll i = (ll)_shift; i < _data.size(); i++) cout << _data[i] << " "; cout << endl; } //TODO: SOLUTION ll n, m; vll trgen; vll A, R; vpll S; void mktrgen(ll init) { m = 1; while (m < n + 1) m *= 2; trgen = vll(2 * m, init); } void updmin(ll x, ll y) { x += m; trgen[x] = y; x /= 2; while (x > 0) { trgen[x] = min(trgen[2 * x], trgen[2 * x + 1]); x /= 2; } } void updmax(ll x, ll y) { x += m; trgen[x] = y; x /= 2; while (x > 0) { trgen[x] = max(trgen[2 * x], trgen[2 * x + 1]); x /= 2; } } void updsum(ll x, ll y) { x += m; trgen[x] = y; trgen[x] = trgen[x] % mod; x /= 2; while (x > 0) { trgen[x] = trgen[2 * x] + trgen[2 * x + 1]; trgen[x] = trgen[x] % mod; x /= 2; } } ll getmin(ll l, ll r) { l += m; r += m; ll mini = INF; while (l <= r) { if (l % 2 == 1) { mini = min(mini, trgen[l]); l++; } if (r % 2 == 0) { mini = min(mini, trgen[r]); r--; } l /= 2; r /= 2; } return mini; } ll getmax(ll l, ll r) { l += m; r += m; ll mini = 0; while (l <= r) { if (l % 2 == 1) { mini = max(mini, trgen[l]); l++; } if (r % 2 == 0) { mini = max(mini, trgen[r]); r--; } l /= 2; r /= 2; } return mini; } ll getsum(ll l, ll r) { l += m; r += m; ll mini = 0; while (l <= r) { if (l % 2 == 1) { mini += trgen[l]; mini = mini % mod; l++; } if (r % 2 == 0) { mini += trgen[r]; mini = mini % mod; r--; } l /= 2; r /= 2; } mini = mini % mod; return mini; } void solve() { cin >> n; A = vll(n + 1); R = vll(n + 1); for (int i = 1; i <= n; i++) { cin >> A[i] >> R[i]; } A[0] = -INF; R[0] = 0; S = vpll(n); for (int i = 1; i <= n; i++) { S[i-1] = {R[i], i}; } sort(S.begin(), S.end(), greater<>()); vi nxt(n + 1), prv(n + 1); vi lbl(n + 1), lbr(n + 1); mktrgen(INF); for (int i = 1; i <= n; i++) { updmin(i, i); int k = lower_bound(A.begin(), A.end(), A[i] - R[i]) - A.begin(); prv[i] = getmin(k, i); updmin(i, prv[i]); lbl[i] = k; } for (int i = 0; i < n; i++) { int j = S[i].second; int r = R[j]; int a = A[j]; int k1 = lbl[j]; int k2 = lower_bound(A.begin(), A.end(), A[j] + R[j] + 1) - A.begin() - 1; prv[j] = getmin(k1, k2); updmin(j, prv[j]); lbr[j] = k2; } mktrgen(0); for (int i = n; i >= 1; i--) { updmax(i, i); int k = lbr[i]; nxt[i] = getmax(i, k); updmax(i, nxt[i]); } for (int i = 0; i < n; i++) { int j = S[i].second; int r = R[j]; int a = A[j]; int k1 = lbl[j]; int k2 = lbr[j]; nxt[j] = getmax(k1, k2); updmax(j, nxt[j]); } mktrgen(0); vll dp(n + 1, 0); dp[0] = 1; updsum(0, 1); for (int i = 1; i <= n; i++) { ll x = dp[prv[i] - 1]; ll y = getsum(0, i - 1); ll z = getsum(nxt[i] + 1, n); dp[i] = x + y; dp[i] = dp[i] % mod; updsum(nxt[i], x - z); } cout << dp[n] << endl; } int main() { ios::sync_with_stdio(false); // TODO: Set value of this variable manually bool _multipleTestCases = false; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { solve(); } return 0; } |