#include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.ST << ", " << p.ND << ")"; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto &e : x) out << e << (&e == &*--x.end() ? "" : ", "); return out << '}'; } template<class... Args> void dump(Args&&... args) { ((cerr << args << "; "), ...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << "\n" #else # define debug(...) false #endif template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = (-mod <= v && v <= mod) ? v : v % mod; if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = (LL) val * m.val % mod; return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator /=(const modular &m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; // using mint = modular<int(998244353)>; struct Tree { using T = PII; vector<T> tree; T f(T a, T b) { return {min(a.ST, b.ST), max(a.ND, b.ND)}; } int size = 1; Tree(int n, T val) { while(size < n) size *= 2; tree.resize(size * 2, val); } void update(int pos, T val) { tree[pos += size] = val; while(pos /= 2) tree[pos] = f(tree[pos * 2], tree[pos * 2 + 1]); } T query(int l, int r) { l += size, r += size; T ret = (l != r ? f(tree[l], tree[r]) : tree[l]); while(l + 1 < r) { if(l % 2 == 0) ret = f(ret, tree[l + 1]); if(r % 2 == 1) ret = f(ret, tree[r - 1]); l /= 2, r /= 2; } return ret; } }; mt19937_64 rng(2137); LL rd(LL a) { return rng() % a; } struct Fenwick { using T = mint; vector<T> s; Fenwick(int n = 0) : s(n + 1) {} void update(int pos, T val) { for(pos++; pos > 0; pos -= pos & (-pos)) s[pos] += val; } void update(int l, int r, T val) { update(r, val); if(l != 0) update(l - 1, -val); } T query(int pos) { T ret = 0; for(pos++; pos < size(s); pos += pos & (-pos)) ret += s[pos]; return ret; } }; const int LOG = 20; struct Graph { vector<vector<int>> adj; vector<vector<PII>> jump; vector<int> pre, pos, &r; Fenwick tree; int cur_t = -1; void dfs(int v) { pre[v] = ++cur_t; for(int u : adj[v]) dfs(u); pos[v] = cur_t; } Graph(vector<int> &par, vector<int> &r, bool dir) : r(r) { int n = size(par); adj.resize(n); REP(i, n) if(par[i] != -1) adj[par[i]].emplace_back(i); pre = pos = vector<int>(n, -1); if(dir == 0) { REP(i, n) if(pre[i] == -1) dfs(i); } else { for(int i = n - 1; i >= 0; i--) if(pre[i] == -1) dfs(i); } tree = Fenwick(n); jump.resize(LOG, vector<PII>(n)); REP(i, n) jump[0][i] = {par[i], (par[i] == -1 ? -1 : r[par[i]])}; FOR(i, 1, LOG - 1) REP(j, n) { auto &[v, x] = jump[i - 1][j]; if(v == -1) jump[i][j] = jump[i - 1][j]; else { auto &[u, y] = jump[i - 1][v]; jump[i][j] = {u, max(x, y)}; } } } void update(int v, mint score) { tree.update(pre[v], pos[v], score); } int search(int v) { int p = v; for(int i = LOG - 1; i >= 0; i--) { auto &[u, w] = jump[i][p]; if(u != -1 && w < r[v]) p = u; } return jump[0][p].ST; } mint query(int v) { if(v == -1) return 0; return tree.query(pre[v]); } mint query_path(int v) { return query(v) - query(search(v)); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<LL> pos(n), range(n); REP(i, n) cin >> pos[i] >> range[i]; vector<int> l(n), r(n); Tree tree(n, {n, 0}); REP(i, n) { auto it = lower_bound(pos.begin(), pos.end(), pos[i] - range[i]); l[i] = (int) distance(pos.begin(), it); it = upper_bound(pos.begin(), pos.end(), pos[i] + range[i]); r[i] = (int) distance(pos.begin(), it) - 1; tree.update(i, {l[i], r[i]}); } bool improved = true; while(improved) { improved = false; REP(i, n) { PII old = {l[i], r[i]}; tie(l[i], r[i]) = tree.query(l[i], r[i]); tree.update(i, {l[i], r[i]}); if(PII(l[i], r[i]) != old) improved = true; } } auto get_prev = [&]() { vector<int> prev(n, -1); vector<PII> s; REP(i, n) { while(!s.empty() && s.back().ST >= l[i]) s.pop_back(); prev[i] = (s.empty() ? -1 : s.back().ND); s.emplace_back(l[i], i); } return prev; }; auto prev = get_prev(); reverse(l.begin(), l.end()); auto next = get_prev(); reverse(l.begin(), l.end()); reverse(next.begin(), next.end()); REP(i, n) if(next[i] != -1) next[i] = n - 1 - next[i]; Graph pr(prev, r, 0), ne(next, r, 1); vector<vector<int>> Ls(n + 2), Rs(n); REP(i, n) { Ls[l[i]].emplace_back(i); Rs[r[i]].emplace_back(i); } auto add_l = [&](int L, mint val) { for(int i : Ls[L]) { pr.update(i, val);; ne.update(i, val); } }; add_l(0, 1), add_l(1, 1); vector<mint> dp(n); vector<bool> bylo(n); REP(i, n) { dp[i] = (i == 0 ? 1 : dp[i - 1]); for(int j : Rs[i]) { if(bylo[l[j]]) continue; bylo[l[j]] = true; dp[i] += pr.query_path(j); dp[i] += ne.query_path(j); dp[i] -= (l[j] >= 2 ? dp[l[j] - 2] : 1); } for(int j : Rs[i]) bylo[l[j]] = false; add_l(i + 2, dp[i]); } cout << dp.back() << "\n"; }
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #include<bits/stdc++.h> using namespace std; #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.ST << ", " << p.ND << ")"; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto &e : x) out << e << (&e == &*--x.end() ? "" : ", "); return out << '}'; } template<class... Args> void dump(Args&&... args) { ((cerr << args << "; "), ...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << "\n" #else # define debug(...) false #endif template<class T> int size(T && a) { return (int) a.size(); } using LL = long long; using PII = pair<int, int>; template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = (-mod <= v && v <= mod) ? v : v % mod; if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = (LL) val * m.val % mod; return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator /=(const modular &m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; // using mint = modular<int(998244353)>; struct Tree { using T = PII; vector<T> tree; T f(T a, T b) { return {min(a.ST, b.ST), max(a.ND, b.ND)}; } int size = 1; Tree(int n, T val) { while(size < n) size *= 2; tree.resize(size * 2, val); } void update(int pos, T val) { tree[pos += size] = val; while(pos /= 2) tree[pos] = f(tree[pos * 2], tree[pos * 2 + 1]); } T query(int l, int r) { l += size, r += size; T ret = (l != r ? f(tree[l], tree[r]) : tree[l]); while(l + 1 < r) { if(l % 2 == 0) ret = f(ret, tree[l + 1]); if(r % 2 == 1) ret = f(ret, tree[r - 1]); l /= 2, r /= 2; } return ret; } }; mt19937_64 rng(2137); LL rd(LL a) { return rng() % a; } struct Fenwick { using T = mint; vector<T> s; Fenwick(int n = 0) : s(n + 1) {} void update(int pos, T val) { for(pos++; pos > 0; pos -= pos & (-pos)) s[pos] += val; } void update(int l, int r, T val) { update(r, val); if(l != 0) update(l - 1, -val); } T query(int pos) { T ret = 0; for(pos++; pos < size(s); pos += pos & (-pos)) ret += s[pos]; return ret; } }; const int LOG = 20; struct Graph { vector<vector<int>> adj; vector<vector<PII>> jump; vector<int> pre, pos, &r; Fenwick tree; int cur_t = -1; void dfs(int v) { pre[v] = ++cur_t; for(int u : adj[v]) dfs(u); pos[v] = cur_t; } Graph(vector<int> &par, vector<int> &r, bool dir) : r(r) { int n = size(par); adj.resize(n); REP(i, n) if(par[i] != -1) adj[par[i]].emplace_back(i); pre = pos = vector<int>(n, -1); if(dir == 0) { REP(i, n) if(pre[i] == -1) dfs(i); } else { for(int i = n - 1; i >= 0; i--) if(pre[i] == -1) dfs(i); } tree = Fenwick(n); jump.resize(LOG, vector<PII>(n)); REP(i, n) jump[0][i] = {par[i], (par[i] == -1 ? -1 : r[par[i]])}; FOR(i, 1, LOG - 1) REP(j, n) { auto &[v, x] = jump[i - 1][j]; if(v == -1) jump[i][j] = jump[i - 1][j]; else { auto &[u, y] = jump[i - 1][v]; jump[i][j] = {u, max(x, y)}; } } } void update(int v, mint score) { tree.update(pre[v], pos[v], score); } int search(int v) { int p = v; for(int i = LOG - 1; i >= 0; i--) { auto &[u, w] = jump[i][p]; if(u != -1 && w < r[v]) p = u; } return jump[0][p].ST; } mint query(int v) { if(v == -1) return 0; return tree.query(pre[v]); } mint query_path(int v) { return query(v) - query(search(v)); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<LL> pos(n), range(n); REP(i, n) cin >> pos[i] >> range[i]; vector<int> l(n), r(n); Tree tree(n, {n, 0}); REP(i, n) { auto it = lower_bound(pos.begin(), pos.end(), pos[i] - range[i]); l[i] = (int) distance(pos.begin(), it); it = upper_bound(pos.begin(), pos.end(), pos[i] + range[i]); r[i] = (int) distance(pos.begin(), it) - 1; tree.update(i, {l[i], r[i]}); } bool improved = true; while(improved) { improved = false; REP(i, n) { PII old = {l[i], r[i]}; tie(l[i], r[i]) = tree.query(l[i], r[i]); tree.update(i, {l[i], r[i]}); if(PII(l[i], r[i]) != old) improved = true; } } auto get_prev = [&]() { vector<int> prev(n, -1); vector<PII> s; REP(i, n) { while(!s.empty() && s.back().ST >= l[i]) s.pop_back(); prev[i] = (s.empty() ? -1 : s.back().ND); s.emplace_back(l[i], i); } return prev; }; auto prev = get_prev(); reverse(l.begin(), l.end()); auto next = get_prev(); reverse(l.begin(), l.end()); reverse(next.begin(), next.end()); REP(i, n) if(next[i] != -1) next[i] = n - 1 - next[i]; Graph pr(prev, r, 0), ne(next, r, 1); vector<vector<int>> Ls(n + 2), Rs(n); REP(i, n) { Ls[l[i]].emplace_back(i); Rs[r[i]].emplace_back(i); } auto add_l = [&](int L, mint val) { for(int i : Ls[L]) { pr.update(i, val);; ne.update(i, val); } }; add_l(0, 1), add_l(1, 1); vector<mint> dp(n); vector<bool> bylo(n); REP(i, n) { dp[i] = (i == 0 ? 1 : dp[i - 1]); for(int j : Rs[i]) { if(bylo[l[j]]) continue; bylo[l[j]] = true; dp[i] += pr.query_path(j); dp[i] += ne.query_path(j); dp[i] -= (l[j] >= 2 ? dp[l[j] - 2] : 1); } for(int j : Rs[i]) bylo[l[j]] = false; add_l(i + 2, dp[i]); } cout << dp.back() << "\n"; } |