#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(int64_t x) { cerr << x; } void __print(uint32_t x) { cerr << x; } void __print(uint64_t x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template<typename T, typename V> void __print(const pair<T, V> &x) { cerr << '('; __print(x.first); cerr << ", "; __print(x.second); cerr << ')'; } template<typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #define debug(x...) cerr << " > [" << #x << "] = ["; _print(x) const int64_t mod = 1e9 + 7; const int64_t oo = 1e18 + 5; map<int64_t, int> sc; void init() { int sc_timer = 0; for(auto &p : sc) p.second = sc_timer++; } struct TreeNode { int64_t sum, lazy; TreeNode *leftNode, *rightNode; TreeNode() : sum(0), lazy(-1), leftNode(nullptr), rightNode(nullptr) {} TreeNode(int64_t _sum, int64_t _lazy) : sum(_sum), lazy(_lazy), leftNode(nullptr), rightNode(nullptr) {} TreeNode(TreeNode* _leftNode, TreeNode* _rightNode) : sum(0), lazy(-1), leftNode(_leftNode), rightNode(_rightNode) {} TreeNode(int64_t _sum, int64_t _lazy, TreeNode* _leftNode, TreeNode* _rightNode) : sum(_sum), lazy(_lazy), leftNode(_leftNode), rightNode(_rightNode) {} }; TreeNode* build(int left, int right) { if(left == right) return new TreeNode(); int mid = (left + right) / 2; return new TreeNode(build(left, mid), build(mid + 1, right)); } void push(TreeNode* node, int left, int right) { if(node->lazy == -1) return; int64_t sz = (right - left + 1) / 2; if(node->leftNode == nullptr) { node->leftNode = new TreeNode((node->lazy * sz) % mod, node->lazy); } else { node->leftNode->sum = (node->lazy * sz) % mod; node->leftNode->lazy = node->lazy; } if(node->rightNode == nullptr) { node->rightNode = new TreeNode((node->lazy * sz) % mod, node->lazy); } else { node->rightNode->sum = (node->lazy * sz) % mod; node->rightNode->lazy = node->lazy; } node->lazy = -1; } int64_t read(TreeNode* node, int left, int right, int l, int r) { if(left > r or right < l) { return 0; } if(left >= l and right <= r) { return node->sum; } push(node, left, right); int mid = (left + right) / 2; return (read(node->leftNode, left, mid, l, r) + read(node->rightNode, mid + 1, right, l, r)) % mod; } TreeNode* change(TreeNode* node, int left, int right, int l, int r, int64_t val) { if(left > r or right < l) { return node; } if(left >= l and right <= r) { int64_t sz = (right - left + 1); return new TreeNode((val * sz) % mod, val); } push(node, left, right); int mid = (left + right) / 2; TreeNode* curleft = change(node->leftNode, left, mid, l, r, val); TreeNode* curright = change(node->rightNode, mid + 1, right, l, r, val); int64_t new_sum = (curleft->sum + curright->sum) % mod; return new TreeNode(new_sum, node->lazy, curleft, curright); } int n; vector<int64_t> a; vector<int64_t> r; vector<int> left_id; vector<int> right_id; vector<int64_t> left_range; vector<int64_t> right_range; vector<int64_t> max_len; vector<bool> processed; void process(int l, int p) { if(l > p) return; int i = (l + p) / 2; if(processed[i]) return; processed[i] = true; int64_t max_left = a[i] - r[i]; int64_t max_right = a[i] + r[i]; left_id[i] = i - 1; right_id[i] = i + 1; while((left_id[i] > 0 and max_left <= a[left_id[i]]) or (right_id[i] < n and max_right >= a[right_id[i]])) { while(left_id[i] > 0 and max_left <= a[left_id[i]]) { if(processed[left_id[i]]) { max_left = min(max_left, left_range[left_id[i]]); max_right = max(max_right, right_range[left_id[i]]); right_id[i] = max(right_id[i], right_id[left_id[i]]); left_id[i] = left_id[left_id[i]]; } else { max_left = min(max_left, a[left_id[i]] - r[left_id[i]]); max_right = max(max_right, a[left_id[i]] + r[left_id[i]]); left_id[i]--; } } while(right_id[i] < n and max_right >= a[right_id[i]]) { if(processed[right_id[i]]) { max_left = min(max_left, left_range[right_id[i]]); max_right = max(max_right, right_range[right_id[i]]); left_id[i] = min(left_id[i], left_id[right_id[i]]); right_id[i] = right_id[right_id[i]]; } else { max_left = min(max_left, a[right_id[i]] - r[right_id[i]]); max_right = max(max_right, a[right_id[i]] + r[right_id[i]]); right_id[i]++; } } } left_range[i] = max_left; right_range[i] = max_right; process(l, i - 1); process(i + 1, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; n++; a.resize(n + 1, oo); r.resize(n + 1); left_id.resize(n + 1); right_id.resize(n + 1); left_range.resize(n + 1); right_range.resize(n + 1); max_len.resize(n + 1); processed.resize(n + 1, false); processed[0] = true; a[0] = -oo; r[0] = 0; left_id[0] = -1; right_id[0] = 1; max_len[0] = 0; sc[0]; for(int i = 1; i < n; i++) { cin >> a[i] >> r[i]; a[i]++; sc[a[i]]; sc[a[i] + r[i]]; max_len[i] = max(max_len[i - 1], a[i] + r[i]); } process(1, n - 1); init(); int BASE = 1; while(BASE <= (int)sc.size()) BASE <<= 1; vector<TreeNode*> state; TreeNode* root = build(0, BASE - 1); TreeNode* root2 = change(root, 0, BASE - 1, sc[0], sc[0], 1); state.push_back(root2); for(int i = 1; i < n; i++) { TreeNode* cur_root = state.back(); TreeNode* temp_root = change(cur_root, 0, BASE - 1, sc[a[i]], sc[right_range[i]], 0); int64_t next_sum = read(state[left_id[i]], 0, BASE - 1, sc[0], sc[right_range[i]]); TreeNode* new_root = change(temp_root, 0, BASE - 1, sc[right_range[i]], sc[right_range[i]], next_sum); state.push_back(new_root); } int64_t res = read(state.back(), 0, BASE - 1, 0, BASE - 1); cout << res; 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 | #include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(int64_t x) { cerr << x; } void __print(uint32_t x) { cerr << x; } void __print(uint64_t x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template<typename T, typename V> void __print(const pair<T, V> &x) { cerr << '('; __print(x.first); cerr << ", "; __print(x.second); cerr << ')'; } template<typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #define debug(x...) cerr << " > [" << #x << "] = ["; _print(x) const int64_t mod = 1e9 + 7; const int64_t oo = 1e18 + 5; map<int64_t, int> sc; void init() { int sc_timer = 0; for(auto &p : sc) p.second = sc_timer++; } struct TreeNode { int64_t sum, lazy; TreeNode *leftNode, *rightNode; TreeNode() : sum(0), lazy(-1), leftNode(nullptr), rightNode(nullptr) {} TreeNode(int64_t _sum, int64_t _lazy) : sum(_sum), lazy(_lazy), leftNode(nullptr), rightNode(nullptr) {} TreeNode(TreeNode* _leftNode, TreeNode* _rightNode) : sum(0), lazy(-1), leftNode(_leftNode), rightNode(_rightNode) {} TreeNode(int64_t _sum, int64_t _lazy, TreeNode* _leftNode, TreeNode* _rightNode) : sum(_sum), lazy(_lazy), leftNode(_leftNode), rightNode(_rightNode) {} }; TreeNode* build(int left, int right) { if(left == right) return new TreeNode(); int mid = (left + right) / 2; return new TreeNode(build(left, mid), build(mid + 1, right)); } void push(TreeNode* node, int left, int right) { if(node->lazy == -1) return; int64_t sz = (right - left + 1) / 2; if(node->leftNode == nullptr) { node->leftNode = new TreeNode((node->lazy * sz) % mod, node->lazy); } else { node->leftNode->sum = (node->lazy * sz) % mod; node->leftNode->lazy = node->lazy; } if(node->rightNode == nullptr) { node->rightNode = new TreeNode((node->lazy * sz) % mod, node->lazy); } else { node->rightNode->sum = (node->lazy * sz) % mod; node->rightNode->lazy = node->lazy; } node->lazy = -1; } int64_t read(TreeNode* node, int left, int right, int l, int r) { if(left > r or right < l) { return 0; } if(left >= l and right <= r) { return node->sum; } push(node, left, right); int mid = (left + right) / 2; return (read(node->leftNode, left, mid, l, r) + read(node->rightNode, mid + 1, right, l, r)) % mod; } TreeNode* change(TreeNode* node, int left, int right, int l, int r, int64_t val) { if(left > r or right < l) { return node; } if(left >= l and right <= r) { int64_t sz = (right - left + 1); return new TreeNode((val * sz) % mod, val); } push(node, left, right); int mid = (left + right) / 2; TreeNode* curleft = change(node->leftNode, left, mid, l, r, val); TreeNode* curright = change(node->rightNode, mid + 1, right, l, r, val); int64_t new_sum = (curleft->sum + curright->sum) % mod; return new TreeNode(new_sum, node->lazy, curleft, curright); } int n; vector<int64_t> a; vector<int64_t> r; vector<int> left_id; vector<int> right_id; vector<int64_t> left_range; vector<int64_t> right_range; vector<int64_t> max_len; vector<bool> processed; void process(int l, int p) { if(l > p) return; int i = (l + p) / 2; if(processed[i]) return; processed[i] = true; int64_t max_left = a[i] - r[i]; int64_t max_right = a[i] + r[i]; left_id[i] = i - 1; right_id[i] = i + 1; while((left_id[i] > 0 and max_left <= a[left_id[i]]) or (right_id[i] < n and max_right >= a[right_id[i]])) { while(left_id[i] > 0 and max_left <= a[left_id[i]]) { if(processed[left_id[i]]) { max_left = min(max_left, left_range[left_id[i]]); max_right = max(max_right, right_range[left_id[i]]); right_id[i] = max(right_id[i], right_id[left_id[i]]); left_id[i] = left_id[left_id[i]]; } else { max_left = min(max_left, a[left_id[i]] - r[left_id[i]]); max_right = max(max_right, a[left_id[i]] + r[left_id[i]]); left_id[i]--; } } while(right_id[i] < n and max_right >= a[right_id[i]]) { if(processed[right_id[i]]) { max_left = min(max_left, left_range[right_id[i]]); max_right = max(max_right, right_range[right_id[i]]); left_id[i] = min(left_id[i], left_id[right_id[i]]); right_id[i] = right_id[right_id[i]]; } else { max_left = min(max_left, a[right_id[i]] - r[right_id[i]]); max_right = max(max_right, a[right_id[i]] + r[right_id[i]]); right_id[i]++; } } } left_range[i] = max_left; right_range[i] = max_right; process(l, i - 1); process(i + 1, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; n++; a.resize(n + 1, oo); r.resize(n + 1); left_id.resize(n + 1); right_id.resize(n + 1); left_range.resize(n + 1); right_range.resize(n + 1); max_len.resize(n + 1); processed.resize(n + 1, false); processed[0] = true; a[0] = -oo; r[0] = 0; left_id[0] = -1; right_id[0] = 1; max_len[0] = 0; sc[0]; for(int i = 1; i < n; i++) { cin >> a[i] >> r[i]; a[i]++; sc[a[i]]; sc[a[i] + r[i]]; max_len[i] = max(max_len[i - 1], a[i] + r[i]); } process(1, n - 1); init(); int BASE = 1; while(BASE <= (int)sc.size()) BASE <<= 1; vector<TreeNode*> state; TreeNode* root = build(0, BASE - 1); TreeNode* root2 = change(root, 0, BASE - 1, sc[0], sc[0], 1); state.push_back(root2); for(int i = 1; i < n; i++) { TreeNode* cur_root = state.back(); TreeNode* temp_root = change(cur_root, 0, BASE - 1, sc[a[i]], sc[right_range[i]], 0); int64_t next_sum = read(state[left_id[i]], 0, BASE - 1, sc[0], sc[right_range[i]]); TreeNode* new_root = change(temp_root, 0, BASE - 1, sc[right_range[i]], sc[right_range[i]], next_sum); state.push_back(new_root); } int64_t res = read(state.back(), 0, BASE - 1, 0, BASE - 1); cout << res; return 0; } |