#include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; const long long INFINITY = 0x7fffffffffffffff; const int MAX_N = 300007; const int DEPTH = 19; const int LEAVES = 1 << DEPTH; inline int L(int v) {return (v << 1);} inline int R(int v) {return ((v << 1)|1);} inline int P(int v) {return (v >> 1);} long long a[MAX_N], r[MAX_N]; pair<long long, int> b[MAX_N]; long long leftTreeData[MAX_N]; int leftTree[2 << DEPTH]; long long rightTreeData[MAX_N]; int rightTree[2 << DEPTH]; long long e[MAX_N]; int maxETree[2 << DEPTH]; long long s[MAX_N]; int minSTree[2 << DEPTH]; inline int merge(long long treeData[], bool type, int x, int y) { return (((treeData[x] >= treeData[y]) == type) ? x : y); } int query(int tree[], bool type, long long treeData[], int ps, int pe, int vs, int ve, int v) { if(pe < vs || ve < ps) return MAX_N-1; if(ps <= vs && ve <= pe) return tree[v]; int split = (vs+ve)>>1; return merge(treeData, type, query(tree, type, treeData, ps, pe, vs, split, L(v)), query(tree, type, treeData, ps, pe, split+1, ve, R(v))); } inline int query(int tree[], bool type, long long treeData[], int ps, int pe) { if(pe < ps) return MAX_N-1; return query(tree, type, treeData, ps, pe, 0, LEAVES-1, 1); } void change(int tree[], bool type, long long treeData[], int i, long long val) { treeData[i] = val; i |= LEAVES; while(i = P(i)) { tree[i] = merge(treeData, type, tree[L(i)], tree[R(i)]); } } int FAU[MAX_N]; int fauFind(int x) { if(FAU[x] == x) return x; return FAU[x] = fauFind(FAU[x]); } vector<int> graf[MAX_N]; bool visited[MAX_N]; pair<int, int> sp[MAX_N]; vector<int> temp[MAX_N]; long long MOD = 1000000007; long long dp[MAX_N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 0; i < n; i++) { cin >> a[i] >> r[i]; b[i].first = r[i]; b[i].second = i; leftTreeData[i] = rightTreeData[i] = r[i]; leftTree[i|LEAVES] = rightTree[i|LEAVES] = minSTree[i|LEAVES] = maxETree[i|LEAVES] = i; FAU[i] = i; } for(int i = n; i < LEAVES; i++) { leftTreeData[i] = rightTreeData[i] = s[i] = e[i] = 1e9; leftTree[i|LEAVES] = rightTree[i|LEAVES] = minSTree[i|LEAVES] = maxETree[i|LEAVES] = i; } for(int i = 0; i < n; i++) { int low = -1, high = n, mid; while(low+1 < high) { mid = (low+high)>>1; if(a[mid] <= a[i]+r[i]) low = mid; else high = mid; } e[i] = low; low = -1, high = n; while(low+1 < high) { mid = (low+high)>>1; if(a[i]-r[i] <= a[mid]) high = mid; low = mid; } s[i] = high; } for(int i = LEAVES-1; i > 0; i--) { leftTree[i] = merge(leftTreeData, false, leftTree[L(i)], leftTree[R(i)]); rightTree[i] = merge(rightTreeData, false, rightTree[L(i)], rightTree[R(i)]); minSTree[i] = merge(s, false, minSTree[L(i)], minSTree[R(i)]); maxETree[i] = merge(e, true, maxETree[L(i)], maxETree[R(i)]); } sort(b, b+n); for(int i = 0; i < n; i++) { int x = query(minSTree, false, s, b[i].second+1, e[b[i].second]); if(x == MAX_N-1 || s[x] > b[i].second) { x = query(maxETree, true, e, s[b[i].second], b[i].second-1); } if(x == MAX_N-1 || e[x] < b[i].second) { int check = query(leftTree, false, leftTreeData, s[b[i].second], b[i].second-1); while(leftTreeData[check] < b[i].first) { graf[b[i].second].push_back(check); change(leftTree, false, leftTreeData, check, INFINITY); check = query(leftTree, false, leftTreeData, s[b[i].second], b[i].second-1); } check = query(rightTree, false, rightTreeData, b[i].second+1, e[b[i].second]); while(rightTreeData[check] < b[i].first) { graf[b[i].second].push_back(check); change(rightTree, false, rightTreeData, check, INFINITY); check = query(rightTree, false, rightTreeData, b[i].second+1, e[b[i].second]); } } else { int snew = min(s[x], s[b[i].second]); int enew = max(e[x], e[b[i].second]); change(minSTree, false, s, x, snew); change(maxETree, true, e, x, enew); change(minSTree, false, s, b[i].second, snew); change(maxETree, true, e, b[i].second, enew); FAU[b[i].second] = x; } } vector<pair<int, bool>> dfs; dfs.reserve(n); for(int i = 0; i < n; i++) { if(FAU[i] != i) continue; if(visited[i]) continue; dfs.clear(); dfs.push_back(make_pair(i, true)); while(dfs.size()) { pair<int, bool> x = dfs.back(); dfs.pop_back(); if(x.second) { if(visited[x.first]) continue; dfs.push_back(make_pair(x.first, false)); for(auto& c : graf[x.first]) { if(!visited[c]) { visited[c] = true; dfs.push_back(make_pair(fauFind(c), true)); } } } else { for(auto& c : graf[x.first]) { s[x.first] = min(s[x.first], s[fauFind(c)]); e[x.first] = max(e[x.first], e[fauFind(c)]); } } } } map<long long, int> scale; for(int i = 0; i < n; i++) { scale[s[i]] = 1; scale[e[i]] = 1; } int am = 0; for(map<long long, int>::iterator it = scale.begin(); it != scale.end(); it++) { it->second = am++; } int index = 0; for(int i = 0; i < n; i++) { if(FAU[i] == i) { sp[index] = make_pair(scale[s[i]], scale[e[i]]); index++; } } sort(sp, sp+index); for(int i = index-1; i >= 0; i--) { for(auto xd : temp[sp[i].second]) { temp[sp[i].first].push_back(xd); } temp[sp[i].first].push_back(sp[i].second); } for(int i = am; i < MAX_N; i++) { dp[i] = 1; } long long all = 0; for(int i = am-1; i >= 0; i--) { dp[i] = 0; for(auto xd : temp[i]) { dp[i] = (dp[i]+dp[xd+2])%MOD; } all = (all + dp[i]) % MOD; } cout << (all+1) << '\n'; 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 | #include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; const long long INFINITY = 0x7fffffffffffffff; const int MAX_N = 300007; const int DEPTH = 19; const int LEAVES = 1 << DEPTH; inline int L(int v) {return (v << 1);} inline int R(int v) {return ((v << 1)|1);} inline int P(int v) {return (v >> 1);} long long a[MAX_N], r[MAX_N]; pair<long long, int> b[MAX_N]; long long leftTreeData[MAX_N]; int leftTree[2 << DEPTH]; long long rightTreeData[MAX_N]; int rightTree[2 << DEPTH]; long long e[MAX_N]; int maxETree[2 << DEPTH]; long long s[MAX_N]; int minSTree[2 << DEPTH]; inline int merge(long long treeData[], bool type, int x, int y) { return (((treeData[x] >= treeData[y]) == type) ? x : y); } int query(int tree[], bool type, long long treeData[], int ps, int pe, int vs, int ve, int v) { if(pe < vs || ve < ps) return MAX_N-1; if(ps <= vs && ve <= pe) return tree[v]; int split = (vs+ve)>>1; return merge(treeData, type, query(tree, type, treeData, ps, pe, vs, split, L(v)), query(tree, type, treeData, ps, pe, split+1, ve, R(v))); } inline int query(int tree[], bool type, long long treeData[], int ps, int pe) { if(pe < ps) return MAX_N-1; return query(tree, type, treeData, ps, pe, 0, LEAVES-1, 1); } void change(int tree[], bool type, long long treeData[], int i, long long val) { treeData[i] = val; i |= LEAVES; while(i = P(i)) { tree[i] = merge(treeData, type, tree[L(i)], tree[R(i)]); } } int FAU[MAX_N]; int fauFind(int x) { if(FAU[x] == x) return x; return FAU[x] = fauFind(FAU[x]); } vector<int> graf[MAX_N]; bool visited[MAX_N]; pair<int, int> sp[MAX_N]; vector<int> temp[MAX_N]; long long MOD = 1000000007; long long dp[MAX_N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 0; i < n; i++) { cin >> a[i] >> r[i]; b[i].first = r[i]; b[i].second = i; leftTreeData[i] = rightTreeData[i] = r[i]; leftTree[i|LEAVES] = rightTree[i|LEAVES] = minSTree[i|LEAVES] = maxETree[i|LEAVES] = i; FAU[i] = i; } for(int i = n; i < LEAVES; i++) { leftTreeData[i] = rightTreeData[i] = s[i] = e[i] = 1e9; leftTree[i|LEAVES] = rightTree[i|LEAVES] = minSTree[i|LEAVES] = maxETree[i|LEAVES] = i; } for(int i = 0; i < n; i++) { int low = -1, high = n, mid; while(low+1 < high) { mid = (low+high)>>1; if(a[mid] <= a[i]+r[i]) low = mid; else high = mid; } e[i] = low; low = -1, high = n; while(low+1 < high) { mid = (low+high)>>1; if(a[i]-r[i] <= a[mid]) high = mid; low = mid; } s[i] = high; } for(int i = LEAVES-1; i > 0; i--) { leftTree[i] = merge(leftTreeData, false, leftTree[L(i)], leftTree[R(i)]); rightTree[i] = merge(rightTreeData, false, rightTree[L(i)], rightTree[R(i)]); minSTree[i] = merge(s, false, minSTree[L(i)], minSTree[R(i)]); maxETree[i] = merge(e, true, maxETree[L(i)], maxETree[R(i)]); } sort(b, b+n); for(int i = 0; i < n; i++) { int x = query(minSTree, false, s, b[i].second+1, e[b[i].second]); if(x == MAX_N-1 || s[x] > b[i].second) { x = query(maxETree, true, e, s[b[i].second], b[i].second-1); } if(x == MAX_N-1 || e[x] < b[i].second) { int check = query(leftTree, false, leftTreeData, s[b[i].second], b[i].second-1); while(leftTreeData[check] < b[i].first) { graf[b[i].second].push_back(check); change(leftTree, false, leftTreeData, check, INFINITY); check = query(leftTree, false, leftTreeData, s[b[i].second], b[i].second-1); } check = query(rightTree, false, rightTreeData, b[i].second+1, e[b[i].second]); while(rightTreeData[check] < b[i].first) { graf[b[i].second].push_back(check); change(rightTree, false, rightTreeData, check, INFINITY); check = query(rightTree, false, rightTreeData, b[i].second+1, e[b[i].second]); } } else { int snew = min(s[x], s[b[i].second]); int enew = max(e[x], e[b[i].second]); change(minSTree, false, s, x, snew); change(maxETree, true, e, x, enew); change(minSTree, false, s, b[i].second, snew); change(maxETree, true, e, b[i].second, enew); FAU[b[i].second] = x; } } vector<pair<int, bool>> dfs; dfs.reserve(n); for(int i = 0; i < n; i++) { if(FAU[i] != i) continue; if(visited[i]) continue; dfs.clear(); dfs.push_back(make_pair(i, true)); while(dfs.size()) { pair<int, bool> x = dfs.back(); dfs.pop_back(); if(x.second) { if(visited[x.first]) continue; dfs.push_back(make_pair(x.first, false)); for(auto& c : graf[x.first]) { if(!visited[c]) { visited[c] = true; dfs.push_back(make_pair(fauFind(c), true)); } } } else { for(auto& c : graf[x.first]) { s[x.first] = min(s[x.first], s[fauFind(c)]); e[x.first] = max(e[x.first], e[fauFind(c)]); } } } } map<long long, int> scale; for(int i = 0; i < n; i++) { scale[s[i]] = 1; scale[e[i]] = 1; } int am = 0; for(map<long long, int>::iterator it = scale.begin(); it != scale.end(); it++) { it->second = am++; } int index = 0; for(int i = 0; i < n; i++) { if(FAU[i] == i) { sp[index] = make_pair(scale[s[i]], scale[e[i]]); index++; } } sort(sp, sp+index); for(int i = index-1; i >= 0; i--) { for(auto xd : temp[sp[i].second]) { temp[sp[i].first].push_back(xd); } temp[sp[i].first].push_back(sp[i].second); } for(int i = am; i < MAX_N; i++) { dp[i] = 1; } long long all = 0; for(int i = am-1; i >= 0; i--) { dp[i] = 0; for(auto xd : temp[i]) { dp[i] = (dp[i]+dp[xd+2])%MOD; } all = (all + dp[i]) % MOD; } cout << (all+1) << '\n'; return 0; } |