#include <cstdio> #include <vector> #include <set> const int max_n = 500100; int n; long long a[max_n]; long long r[max_n]; int top; std::pair<long long, int> stack[max_n]; std::vector<int> G[max_n]; std::vector<int> current; std::vector<std::vector<int>> solution; int D[max_n], S[max_n], InS[max_n], C[max_n]; int ile, tt; int delta[max_n], eta[max_n]; int best; int m; std::vector<int> DAG[max_n]; int DMN[max_n], DMX[max_n]; int dvis[max_n]; const int mod = 1000000007; void dfsX(int v) { dvis[v] = 1; int mx = DMN[v], mn = DMX[v]; for (int i = 0; i < DAG[v].size(); ++i) { if (dvis[DAG[v][i]] == 0) { dfsX(DAG[v][i]); } DMN[v] = std::min(DMN[v], DMN[DAG[v][i]]); DMX[v] = std::max(DMX[v], DMX[DAG[v][i]]); } } void dfs(int v) { S[top++] = v; InS[v] = 1; delta[v] = eta[v] = ++tt; D[v] = 1; for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] == 0) { dfs(G[v][i]); eta[v] = std::min(eta[v], eta[G[v][i]]); } else if (delta[G[v][i]] < delta[v] && InS[G[v][i]]) eta[v] = std::min(eta[v], delta[G[v][i]]); } if (delta[v] == eta[v]) { ++ile; current.clear(); while (top > 0 && delta[v] <= delta[S[top - 1]]) { --top; InS[S[top]] = 0; C[S[top]] = ile; current.push_back(S[top] + 1); } solution.push_back(current); } } int lmSCCTarjanAlgo() { for (int i = 0; i < n; ++i) D[i] = InS[i] = 0; top = ile = tt = 0; for (int i = 0; i < n; ++i) if (!D[i]) dfs(i); return ile; } const int czapa = 524288; long long tree[2 * czapa + 1]; std::vector<std::pair<int, long long>> pending[max_n]; void add(int index, long long val) { index += czapa; while (index >= 1) { tree[index] += val; index >>= 1; } } long long sum(int index) { index += czapa; long long ret = 0; while (index > 1) { if (index & 1) ret += tree[index - 1]; index >>= 1; } return ret % mod; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld %lld", &a[i], &r[i]); } top = 1; stack[0].first = a[0] + r[0]; stack[0].second = 0; for (int i = 1; i < n; ++i) { while (top > 0 && stack[top - 1].first < a[i]) --top; if (top > 0) { G[stack[top - 1].second].push_back(i); } while (top > 0 && stack[top - 1].first <= a[i] + r[i]) --top; stack[top].first = a[i] + r[i]; stack[top].second = i; ++top; } top = 1; stack[0].first = a[n - 1] - r[n - 1]; stack[0].second = n - 1; for (int i = n - 2; i >= 0; --i) { while (top > 0 && stack[top - 1].first > a[i]) --top; if (top > 0) { G[stack[top - 1].second].push_back(i); } while (top > 0 && stack[top - 1].first >= a[i] - r[i]) --top; stack[top].first = a[i] - r[i]; stack[top].second = i; ++top; } // for (int i = 0; i < n; ++i) { // printf("%d:", i); // for (int j = 0; j < G[i].size(); ++j) { // printf (" %d", G[i][j]); // } // printf("\n"); // } lmSCCTarjanAlgo(); // for (int i = 0; i < n; ++i) { // printf("C: %d: %d\n", i, C[i]); // } m = ile; for (int i = 0; i < n; ++i) { for (int j = 0; j < G[i].size(); ++j) { if (C[i] != C[G[i][j]]) DAG[C[i] - 1].push_back(C[G[i][j]] - 1); } } for (int i = 0; i < m; ++i) DMX[i] = -1, DMN[i] = 1000000000, dvis[i] = 0; for (int i = 0; i < n; ++i) { DMX[C[i] - 1] = std::max(DMX[C[i] - 1], i); DMN[C[i] - 1] = std::min(DMN[C[i] - 1], i); } // for (int i = 0; i < n; ++i) { // printf("V: %d: %d %d\n", i, DMN[C[i] - 1], DMX[C[i] - 1]); // } for (int i = 0; i < m; ++i) { if (dvis[i] == 0) dfsX(i); } for (int i = 0; i < 2 * czapa + 1; ++i) { tree[i] = 0; } add(0, 1); long long ret = 1; // std::vector<std::pair<std::pair<int, int>, long long>> X; // X.push_back(std::make_pair(std::make_pair(0, 0), 1)); std::set<std::pair<int, int>> S; for (int i = 1; i <= n; ++i) { // printf("%d\n", i); std::pair<int, int> rt = std::make_pair(DMN[C[i - 1] - 1] + 1, DMX[C[i - 1] - 1] + 1); if (S.find(rt) == S.end()) { S.insert(rt); long long lret = sum(rt.first); // long long lret2 = 0; // for (int j = 0; j < X.size(); ++j) { // if (X[j].first.first < rt.first && X[j].first.second < i) { // lret2 = (lret2 + X[j].second) % mod; // } // } // printf("V: %d: %d %d\n", i, rt.first, rt.second); // printf("W: %lld vs %lld\n", lret, lret2); // X.push_back(std::make_pair(std::make_pair(i, rt.second), lret2)); pending[rt.second]. push_back(std::make_pair(i, lret)); ret = (ret + lret) % mod; } for (int j = 0; j < pending[i].size(); ++j) { add(pending[i][j].first, pending[i][j].second); } } printf("%lld\n", ret); 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 | #include <cstdio> #include <vector> #include <set> const int max_n = 500100; int n; long long a[max_n]; long long r[max_n]; int top; std::pair<long long, int> stack[max_n]; std::vector<int> G[max_n]; std::vector<int> current; std::vector<std::vector<int>> solution; int D[max_n], S[max_n], InS[max_n], C[max_n]; int ile, tt; int delta[max_n], eta[max_n]; int best; int m; std::vector<int> DAG[max_n]; int DMN[max_n], DMX[max_n]; int dvis[max_n]; const int mod = 1000000007; void dfsX(int v) { dvis[v] = 1; int mx = DMN[v], mn = DMX[v]; for (int i = 0; i < DAG[v].size(); ++i) { if (dvis[DAG[v][i]] == 0) { dfsX(DAG[v][i]); } DMN[v] = std::min(DMN[v], DMN[DAG[v][i]]); DMX[v] = std::max(DMX[v], DMX[DAG[v][i]]); } } void dfs(int v) { S[top++] = v; InS[v] = 1; delta[v] = eta[v] = ++tt; D[v] = 1; for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] == 0) { dfs(G[v][i]); eta[v] = std::min(eta[v], eta[G[v][i]]); } else if (delta[G[v][i]] < delta[v] && InS[G[v][i]]) eta[v] = std::min(eta[v], delta[G[v][i]]); } if (delta[v] == eta[v]) { ++ile; current.clear(); while (top > 0 && delta[v] <= delta[S[top - 1]]) { --top; InS[S[top]] = 0; C[S[top]] = ile; current.push_back(S[top] + 1); } solution.push_back(current); } } int lmSCCTarjanAlgo() { for (int i = 0; i < n; ++i) D[i] = InS[i] = 0; top = ile = tt = 0; for (int i = 0; i < n; ++i) if (!D[i]) dfs(i); return ile; } const int czapa = 524288; long long tree[2 * czapa + 1]; std::vector<std::pair<int, long long>> pending[max_n]; void add(int index, long long val) { index += czapa; while (index >= 1) { tree[index] += val; index >>= 1; } } long long sum(int index) { index += czapa; long long ret = 0; while (index > 1) { if (index & 1) ret += tree[index - 1]; index >>= 1; } return ret % mod; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld %lld", &a[i], &r[i]); } top = 1; stack[0].first = a[0] + r[0]; stack[0].second = 0; for (int i = 1; i < n; ++i) { while (top > 0 && stack[top - 1].first < a[i]) --top; if (top > 0) { G[stack[top - 1].second].push_back(i); } while (top > 0 && stack[top - 1].first <= a[i] + r[i]) --top; stack[top].first = a[i] + r[i]; stack[top].second = i; ++top; } top = 1; stack[0].first = a[n - 1] - r[n - 1]; stack[0].second = n - 1; for (int i = n - 2; i >= 0; --i) { while (top > 0 && stack[top - 1].first > a[i]) --top; if (top > 0) { G[stack[top - 1].second].push_back(i); } while (top > 0 && stack[top - 1].first >= a[i] - r[i]) --top; stack[top].first = a[i] - r[i]; stack[top].second = i; ++top; } // for (int i = 0; i < n; ++i) { // printf("%d:", i); // for (int j = 0; j < G[i].size(); ++j) { // printf (" %d", G[i][j]); // } // printf("\n"); // } lmSCCTarjanAlgo(); // for (int i = 0; i < n; ++i) { // printf("C: %d: %d\n", i, C[i]); // } m = ile; for (int i = 0; i < n; ++i) { for (int j = 0; j < G[i].size(); ++j) { if (C[i] != C[G[i][j]]) DAG[C[i] - 1].push_back(C[G[i][j]] - 1); } } for (int i = 0; i < m; ++i) DMX[i] = -1, DMN[i] = 1000000000, dvis[i] = 0; for (int i = 0; i < n; ++i) { DMX[C[i] - 1] = std::max(DMX[C[i] - 1], i); DMN[C[i] - 1] = std::min(DMN[C[i] - 1], i); } // for (int i = 0; i < n; ++i) { // printf("V: %d: %d %d\n", i, DMN[C[i] - 1], DMX[C[i] - 1]); // } for (int i = 0; i < m; ++i) { if (dvis[i] == 0) dfsX(i); } for (int i = 0; i < 2 * czapa + 1; ++i) { tree[i] = 0; } add(0, 1); long long ret = 1; // std::vector<std::pair<std::pair<int, int>, long long>> X; // X.push_back(std::make_pair(std::make_pair(0, 0), 1)); std::set<std::pair<int, int>> S; for (int i = 1; i <= n; ++i) { // printf("%d\n", i); std::pair<int, int> rt = std::make_pair(DMN[C[i - 1] - 1] + 1, DMX[C[i - 1] - 1] + 1); if (S.find(rt) == S.end()) { S.insert(rt); long long lret = sum(rt.first); // long long lret2 = 0; // for (int j = 0; j < X.size(); ++j) { // if (X[j].first.first < rt.first && X[j].first.second < i) { // lret2 = (lret2 + X[j].second) % mod; // } // } // printf("V: %d: %d %d\n", i, rt.first, rt.second); // printf("W: %lld vs %lld\n", lret, lret2); // X.push_back(std::make_pair(std::make_pair(i, rt.second), lret2)); pending[rt.second]. push_back(std::make_pair(i, lret)); ret = (ret + lret) % mod; } for (int j = 0; j < pending[i].size(); ++j) { add(pending[i][j].first, pending[i][j].second); } } printf("%lld\n", ret); return 0; } |