// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) ; #define M 1000000007 #define INF 1000000007LL using namespace std; int n, S = 1; list <int> l; //vector <pair<int ,int>> cofka[300007]; // <poz_powrotna, wynik> //long long d2[2100007]; long long ress[300007]; long long* res; int ill[1000007]; vector <pair<int, int>> v; struct mina { long long poz, range; int l, r; }; mina tab[300007]; struct drzewo { int minn, maks; }; drzewo d[2100007]; struct drzewo2 { long long suma; long long maxpref; }; drzewo2 d2[2100007]; pair<int, int> get(int nr, int pocz, int kon, int p, int k) { if(p == pocz && k == kon) return MP(d[nr].minn, d[nr].maks); int s = (pocz + kon)/2; if(k <= s) return get(nr*2, pocz, s, p, k); if(s < p) return get(nr*2+1, s+1, kon, p, k); auto p1 = get(nr*2, pocz, s, p, s); auto p2 = get(nr*2+1, s+1, kon, s+1, k); return MP(min(p1.ff, p2.ff), max(p1.ss, p2.ss)); } void update(int nr, int minn, int maks) { nr += S; d[nr].minn = min(d[nr].minn, minn); d[nr].maks = max(d[nr].maks, maks); nr >>= 1; while(nr > 0) { d[nr].minn = min(d[nr*2].minn, d[nr*2+1].minn); d[nr].maks = max(d[nr*2].maks, d[nr*2+1].maks); nr >>= 1; } } void dokladaj(int nr, long long ile) { deb(cout << "doklada " << ile << " na pozycji " << nr << endl); nr += S; d2[nr].suma += ile; d2[nr].maxpref = max(0ll, d2[nr].suma); nr >>= 1; while(nr > 0) { d2[nr].suma = d2[nr*2].suma + d2[nr*2+1].suma; d2[nr].maxpref = max(d2[nr*2].maxpref, d2[nr*2].suma + d2[nr*2+1].maxpref); nr >>= 1; } } // <maxpref, poz> pair<long long, int> znajdz_dod_pref(int nr) { nr += S; if(d2[nr].suma > 0) return MP(d2[nr].suma, nr-S); long long suma = d2[nr].suma; while(nr > 0) { if((nr & 1) == 0) // idziemy z lewej do gory { if(suma + d2[nr+1].maxpref > 0) { nr++; break; } suma += d2[nr+1].suma; } nr >>= 1; } if(nr == 0) // nie ma dod prefiksu return MP(0, 10000007); while(nr < S) { if(suma + d2[nr*2].maxpref > 0) nr *= 2; else { suma += d2[nr*2].suma; nr = nr*2+1; } } return MP(suma+d2[nr].suma, nr-S); } int main() { scanf("%d", &n); FOR(i, 1, n) { scanf("%lld %lld", &tab[i].poz, &tab[i].range); tab[i].l = tab[i].r = i; l.PB(i); } FORD(i, n, 1) l.PB(i); FOR(i, 1, n) { int pocz = 1, kon = n, s; while(pocz < kon) { s = (pocz+kon)/2; if(tab[s].poz < tab[i].poz-tab[i].range) pocz = s+1; else kon = s; } tab[i].l = pocz; pocz = 1; kon = n; while(pocz < kon) { s = (pocz+kon+1)/2; if(tab[i].poz+tab[i].range < tab[s].poz) kon = s-1; else pocz = s; } tab[i].r = pocz; // cout << i << ": " << tab[i].l << " " << tab[i].r << endl; } while(S < n+3) S *= 2; forr(i, S) { d[i+S].minn = n+1; d[i+S].maks = 0; } FOR(i, 1, n) { d[i+S].minn = tab[i].l; d[i+S].maks = tab[i].r; } FORD(i, S-1, 1) { d[i].minn = min(d[i*2].minn, d[i*2+1].minn); d[i].maks = max(d[i*2].maks, d[i*2+1].maks); //cout << "node " << i << ": " << d[i].minn << " " << d[i].maks << endl; } while(!l.empty()) { int nr = l.front(); l.pop_front(); auto p = get(1, 0, S-1, tab[nr].l, tab[nr].r); //cout << "dla " << nr << " znalazl " << p.ff << " " << p.ss << endl; if(p.ff < tab[nr].l || p.ss > tab[nr].r) { tab[nr].l = p.ff; tab[nr].r = p.ss; update(nr, p.ff, p.ss); l.PB(nr); ill[nr] = 0; } else { if(ill[nr] == 0) l.PB(nr); ill[nr]++; } } FOR(i, 1, n) { deb(cout << i << ": " << tab[i].l << " " << tab[i].r << endl); v.PB(MP(tab[i].r, -tab[i].l)); } sort(v.begin(), v.end()); res = (long long*)ress+2; res[-1] = 1; res[0] = 1; //dokladaj(0, -1); //dokladaj(1, 1); int poz = 0; FOR(i, 1, n) { res[i] = res[i-1]; long long r2 = 0; while(poz < n && v[poz].ff == i) { long long r = 0; int pocz = -v[poz].ss; poz++; deb(cout << "przedzial " << pocz << " " << i << endl); while(1) { auto p = znajdz_dod_pref(pocz); if(p.ss > i) // nie ma juz dodatnich prefiksow break; deb(cout << "znalazl prefiks " << p.ff << " na pozycji " << p.ss << endl); r += p.ff; dokladaj(p.ss, -p.ff); } if(r > 0) dokladaj(i+1, r); if(d2[pocz+S].suma == 0) { } if(d2[pocz+S].suma < 0) { long long ile = -d2[pocz+S].suma; auto p = znajdz_dod_pref(pocz+1); if(p.ss <= i) { deb(cout << "tera bedzie zdejmowal prefiks krotki " << ile << " na przedziale " << pocz << " " << p.ss << endl); dokladaj(pocz, ile); dokladaj(p.ss, -ile); } } if(d2[pocz+S].suma == 0) { deb(cout << "doklada przedzial " << pocz << " " << i << " o wadze " << res[pocz-2] << endl); dokladaj(pocz, -res[pocz-2]); dokladaj(i+1, res[pocz-2]); r2 += res[pocz-2]; } deb(cout << " r = " << r << endl); r2 += r; deb(cout << " r2 = " << r2 << endl); } res[i] = (res[i] + r2)%M; deb(cout << "res[" << i << "] = " << res[i] << endl); } printf("%lld\n", res[n]); /* wkladaj(1, 1); FOR(i, 1, n) { res[i] = (res[i-1] + policz(tab[i].l-1)); cofka[tab[i].r].PB(MP(i, res[i])); for(auto p : cofka[i]) // <poz_powrotna, wynik> { res[i] = (res[i] + p.ss) % M; wkladaj(p.ff, p.ss); } cout << "res[" << i << "] = " << res[i] << endl; } */ 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 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | // Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) ; #define M 1000000007 #define INF 1000000007LL using namespace std; int n, S = 1; list <int> l; //vector <pair<int ,int>> cofka[300007]; // <poz_powrotna, wynik> //long long d2[2100007]; long long ress[300007]; long long* res; int ill[1000007]; vector <pair<int, int>> v; struct mina { long long poz, range; int l, r; }; mina tab[300007]; struct drzewo { int minn, maks; }; drzewo d[2100007]; struct drzewo2 { long long suma; long long maxpref; }; drzewo2 d2[2100007]; pair<int, int> get(int nr, int pocz, int kon, int p, int k) { if(p == pocz && k == kon) return MP(d[nr].minn, d[nr].maks); int s = (pocz + kon)/2; if(k <= s) return get(nr*2, pocz, s, p, k); if(s < p) return get(nr*2+1, s+1, kon, p, k); auto p1 = get(nr*2, pocz, s, p, s); auto p2 = get(nr*2+1, s+1, kon, s+1, k); return MP(min(p1.ff, p2.ff), max(p1.ss, p2.ss)); } void update(int nr, int minn, int maks) { nr += S; d[nr].minn = min(d[nr].minn, minn); d[nr].maks = max(d[nr].maks, maks); nr >>= 1; while(nr > 0) { d[nr].minn = min(d[nr*2].minn, d[nr*2+1].minn); d[nr].maks = max(d[nr*2].maks, d[nr*2+1].maks); nr >>= 1; } } void dokladaj(int nr, long long ile) { deb(cout << "doklada " << ile << " na pozycji " << nr << endl); nr += S; d2[nr].suma += ile; d2[nr].maxpref = max(0ll, d2[nr].suma); nr >>= 1; while(nr > 0) { d2[nr].suma = d2[nr*2].suma + d2[nr*2+1].suma; d2[nr].maxpref = max(d2[nr*2].maxpref, d2[nr*2].suma + d2[nr*2+1].maxpref); nr >>= 1; } } // <maxpref, poz> pair<long long, int> znajdz_dod_pref(int nr) { nr += S; if(d2[nr].suma > 0) return MP(d2[nr].suma, nr-S); long long suma = d2[nr].suma; while(nr > 0) { if((nr & 1) == 0) // idziemy z lewej do gory { if(suma + d2[nr+1].maxpref > 0) { nr++; break; } suma += d2[nr+1].suma; } nr >>= 1; } if(nr == 0) // nie ma dod prefiksu return MP(0, 10000007); while(nr < S) { if(suma + d2[nr*2].maxpref > 0) nr *= 2; else { suma += d2[nr*2].suma; nr = nr*2+1; } } return MP(suma+d2[nr].suma, nr-S); } int main() { scanf("%d", &n); FOR(i, 1, n) { scanf("%lld %lld", &tab[i].poz, &tab[i].range); tab[i].l = tab[i].r = i; l.PB(i); } FORD(i, n, 1) l.PB(i); FOR(i, 1, n) { int pocz = 1, kon = n, s; while(pocz < kon) { s = (pocz+kon)/2; if(tab[s].poz < tab[i].poz-tab[i].range) pocz = s+1; else kon = s; } tab[i].l = pocz; pocz = 1; kon = n; while(pocz < kon) { s = (pocz+kon+1)/2; if(tab[i].poz+tab[i].range < tab[s].poz) kon = s-1; else pocz = s; } tab[i].r = pocz; // cout << i << ": " << tab[i].l << " " << tab[i].r << endl; } while(S < n+3) S *= 2; forr(i, S) { d[i+S].minn = n+1; d[i+S].maks = 0; } FOR(i, 1, n) { d[i+S].minn = tab[i].l; d[i+S].maks = tab[i].r; } FORD(i, S-1, 1) { d[i].minn = min(d[i*2].minn, d[i*2+1].minn); d[i].maks = max(d[i*2].maks, d[i*2+1].maks); //cout << "node " << i << ": " << d[i].minn << " " << d[i].maks << endl; } while(!l.empty()) { int nr = l.front(); l.pop_front(); auto p = get(1, 0, S-1, tab[nr].l, tab[nr].r); //cout << "dla " << nr << " znalazl " << p.ff << " " << p.ss << endl; if(p.ff < tab[nr].l || p.ss > tab[nr].r) { tab[nr].l = p.ff; tab[nr].r = p.ss; update(nr, p.ff, p.ss); l.PB(nr); ill[nr] = 0; } else { if(ill[nr] == 0) l.PB(nr); ill[nr]++; } } FOR(i, 1, n) { deb(cout << i << ": " << tab[i].l << " " << tab[i].r << endl); v.PB(MP(tab[i].r, -tab[i].l)); } sort(v.begin(), v.end()); res = (long long*)ress+2; res[-1] = 1; res[0] = 1; //dokladaj(0, -1); //dokladaj(1, 1); int poz = 0; FOR(i, 1, n) { res[i] = res[i-1]; long long r2 = 0; while(poz < n && v[poz].ff == i) { long long r = 0; int pocz = -v[poz].ss; poz++; deb(cout << "przedzial " << pocz << " " << i << endl); while(1) { auto p = znajdz_dod_pref(pocz); if(p.ss > i) // nie ma juz dodatnich prefiksow break; deb(cout << "znalazl prefiks " << p.ff << " na pozycji " << p.ss << endl); r += p.ff; dokladaj(p.ss, -p.ff); } if(r > 0) dokladaj(i+1, r); if(d2[pocz+S].suma == 0) { } if(d2[pocz+S].suma < 0) { long long ile = -d2[pocz+S].suma; auto p = znajdz_dod_pref(pocz+1); if(p.ss <= i) { deb(cout << "tera bedzie zdejmowal prefiks krotki " << ile << " na przedziale " << pocz << " " << p.ss << endl); dokladaj(pocz, ile); dokladaj(p.ss, -ile); } } if(d2[pocz+S].suma == 0) { deb(cout << "doklada przedzial " << pocz << " " << i << " o wadze " << res[pocz-2] << endl); dokladaj(pocz, -res[pocz-2]); dokladaj(i+1, res[pocz-2]); r2 += res[pocz-2]; } deb(cout << " r = " << r << endl); r2 += r; deb(cout << " r2 = " << r2 << endl); } res[i] = (res[i] + r2)%M; deb(cout << "res[" << i << "] = " << res[i] << endl); } printf("%lld\n", res[n]); /* wkladaj(1, 1); FOR(i, 1, n) { res[i] = (res[i-1] + policz(tab[i].l-1)); cofka[tab[i].r].PB(MP(i, res[i])); for(auto p : cofka[i]) // <poz_powrotna, wynik> { res[i] = (res[i] + p.ss) % M; wkladaj(p.ff, p.ss); } cout << "res[" << i << "] = " << res[i] << endl; } */ return 0; } |