#include <bits/stdc++.h> using namespace std; // sprawdź stałą!!! const long long M = 1000000007; long long n; long long a[350000], r[350000]; long long dp_zab[350000]; long long dp_zost[350000]; long long dp_total[350000]; long long pref_sum[350000]; class ordering{ public: bool high; vector < pair < long long, long long > > v; vector < long long> v_min; bool comp(long long x, long long y){ if((x > y and high) or (x < y and !high)){ return true; } return false; } long long mini(long long x, long long y){ if(comp(x, y)){ //cout << "T" << x << endl; return x; } return y; } void insert(long long ind, long long val){ //cout << "insert: " << ind << " " << val << endl; if(v.size() > 0){ if(high){ while(!v_min.empty()){ if(val <= v_min[v_min.size() - 1]){ v_min.pop_back(); v.pop_back(); } else{ break; } } v_min.push_back(val); v.push_back(make_pair(ind, val)); } else{ while(!v_min.empty()){ if(val >= v_min[v_min.size() - 1]){ v_min.pop_back(); v.pop_back(); } else{ break; } } v_min.push_back(val); v.push_back(make_pair(ind, val)); } } else{ v_min.push_back(val); v.push_back(make_pair(ind, val)); } } // zwraca największy ind, że val long long bound(long long val){ // binsearch long long maksimum = v_min.size() - 1; long long minimum = -1; long long cur; if(high){ while(maksimum > minimum){ //cout << "A" << endl; cur = minimum + (maksimum - minimum + 1) / 2; //cout << "maksimum = " << maksimum << " cur = " << cur << " minimum = " << minimum << endl; if(val > v_min[cur]){ minimum = cur; } else{ maksimum = cur - 1; } } } else{ while(maksimum > minimum){ //cout << "B" << endl; cur = minimum + (maksimum - minimum +1) / 2; //cout << "maksimum = " << maksimum << " cur = " << cur << " minimum = " << minimum << endl; if(val < v_min[cur]){ minimum = cur; } else{ maksimum = cur - 1; } } } /*cout << "val = " << val << endl; cout << "high = " << high << endl; for (long long i = 0; i < v.size(); ++i) { cout << v[i].second << "(" << v[i].first<< ") "; } cout << endl; cout << "m = " << m << " maksimum = " << maksimum << endl << endl; return m;*/ if(maksimum == -1) return -1; /*if(v[maksimum].first == 0){ return -1; }*/ return v[maksimum].first; } }; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; ++i) { cin >> a[i] >> r[i]; } // to trzeba sprawdzić dp_zost[1] = 1; dp_zab[1] = 1; dp_total[1] = 2; // orderingi ordering a_ord; ordering a_diff_ord; ordering a_sum_ord; a_ord.insert(1, a[1] % M); a_ord.high = true; a_diff_ord.insert(1, (a[1] - r[1] - 1) ); a_diff_ord.high = true; a_sum_ord.insert(1, (a[1] + r[1] + 1)); a_sum_ord.high = false; stack < long long > white_alive; set < pair < long long, long long > > white_alive_set; white_alive.push(1); pref_sum[1] = dp_zost[1] % M; white_alive_set.insert(make_pair(1, 0)); for (long long i = 2; i <= n; ++i) { // najpierw i-ty żyje long long zost_j = a_sum_ord.bound(a[i]); if(zost_j == -1){ dp_zost[i]++; } //cout << zost_j << endl; long long c = 0; pair < long long, long long > t1 = *(white_alive_set.upper_bound(make_pair(zost_j, -2))); dp_zost[i] += (pref_sum[white_alive.size()] - pref_sum[t1.second] + M ) % M; // teraz i-ty nie żyje long long first_j = a_ord.bound(a[i] - r[i]); long long second_j = a_diff_ord.bound(a[i] - r[i]); if(first_j == second_j and second_j == -1){ dp_zab[i] = 1; } else{ if(second_j > first_j){ dp_zab[i] = dp_zab[second_j]; } else{ if(first_j == i - 1){ dp_zab[i] = dp_total[first_j]; } dp_zab[i] = dp_total[first_j]; } } dp_total[i] = (dp_zab[i] + dp_zost[i]) % M; a_ord.insert(i, a[i]); a_diff_ord.insert(i, a[i] - r[i] - 1); a_sum_ord.insert(i, a[i] + r[i] + 1); // aktualizujemy white_alive while(!white_alive.empty()){ if(a[white_alive.top()] >= a[i] - r[i]){ //cout << "A" << endl; white_alive_set.erase(white_alive_set.find(make_pair(white_alive.top(), white_alive.size() - 1))); //cout << "B" << endl; pref_sum[white_alive.size()] = 0; white_alive.pop(); } else{ break; } //cout << "A" << endl; } white_alive_set.insert(make_pair(i, white_alive.size())); white_alive.push(i); pref_sum[white_alive.size()] = (pref_sum[white_alive.size() - 1] + dp_zost[i])%M; //cout << "size = " << white_alive.size() << endl; //cout << dp_zab[i] << " " << dp_zost[i] << endl; } //cout << dp_zab[n] << " "; cout << (dp_zab[n] + dp_zost[n] + M) % M << endl; }
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 | #include <bits/stdc++.h> using namespace std; // sprawdź stałą!!! const long long M = 1000000007; long long n; long long a[350000], r[350000]; long long dp_zab[350000]; long long dp_zost[350000]; long long dp_total[350000]; long long pref_sum[350000]; class ordering{ public: bool high; vector < pair < long long, long long > > v; vector < long long> v_min; bool comp(long long x, long long y){ if((x > y and high) or (x < y and !high)){ return true; } return false; } long long mini(long long x, long long y){ if(comp(x, y)){ //cout << "T" << x << endl; return x; } return y; } void insert(long long ind, long long val){ //cout << "insert: " << ind << " " << val << endl; if(v.size() > 0){ if(high){ while(!v_min.empty()){ if(val <= v_min[v_min.size() - 1]){ v_min.pop_back(); v.pop_back(); } else{ break; } } v_min.push_back(val); v.push_back(make_pair(ind, val)); } else{ while(!v_min.empty()){ if(val >= v_min[v_min.size() - 1]){ v_min.pop_back(); v.pop_back(); } else{ break; } } v_min.push_back(val); v.push_back(make_pair(ind, val)); } } else{ v_min.push_back(val); v.push_back(make_pair(ind, val)); } } // zwraca największy ind, że val long long bound(long long val){ // binsearch long long maksimum = v_min.size() - 1; long long minimum = -1; long long cur; if(high){ while(maksimum > minimum){ //cout << "A" << endl; cur = minimum + (maksimum - minimum + 1) / 2; //cout << "maksimum = " << maksimum << " cur = " << cur << " minimum = " << minimum << endl; if(val > v_min[cur]){ minimum = cur; } else{ maksimum = cur - 1; } } } else{ while(maksimum > minimum){ //cout << "B" << endl; cur = minimum + (maksimum - minimum +1) / 2; //cout << "maksimum = " << maksimum << " cur = " << cur << " minimum = " << minimum << endl; if(val < v_min[cur]){ minimum = cur; } else{ maksimum = cur - 1; } } } /*cout << "val = " << val << endl; cout << "high = " << high << endl; for (long long i = 0; i < v.size(); ++i) { cout << v[i].second << "(" << v[i].first<< ") "; } cout << endl; cout << "m = " << m << " maksimum = " << maksimum << endl << endl; return m;*/ if(maksimum == -1) return -1; /*if(v[maksimum].first == 0){ return -1; }*/ return v[maksimum].first; } }; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; ++i) { cin >> a[i] >> r[i]; } // to trzeba sprawdzić dp_zost[1] = 1; dp_zab[1] = 1; dp_total[1] = 2; // orderingi ordering a_ord; ordering a_diff_ord; ordering a_sum_ord; a_ord.insert(1, a[1] % M); a_ord.high = true; a_diff_ord.insert(1, (a[1] - r[1] - 1) ); a_diff_ord.high = true; a_sum_ord.insert(1, (a[1] + r[1] + 1)); a_sum_ord.high = false; stack < long long > white_alive; set < pair < long long, long long > > white_alive_set; white_alive.push(1); pref_sum[1] = dp_zost[1] % M; white_alive_set.insert(make_pair(1, 0)); for (long long i = 2; i <= n; ++i) { // najpierw i-ty żyje long long zost_j = a_sum_ord.bound(a[i]); if(zost_j == -1){ dp_zost[i]++; } //cout << zost_j << endl; long long c = 0; pair < long long, long long > t1 = *(white_alive_set.upper_bound(make_pair(zost_j, -2))); dp_zost[i] += (pref_sum[white_alive.size()] - pref_sum[t1.second] + M ) % M; // teraz i-ty nie żyje long long first_j = a_ord.bound(a[i] - r[i]); long long second_j = a_diff_ord.bound(a[i] - r[i]); if(first_j == second_j and second_j == -1){ dp_zab[i] = 1; } else{ if(second_j > first_j){ dp_zab[i] = dp_zab[second_j]; } else{ if(first_j == i - 1){ dp_zab[i] = dp_total[first_j]; } dp_zab[i] = dp_total[first_j]; } } dp_total[i] = (dp_zab[i] + dp_zost[i]) % M; a_ord.insert(i, a[i]); a_diff_ord.insert(i, a[i] - r[i] - 1); a_sum_ord.insert(i, a[i] + r[i] + 1); // aktualizujemy white_alive while(!white_alive.empty()){ if(a[white_alive.top()] >= a[i] - r[i]){ //cout << "A" << endl; white_alive_set.erase(white_alive_set.find(make_pair(white_alive.top(), white_alive.size() - 1))); //cout << "B" << endl; pref_sum[white_alive.size()] = 0; white_alive.pop(); } else{ break; } //cout << "A" << endl; } white_alive_set.insert(make_pair(i, white_alive.size())); white_alive.push(i); pref_sum[white_alive.size()] = (pref_sum[white_alive.size() - 1] + dp_zost[i])%M; //cout << "size = " << white_alive.size() << endl; //cout << dp_zab[i] << " " << dp_zost[i] << endl; } //cout << dp_zab[n] << " "; cout << (dp_zab[n] + dp_zost[n] + M) % M << endl; } |