#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int mod = 1000000007; const int N = 300300; const int M = 1 << 19; int n; ll a[N], r[N]; int le[N], ri[N], L[M*2+13], R[M*2+13]; int minL[N]; int vis[N]; int DP[N], DPS[N]; int get(int *T, int pos) { return T[pos+M]; } void put(int *T, int pos, int val) { pos += M; T[pos] = val; while (pos > 1) { pos /= 2; T[pos] = max(T[pos*2], T[pos*2+1]); } } int argmax(int *T, int posl, int posr) { posl += M; posr += M+1; int res = posl; while (posl+1 < posr) { if ((posl&1)==0 && T[posl+1]>T[res]) res = posl+1; if ((posr&1)==1 && T[posr-1]>T[res]) res = posr-1; posl /= 2; posr /= 2; } while (res < M) { if (T[res*2] > T[res*2+1]) res = res*2; else res = res*2+1; } return res-M; } void gen_rad() { FORI(i,n) { le[i] = L[i] = lower_bound(a+1, a+n+1, a[i]-r[i]) - a; ri[i] = R[i] = lower_bound(a+1, a+n+1, a[i]+r[i]+1) - a-1; } } void gen_ran() { FORI(i,n) R[M+i] = R[i]; for (int i = M-1; i >= 1; i--) { R[i] = max(R[i*2], R[i*2+1]); } FORI(i,n) if (!vis[i]) { vi path; int j = i; while (!vis[j]) { vis[j] = 1; path.pb(j); j = argmax(R, L[j], get(R,j)); } int ans = get(R,j); for (auto x : path) { put(R,x,ans); } } FORI(i,n) R[i] = R[M+i]; FORI(i,n) vis[i]=0; FORI(i,n) L[M+i] = n+1-L[i]; for (int i = M-1; i >= 1; i--) { L[i] = max(L[i*2], L[i*2+1]); } for (int i = n; i >= 1; i--) if (!vis[i]) { vi path; int j = i; while (!vis[j]) { vis[j] = 1; path.pb(j); j = argmax(L, n+1-get(L,j), R[j]); } int ans = get(L,j); for (auto x : path) { put(L,x,ans); } } FORI(i,n) L[i] = n+1-L[M+i]; } void gen_ran_slow() { FORI(i,n) { //cerr << "i = " << i << "\n"; int lf = le[i], rf = ri[i]; int lc = i, rc = i; while (lc >= lf || rc <= rf) { while (lc >= lf) { //cerr << lf << " " << lc << " " << " | " << rc << " " << rf << " -> l " << lc << "(" << le[lc] << " " << ri[lc] << ")\n"; lf = min(lf, le[lc]); rf = max(rf, ri[lc]); lc--; } while (rc <= rf) { //cerr << lf << " " << lc << " " << " | " << rc << " " << rf << " -> r " << rc << "(" << le[rc] << " " << ri[rc] << ")\n"; lf = min(lf, le[rc]); rf = max(rf, ri[rc]); rc++; } } L[i] = lf; R[i] = rf; } } int main() { scanf("%d", &n); FORI(i,n) scanf("%lld%lld", &a[i], &r[i]); gen_rad(); if (n <= 20000) { gen_ran_slow(); } else { gen_ran(); gen_ran(); gen_ran(); } minL[n] = L[n]; for (int i = n-1; i >= 1; i--) minL[i] = min(L[i], minL[i+1]); DP[n+1] = 1; DPS[n+1] = 1; for (int i = n; i >= 0; i--) { DP[i] = DP[i+1]; for (int j = i+1; j<=n && L[j]>i; j=R[j]+1) { if (minL[j]>i) { DP[i] += DPS[R[j]+1]; if (DP[i] >= mod) DP[i] -= mod; break; } DP[i] += DP[R[j]+1]; if (DP[i] >= mod) DP[i] -= mod; } DPS[i] = (DP[i] + DPS[R[i]+1]) % mod; } //FORI(i,n) cerr << le[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << ri[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << L[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << R[i] << "\t"; cerr << "\n"; //FOR(i,n+1) cerr << DP[i] << "\t"; cerr << "\n"; //FOR(i,n+1) cerr << DPS[i] << "\t"; cerr << "\n"; printf("%d\n", DP[0]); 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int mod = 1000000007; const int N = 300300; const int M = 1 << 19; int n; ll a[N], r[N]; int le[N], ri[N], L[M*2+13], R[M*2+13]; int minL[N]; int vis[N]; int DP[N], DPS[N]; int get(int *T, int pos) { return T[pos+M]; } void put(int *T, int pos, int val) { pos += M; T[pos] = val; while (pos > 1) { pos /= 2; T[pos] = max(T[pos*2], T[pos*2+1]); } } int argmax(int *T, int posl, int posr) { posl += M; posr += M+1; int res = posl; while (posl+1 < posr) { if ((posl&1)==0 && T[posl+1]>T[res]) res = posl+1; if ((posr&1)==1 && T[posr-1]>T[res]) res = posr-1; posl /= 2; posr /= 2; } while (res < M) { if (T[res*2] > T[res*2+1]) res = res*2; else res = res*2+1; } return res-M; } void gen_rad() { FORI(i,n) { le[i] = L[i] = lower_bound(a+1, a+n+1, a[i]-r[i]) - a; ri[i] = R[i] = lower_bound(a+1, a+n+1, a[i]+r[i]+1) - a-1; } } void gen_ran() { FORI(i,n) R[M+i] = R[i]; for (int i = M-1; i >= 1; i--) { R[i] = max(R[i*2], R[i*2+1]); } FORI(i,n) if (!vis[i]) { vi path; int j = i; while (!vis[j]) { vis[j] = 1; path.pb(j); j = argmax(R, L[j], get(R,j)); } int ans = get(R,j); for (auto x : path) { put(R,x,ans); } } FORI(i,n) R[i] = R[M+i]; FORI(i,n) vis[i]=0; FORI(i,n) L[M+i] = n+1-L[i]; for (int i = M-1; i >= 1; i--) { L[i] = max(L[i*2], L[i*2+1]); } for (int i = n; i >= 1; i--) if (!vis[i]) { vi path; int j = i; while (!vis[j]) { vis[j] = 1; path.pb(j); j = argmax(L, n+1-get(L,j), R[j]); } int ans = get(L,j); for (auto x : path) { put(L,x,ans); } } FORI(i,n) L[i] = n+1-L[M+i]; } void gen_ran_slow() { FORI(i,n) { //cerr << "i = " << i << "\n"; int lf = le[i], rf = ri[i]; int lc = i, rc = i; while (lc >= lf || rc <= rf) { while (lc >= lf) { //cerr << lf << " " << lc << " " << " | " << rc << " " << rf << " -> l " << lc << "(" << le[lc] << " " << ri[lc] << ")\n"; lf = min(lf, le[lc]); rf = max(rf, ri[lc]); lc--; } while (rc <= rf) { //cerr << lf << " " << lc << " " << " | " << rc << " " << rf << " -> r " << rc << "(" << le[rc] << " " << ri[rc] << ")\n"; lf = min(lf, le[rc]); rf = max(rf, ri[rc]); rc++; } } L[i] = lf; R[i] = rf; } } int main() { scanf("%d", &n); FORI(i,n) scanf("%lld%lld", &a[i], &r[i]); gen_rad(); if (n <= 20000) { gen_ran_slow(); } else { gen_ran(); gen_ran(); gen_ran(); } minL[n] = L[n]; for (int i = n-1; i >= 1; i--) minL[i] = min(L[i], minL[i+1]); DP[n+1] = 1; DPS[n+1] = 1; for (int i = n; i >= 0; i--) { DP[i] = DP[i+1]; for (int j = i+1; j<=n && L[j]>i; j=R[j]+1) { if (minL[j]>i) { DP[i] += DPS[R[j]+1]; if (DP[i] >= mod) DP[i] -= mod; break; } DP[i] += DP[R[j]+1]; if (DP[i] >= mod) DP[i] -= mod; } DPS[i] = (DP[i] + DPS[R[i]+1]) % mod; } //FORI(i,n) cerr << le[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << ri[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << L[i] << "\t"; cerr << "\n"; //FORI(i,n) cerr << R[i] << "\t"; cerr << "\n"; //FOR(i,n+1) cerr << DP[i] << "\t"; cerr << "\n"; //FOR(i,n+1) cerr << DPS[i] << "\t"; cerr << "\n"; printf("%d\n", DP[0]); return 0; } |