#include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } template<class T, class U, class K> ostream &operator<<(ostream &os, map<T,U, K> V){ os<<"[";for(pair<T,U> vv:V)os<<vv<<",";return os<<"]"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif struct Mine{ int mid, range; bool operator<(const Mine & other) const{ return mid < other.mid; } }; struct fu{ VI f; fu(int n){ f.resize(n); REP(i, n){ f[i] = i; } } int fi(int x){ return f[x] == x? x : f[x] = fi(f[x]); } void un(int x, int y){ f[fi(x)] = fi(y); } }; const int mod = 1e9 + 7; void add_m(int & a, int b){ a += b; if(a >= mod){ a -= mod; } } void rem(int & a, int b){ a -= b; if(a < 0){ a += mod; } } struct Dyn{ map<int,int, greater<int>> m; int total = 0; void add(int k, int v){ add_m(m[k], v); add_m(total, v); } void remove_top(int a){ while(!m.empty() && m.begin()->st >= a){ int v = m.begin()->nd; rem(total, v); m.erase(m.begin()); } } void merge(Dyn & other){ if(m.size() < other.m.size()){ swap(m, other.m); swap(total, other.total); } for(const auto & p: other.m){ add(p.st, p.nd); } other.m = {}; } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int n; cin >> n; vector<Mine> mines; REP(i, n){ Mine m; cin >> m.mid >> m.range; mines.PB(m); } vector<PII> idx(n); REP(i, n){ Mine m = mines[i]; auto it_left = lower_bound(ALL(mines), Mine{m.mid - m.range, 0}); auto it_right = prev(upper_bound(ALL(mines), Mine{m.mid + m.range, 0})); idx[i] = {it_left - mines.begin(), it_right - mines.begin()}; } debug(idx); vector<PII> stack; REP(i, n){ debug(stack); while(!stack.empty() && stack.back().st >= idx[i].st){ debug("A"); mini(idx[i].st, stack.back().nd); stack.pop_back(); } stack.PB({i, idx[i].st}); } stack.clear(); FORD(i, n - 1, 0){ while(!stack.empty() && stack.back().st <= idx[i].nd){ maxi(idx[i].nd, stack.back().nd); stack.pop_back(); } stack.PB({i, idx[i].nd}); } debug(idx); vector<vector<int>> endpoints(n); REP(i, n){ endpoints[idx[i].st].PB(i); } set<int> active_indices; fu f(n); REP(i, n){ for(int a: endpoints[i]){ active_indices.insert(a); } active_indices.erase(i); if(!active_indices.empty()){ int active = *active_indices.begin(); if(idx[i].nd >= active){ f.un(i, active); maxi(idx[active].nd, idx[i].nd); } } } endpoints = vector<VI>(n); active_indices = {}; REP(i, n){ endpoints[idx[i].nd].PB(i); } FORD(i, n - 1, 0){ for(int a: endpoints[i]){ active_indices.insert(a); } active_indices.erase(i); if(!active_indices.empty()){ int active = *active_indices.rbegin(); if(idx[i].st <= active){ f.un(i, active); mini(idx[active].st, idx[i].st); } } } endpoints = {}; active_indices = {}; REP(i, n){ int parent = f.fi(i); mini(idx[parent].st, idx[i].st); maxi(idx[parent].nd, idx[i].nd); } REP(i, n){ int parent = f.fi(i); mini(idx[i].st, idx[parent].st); maxi(idx[i].nd, idx[parent].nd); } debug(idx); vector<Dyn> dyn(n + 1); dyn[0].add(-1, 1); REP(i, n){ debug(i, dyn[i].m); // nie detonujemy dyn[i + 1].add(i, dyn[i].total); // akcja - detonacja dyn[i].remove_top(idx[i].st); dyn[idx[i].nd + 1].merge(dyn[i]); } cout << dyn[n].total << "\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 | #include "bits/stdc++.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } template<class T, class U, class K> ostream &operator<<(ostream &os, map<T,U, K> V){ os<<"[";for(pair<T,U> vv:V)os<<vv<<",";return os<<"]"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif struct Mine{ int mid, range; bool operator<(const Mine & other) const{ return mid < other.mid; } }; struct fu{ VI f; fu(int n){ f.resize(n); REP(i, n){ f[i] = i; } } int fi(int x){ return f[x] == x? x : f[x] = fi(f[x]); } void un(int x, int y){ f[fi(x)] = fi(y); } }; const int mod = 1e9 + 7; void add_m(int & a, int b){ a += b; if(a >= mod){ a -= mod; } } void rem(int & a, int b){ a -= b; if(a < 0){ a += mod; } } struct Dyn{ map<int,int, greater<int>> m; int total = 0; void add(int k, int v){ add_m(m[k], v); add_m(total, v); } void remove_top(int a){ while(!m.empty() && m.begin()->st >= a){ int v = m.begin()->nd; rem(total, v); m.erase(m.begin()); } } void merge(Dyn & other){ if(m.size() < other.m.size()){ swap(m, other.m); swap(total, other.total); } for(const auto & p: other.m){ add(p.st, p.nd); } other.m = {}; } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int n; cin >> n; vector<Mine> mines; REP(i, n){ Mine m; cin >> m.mid >> m.range; mines.PB(m); } vector<PII> idx(n); REP(i, n){ Mine m = mines[i]; auto it_left = lower_bound(ALL(mines), Mine{m.mid - m.range, 0}); auto it_right = prev(upper_bound(ALL(mines), Mine{m.mid + m.range, 0})); idx[i] = {it_left - mines.begin(), it_right - mines.begin()}; } debug(idx); vector<PII> stack; REP(i, n){ debug(stack); while(!stack.empty() && stack.back().st >= idx[i].st){ debug("A"); mini(idx[i].st, stack.back().nd); stack.pop_back(); } stack.PB({i, idx[i].st}); } stack.clear(); FORD(i, n - 1, 0){ while(!stack.empty() && stack.back().st <= idx[i].nd){ maxi(idx[i].nd, stack.back().nd); stack.pop_back(); } stack.PB({i, idx[i].nd}); } debug(idx); vector<vector<int>> endpoints(n); REP(i, n){ endpoints[idx[i].st].PB(i); } set<int> active_indices; fu f(n); REP(i, n){ for(int a: endpoints[i]){ active_indices.insert(a); } active_indices.erase(i); if(!active_indices.empty()){ int active = *active_indices.begin(); if(idx[i].nd >= active){ f.un(i, active); maxi(idx[active].nd, idx[i].nd); } } } endpoints = vector<VI>(n); active_indices = {}; REP(i, n){ endpoints[idx[i].nd].PB(i); } FORD(i, n - 1, 0){ for(int a: endpoints[i]){ active_indices.insert(a); } active_indices.erase(i); if(!active_indices.empty()){ int active = *active_indices.rbegin(); if(idx[i].st <= active){ f.un(i, active); mini(idx[active].st, idx[i].st); } } } endpoints = {}; active_indices = {}; REP(i, n){ int parent = f.fi(i); mini(idx[parent].st, idx[i].st); maxi(idx[parent].nd, idx[i].nd); } REP(i, n){ int parent = f.fi(i); mini(idx[i].st, idx[parent].st); maxi(idx[i].nd, idx[parent].nd); } debug(idx); vector<Dyn> dyn(n + 1); dyn[0].add(-1, 1); REP(i, n){ debug(i, dyn[i].m); // nie detonujemy dyn[i + 1].add(i, dyn[i].total); // akcja - detonacja dyn[i].remove_top(idx[i].st); dyn[idx[i].nd + 1].merge(dyn[i]); } cout << dyn[n].total << "\n"; } |