#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 3e5 + 7; const int T = 1 << 19; const int MX = 1e9 + 7; int n; LL a[N], r[N]; set <PLL> S; vector <PII> Ranges; int Left[N]; int Right[N]; LL tree[T + T]; int rtree[2][T + T]; void read() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%lld %lld", &a[i], &r[i]); S.insert({a[i], i}); } } int getLeft(LL v) { auto it = S.lower_bound({v, 0}); return (*it).nd; } int getRight(LL v) { auto it = S.lower_bound({v + 1, 0}); return (*--it).nd; } void add(int p, LL v) { p += T; while(p) { tree[p] += v; p >>= 1; } } LL query(int from, int to) { LL ret = 0; from += T, to += T; while(from <= to) { if(from & 1) ret += tree[from]; if(!(to & 1)) ret += tree[to]; from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void push(int t, int p, int v, function <int (int, int)> fun) { p += T; rtree[t][p] = v; while(p) { p >>= 1; rtree[t][p] = fun(rtree[t][p + p], rtree[t][p + p + 1]); } } int ask(int t, int from, int to, function <int (int, int)> fun) { int ret = t ? 0 : n + 1; from += T, to += T; while(from <= to) { if(from & 1) ret = fun(ret, rtree[t][from]); if(!(to & 1)) ret = fun(ret, rtree[t][to]); from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void propagateRanges() { vector <int> idx(n); iota(idx.begin(), idx.end(), 1); sort(idx.begin(), idx.end(), [&](const int &fa, const int &fb) { return r[fa] > r[fb]; }); for(int t = 0; t < 2; ++t) for(int i = 0; i < T + T; ++i) rtree[t][i] = t ? 0 : n + 1; auto min_fun = [](const int fa, const int fb) -> int { return min(fa, fb); }; auto max_fun = [](const int fa, const int fb) -> int { return max(fa, fb); }; for(const auto &i: idx) { Left[i] = min(Left[i], ask(0, Left[i], Right[i], min_fun)); Right[i] = max(Right[i], ask(1, Left[i], Right[i], max_fun)); push(0, i, Left[i], min_fun); push(1, i, Right[i], max_fun); } } void getRanges() { vector <PII> cur = {{0, 0}}; for(int i = 1; i <= n; ++i) { int left = getLeft(a[i] - r[i]); while(cur.back().nd >= left) { left = min(left, cur.back().st); cur.pop_back(); } Left[i] = left; cur.push_back({left, i}); } cur = {{n + 1, n + 1}}; for(int i = n; i >= 1; --i) { int right = getRight(a[i] + r[i]); while(cur.back().st <= right) { right = max(right, cur.back().nd); cur.pop_back(); } Right[i] = right; cur.push_back({i, right}); } propagateRanges(); // for(int i = 1; i <= n; ++i) // printf("Left[%d] = %d, Right[%d] = %d\n", i, Left[i], i, Right[i]); } void solve() { getRanges(); for(int i = 1; i <= n; ++i) Ranges.push_back({Left[i], Right[i]}); sort(Ranges.begin(), Ranges.end(), [](const PII &fa, const PII &fb) { if(fa.nd == fb.nd) return fa.st > fb.st; return fa.nd < fb.nd; }); Ranges.erase(unique(Ranges.begin(), Ranges.end()), Ranges.end()); int ans = 1; add(0, 1); for(auto [lt, rt]: Ranges) { int tmp = query(0, lt - 1) % MX; // printf("for %d %d we have %d\n", lt, rt, tmp); ans = (ans + tmp) % MX; add(lt, tmp); } printf("%d\n", ans); } int main() { read(); solve(); 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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 3e5 + 7; const int T = 1 << 19; const int MX = 1e9 + 7; int n; LL a[N], r[N]; set <PLL> S; vector <PII> Ranges; int Left[N]; int Right[N]; LL tree[T + T]; int rtree[2][T + T]; void read() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%lld %lld", &a[i], &r[i]); S.insert({a[i], i}); } } int getLeft(LL v) { auto it = S.lower_bound({v, 0}); return (*it).nd; } int getRight(LL v) { auto it = S.lower_bound({v + 1, 0}); return (*--it).nd; } void add(int p, LL v) { p += T; while(p) { tree[p] += v; p >>= 1; } } LL query(int from, int to) { LL ret = 0; from += T, to += T; while(from <= to) { if(from & 1) ret += tree[from]; if(!(to & 1)) ret += tree[to]; from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void push(int t, int p, int v, function <int (int, int)> fun) { p += T; rtree[t][p] = v; while(p) { p >>= 1; rtree[t][p] = fun(rtree[t][p + p], rtree[t][p + p + 1]); } } int ask(int t, int from, int to, function <int (int, int)> fun) { int ret = t ? 0 : n + 1; from += T, to += T; while(from <= to) { if(from & 1) ret = fun(ret, rtree[t][from]); if(!(to & 1)) ret = fun(ret, rtree[t][to]); from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void propagateRanges() { vector <int> idx(n); iota(idx.begin(), idx.end(), 1); sort(idx.begin(), idx.end(), [&](const int &fa, const int &fb) { return r[fa] > r[fb]; }); for(int t = 0; t < 2; ++t) for(int i = 0; i < T + T; ++i) rtree[t][i] = t ? 0 : n + 1; auto min_fun = [](const int fa, const int fb) -> int { return min(fa, fb); }; auto max_fun = [](const int fa, const int fb) -> int { return max(fa, fb); }; for(const auto &i: idx) { Left[i] = min(Left[i], ask(0, Left[i], Right[i], min_fun)); Right[i] = max(Right[i], ask(1, Left[i], Right[i], max_fun)); push(0, i, Left[i], min_fun); push(1, i, Right[i], max_fun); } } void getRanges() { vector <PII> cur = {{0, 0}}; for(int i = 1; i <= n; ++i) { int left = getLeft(a[i] - r[i]); while(cur.back().nd >= left) { left = min(left, cur.back().st); cur.pop_back(); } Left[i] = left; cur.push_back({left, i}); } cur = {{n + 1, n + 1}}; for(int i = n; i >= 1; --i) { int right = getRight(a[i] + r[i]); while(cur.back().st <= right) { right = max(right, cur.back().nd); cur.pop_back(); } Right[i] = right; cur.push_back({i, right}); } propagateRanges(); // for(int i = 1; i <= n; ++i) // printf("Left[%d] = %d, Right[%d] = %d\n", i, Left[i], i, Right[i]); } void solve() { getRanges(); for(int i = 1; i <= n; ++i) Ranges.push_back({Left[i], Right[i]}); sort(Ranges.begin(), Ranges.end(), [](const PII &fa, const PII &fb) { if(fa.nd == fb.nd) return fa.st > fb.st; return fa.nd < fb.nd; }); Ranges.erase(unique(Ranges.begin(), Ranges.end()), Ranges.end()); int ans = 1; add(0, 1); for(auto [lt, rt]: Ranges) { int tmp = query(0, lt - 1) % MX; // printf("for %d %d we have %d\n", lt, rt, tmp); ans = (ans + tmp) % MX; add(lt, tmp); } printf("%d\n", ans); } int main() { read(); solve(); return 0; } |