#include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <deque> #include <algorithm> #include <map> using namespace std; const int MAXN = 500000; const long long MOD = 1000000007; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long x[MAXN]; int a[MAXN]; int b[MAXN]; int ap[MAXN]; int bp[MAXN]; int id[MAXN]; int idweight[MAXN]; long long r[MAXN]; long long P[MAXN+1]; typedef pair<int, int> para; int deg[MAXN]; vector<int> L[MAXN]; int getid(int i) { if (i == -1) return -1; if (id[i] == i) return i; else return id[i] = getid(id[i]); } int mergeid(int i, int j) { // cerr << "Merge " << i << " "<< j << endl; i = getid(i); j = getid(j); if(i == j) return i; if (idweight[i] < idweight[j]) swap(i, j); idweight[i] += idweight[j] + 1; return id[j] = i; } struct Tree { vector<vector<long long> > V; void init(int size) { V.push_back(vector<long long>()); V.back().resize(size); if (size > 1) init(size >> 1); } // suma przed pos long long sum(int pos, int lvl = 0) { // cerr << "SUM " << pos << "/" << lvl << endl; // assert(lvl < V.size()); // assert(pos <= V[lvl].size()); if (pos == 0 || lvl >= V.size()) return 0; long long total = (pos & 1) ? V[lvl][pos-1] : 0; total += sum(pos >> 1, lvl + 1); return total % MOD; } void add(int pos, long long val, int lvl = 0) { if (lvl >= V.size() || pos >= V[lvl].size()) return; // cerr << "ADD " << pos << "/" << lvl << " = " << val << " >> " << V.size() << endl; add(pos >> 1, val, lvl+1); V[lvl][pos] += val; V[lvl][pos] %= MOD; } }; bool sorter(const para&p, const para&q) { if(p.first != q.first) return p.first < q.first; return p.second > q.second; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> x[i] >> r[i]; } FOR(i, n) id[i] = i; vector<int> S; FOR(i, n) { a[i] = lower_bound(x, x+i, x[i]-r[i]) - x; b[i] = upper_bound(x+1, x+n, x[i] + r[i]) - x - 1; } FOR(i, n) { while(!S.empty() && b[S.back()] < i) { S.pop_back(); } ap[i] = S.empty() ? -1 : S.back(); while(!S.empty() && b[S.back()] <= b[i]) { S.pop_back(); } S.push_back(i); } S.clear(); for (int i = n-1; i >= 0; i--) { while(!S.empty() && a[S.back()] > i) { S.pop_back(); } bp[i] = S.empty() ? -1 : S.back(); while(!S.empty() && a[S.back()] >= a[i]) { S.pop_back(); } S.push_back(i); } S.clear(); // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } // int idx = 0; FOR(ig, n) { int k = getid(ig); while (1) { // cerr << ig << endl; // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } int i = -1, j = -1; if(getid(bp[k]) != -1 && getid(ap[getid(bp[k])]) == k) { i = k; j = getid(bp[k]); } if(getid(ap[k]) != -1 && getid(bp[getid(ap[k])]) == k) { i = getid(ap[k]); j = k; } if(i == -1) break; bp[i] = bp[j]; ap[j] = ap[i]; a[i] = a[j] = min(a[i], a[j]); b[i] = b[j] = max(b[i], b[j]); k = mergeid(i, j); } } FOR(i, n) if(getid(i) != i) { int j = getid(i); a[j] = min(a[j], a[i]); b[j] = max(b[j], b[i]); } // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } FOR (i, n) { // cerr << "> " << i << " " << id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; if(ap[i] != -1 && getid(ap[i]) != getid(i)) { L[getid(i)].push_back(getid(ap[i])); deg[getid(ap[i])]++; // cerr << getid(i) << "->" << getid(ap[i]) << endl; // V.push_back(para(getid(ap[i]), getid(i))); } if(bp[i] != -1 && getid(bp[i]) != getid(i)) { L[getid(i)].push_back(getid(bp[i])); deg[getid(bp[i])]++; // cerr << getid(i) << "->" << getid(bp[i]) << endl; // V.push_back(para(getid(bp[i]), getid(i))); } } FOR(i, n) if (getid(i) == i) { if(deg[i] == 0) { S.push_back(i); } } else { int idx = getid(i); // a[idx] = min(a[idx], a[i]); // b[idx] = max(b[idx], b[i]); } for (int idx = 0; idx<S.size(); idx++) { int i = S[idx]; FOR(jdx, L[i].size()) { int j = L[i][jdx]; deg[j]--; if(deg[j] == 0) { S.push_back(j); } } } FOR(idx, S.size()) { // cerr << "}" << S[idx] << endl; int i = S[idx]; FOR(jdx, L[i].size()) { int j = L[i][jdx]; a[j] = min(a[j], a[i]); b[j] = max(b[j], b[i]); } } // FOR (i, n) if (getid(i) == i) { // cerr << "> " << i << " " << id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } vector<pair<int, int> > V; FOR(i, n) if(i == getid(i)) { V.push_back(pair<int, int>(a[i]+1, b[i]+1)); } sort(V.begin(), V.end(), sorter); // V.resize(unique(V.begin(), V.end()) - V.begin()); FOR(i, V.size()) { a[i] = V[i].first; b[i] = V[i].second; } int maxB = n+1; // FOR(i, V.size()) { // cerr << a[i] << " <> " << b[i] << endl; // } Tree T; T.init(maxB); T.add(0, 1); long long total = 1; int head = 0; FOR(i, V.size()) { long long sum = T.sum(b[i]); P[i] = sum; // assert(sum < MOD && sum >= 0); T.add(b[i], sum); total = (total + sum) %MOD; } cout << total << endl; 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 | #include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <deque> #include <algorithm> #include <map> using namespace std; const int MAXN = 500000; const long long MOD = 1000000007; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long x[MAXN]; int a[MAXN]; int b[MAXN]; int ap[MAXN]; int bp[MAXN]; int id[MAXN]; int idweight[MAXN]; long long r[MAXN]; long long P[MAXN+1]; typedef pair<int, int> para; int deg[MAXN]; vector<int> L[MAXN]; int getid(int i) { if (i == -1) return -1; if (id[i] == i) return i; else return id[i] = getid(id[i]); } int mergeid(int i, int j) { // cerr << "Merge " << i << " "<< j << endl; i = getid(i); j = getid(j); if(i == j) return i; if (idweight[i] < idweight[j]) swap(i, j); idweight[i] += idweight[j] + 1; return id[j] = i; } struct Tree { vector<vector<long long> > V; void init(int size) { V.push_back(vector<long long>()); V.back().resize(size); if (size > 1) init(size >> 1); } // suma przed pos long long sum(int pos, int lvl = 0) { // cerr << "SUM " << pos << "/" << lvl << endl; // assert(lvl < V.size()); // assert(pos <= V[lvl].size()); if (pos == 0 || lvl >= V.size()) return 0; long long total = (pos & 1) ? V[lvl][pos-1] : 0; total += sum(pos >> 1, lvl + 1); return total % MOD; } void add(int pos, long long val, int lvl = 0) { if (lvl >= V.size() || pos >= V[lvl].size()) return; // cerr << "ADD " << pos << "/" << lvl << " = " << val << " >> " << V.size() << endl; add(pos >> 1, val, lvl+1); V[lvl][pos] += val; V[lvl][pos] %= MOD; } }; bool sorter(const para&p, const para&q) { if(p.first != q.first) return p.first < q.first; return p.second > q.second; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> x[i] >> r[i]; } FOR(i, n) id[i] = i; vector<int> S; FOR(i, n) { a[i] = lower_bound(x, x+i, x[i]-r[i]) - x; b[i] = upper_bound(x+1, x+n, x[i] + r[i]) - x - 1; } FOR(i, n) { while(!S.empty() && b[S.back()] < i) { S.pop_back(); } ap[i] = S.empty() ? -1 : S.back(); while(!S.empty() && b[S.back()] <= b[i]) { S.pop_back(); } S.push_back(i); } S.clear(); for (int i = n-1; i >= 0; i--) { while(!S.empty() && a[S.back()] > i) { S.pop_back(); } bp[i] = S.empty() ? -1 : S.back(); while(!S.empty() && a[S.back()] >= a[i]) { S.pop_back(); } S.push_back(i); } S.clear(); // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } // int idx = 0; FOR(ig, n) { int k = getid(ig); while (1) { // cerr << ig << endl; // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } int i = -1, j = -1; if(getid(bp[k]) != -1 && getid(ap[getid(bp[k])]) == k) { i = k; j = getid(bp[k]); } if(getid(ap[k]) != -1 && getid(bp[getid(ap[k])]) == k) { i = getid(ap[k]); j = k; } if(i == -1) break; bp[i] = bp[j]; ap[j] = ap[i]; a[i] = a[j] = min(a[i], a[j]); b[i] = b[j] = max(b[i], b[j]); k = mergeid(i, j); } } FOR(i, n) if(getid(i) != i) { int j = getid(i); a[j] = min(a[j], a[i]); b[j] = max(b[j], b[i]); } // FOR(i, n) { // cerr << "> " << i << " " << x[i] << "-" << r[i] << " "<< id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } FOR (i, n) { // cerr << "> " << i << " " << id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; if(ap[i] != -1 && getid(ap[i]) != getid(i)) { L[getid(i)].push_back(getid(ap[i])); deg[getid(ap[i])]++; // cerr << getid(i) << "->" << getid(ap[i]) << endl; // V.push_back(para(getid(ap[i]), getid(i))); } if(bp[i] != -1 && getid(bp[i]) != getid(i)) { L[getid(i)].push_back(getid(bp[i])); deg[getid(bp[i])]++; // cerr << getid(i) << "->" << getid(bp[i]) << endl; // V.push_back(para(getid(bp[i]), getid(i))); } } FOR(i, n) if (getid(i) == i) { if(deg[i] == 0) { S.push_back(i); } } else { int idx = getid(i); // a[idx] = min(a[idx], a[i]); // b[idx] = max(b[idx], b[i]); } for (int idx = 0; idx<S.size(); idx++) { int i = S[idx]; FOR(jdx, L[i].size()) { int j = L[i][jdx]; deg[j]--; if(deg[j] == 0) { S.push_back(j); } } } FOR(idx, S.size()) { // cerr << "}" << S[idx] << endl; int i = S[idx]; FOR(jdx, L[i].size()) { int j = L[i][jdx]; a[j] = min(a[j], a[i]); b[j] = max(b[j], b[i]); } } // FOR (i, n) if (getid(i) == i) { // cerr << "> " << i << " " << id[i] << " :: " << a[i] << " " << b[i] << " :: " << ap[i] << " " << bp[i] << endl; // } vector<pair<int, int> > V; FOR(i, n) if(i == getid(i)) { V.push_back(pair<int, int>(a[i]+1, b[i]+1)); } sort(V.begin(), V.end(), sorter); // V.resize(unique(V.begin(), V.end()) - V.begin()); FOR(i, V.size()) { a[i] = V[i].first; b[i] = V[i].second; } int maxB = n+1; // FOR(i, V.size()) { // cerr << a[i] << " <> " << b[i] << endl; // } Tree T; T.init(maxB); T.add(0, 1); long long total = 1; int head = 0; FOR(i, V.size()) { long long sum = T.sum(b[i]); P[i] = sum; // assert(sum < MOD && sum >= 0); T.add(b[i], sum); total = (total + sum) %MOD; } cout << total << endl; return 0; } |