#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define st first #define nd second #define FOR(__VAR, __START, __END) for(int __VAR=__START; __VAR<=__END; __VAR++) #define FORB(__VAR, __START, __END) for(int __VAR=__START; __VAR>=__END; __VAR--) #define FORK(__VAR, __START, __END, __DIFF) for(int __VAR=__START; __VAR<=END; __VAR+=__DIFF) #define all(__VAR) (__VAR).begin(), (__VAR).end() #define rall(__VAR) (__VAR).rbegin(), (__VAR).rend() #define DEBUG(__VAR) cout << #__VAR << ": " << __VAR; template<typename __T1, typename __T2> ostream & operator<<(ostream &out, pair<__T1, __T2> &__VAR) { cout << "[" << __VAR.st << ", " << __VAR.nd << "]"; return out; } template<typename __T> ostream & operator<<(ostream &out, vector<__T> &__VAR) { cout << "["; FOR(i, 0, (int)__VAR.size()-2) cout << __VAR[i] << ", "; if(__VAR.size()>0) cout << __VAR[__VAR.size()-1]; cout << "]" << endl; return out; } const ll INF=2e18, MOD=1e9+7LL; class minmaxTree { //{min,max} na przedziale private: ll treeSize=1LL; vector<pll> v; //0 - min, 1 - max ll query(ll left, ll right, ll cur, ll p, ll q, bool type) { if(right<p || q<left) { if(type) return 0LL; return INF; } if(p<=left && right<=q) { if(type) return v[cur].nd; return v[cur].st; } ll mid=(left+right)/2LL; if(type) return max(query(left,mid,2LL*cur,p,q,type),query(mid+1LL,right,2LL*cur+1LL,p,q,type)); return min(query(left,mid,2LL*cur,p,q,type),query(mid+1LL,right,2LL*cur+1LL,p,q,type)); } public: minmaxTree(ll N) { while(treeSize<N) treeSize*=2LL; v.resize(2LL*treeSize, {INF,0LL}); } void insert(ll pos, pll val) { pos+=treeSize-1LL; v[pos]=val; while(pos/=2LL) { auto [a1,b1]=v[2LL*pos]; auto [a2,b2]=v[2LL*pos+1LL]; v[pos]={min(a1,a2),max(b1,b2)}; } } ll getMin(ll p, ll q) { return query(1LL,treeSize,1LL,p,q,false); } ll getMax(ll p, ll q) { return query(1LL,treeSize,1LL,p,q,true); } }; class segmentTree { private: ll treeSize=1; vector<ll> sum; ll query(ll left, ll right, ll cur, ll p, ll q) { if(right<p || q<left) return 0LL; if(p<=left && right<=q) return sum[cur]; ll mid=(left+right)/2LL; return (query(left,mid,2*cur,p,q)+query(mid+1,right,2*cur+1,p,q))%MOD; } public: segmentTree(ll N) { while(treeSize<N) treeSize*=2LL; sum.resize(2LL*treeSize,0); } void insert(ll pos, ll val) { pos+=treeSize-1LL; sum[pos]=val%MOD; while(pos/=2LL) sum[pos]=(sum[2*pos]+sum[2*pos+1])%MOD; } ll getSum(ll p, ll q) { return query(1,treeSize,1,p,q); } void print() { cout << "TREE: "; for(int i=1; i<2*treeSize; i++) { if((i&-i)==i) cout << endl; cout << sum[i] << ' '; } cout << endl; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; vector<ll> a(n+1, -INF), r(n+1, 0LL); for(int i=1; i<=n; i++) cin >> a[i] >> r[i]; vector<ll> rightRange(n+1, 0LL); minmaxTree rangeTree(n); for(ll i=n; i>0; i--) { auto it=upper_bound(a.begin(), a.end(), a[i]+r[i]); it--; rightRange[i]=max(a[i]+r[i], rangeTree.getMax(i, it-a.begin())); rangeTree.insert(i, {INF,rightRange[i]}); } vector<ll> leftRange(n+1, INF); for(ll i=1; i<=n; i++) { auto it=lower_bound(a.begin()+1, a.end(), a[i]-r[i]); leftRange[i]=min(a[i]-r[i], rangeTree.getMin(it-a.begin(), i)); rangeTree.insert(i, {leftRange[i], 0}); } vector<pll> dp(1, {-INF,0}); for(ll i=1; i<=n; i++) dp.pb({rightRange[i],0}); sort(dp.begin(), dp.end()); auto last=unique(dp.begin(), dp.end()); dp.erase(last, dp.end()); for(ll i=0; i<dp.size(); i++) dp[i].nd=(ll)(i+1); segmentTree tree(dp.size()); vector<ll> res(n+1, 0LL); res[0]=1LL; tree.insert(1, 1); for(ll i=1; i<=n; i++) { auto itDP=lower_bound(dp.begin(), dp.end(), mp(rightRange[i],0LL)); ll j=itDP->nd; //index dp w drzewie; auto it=lower_bound(a.begin(), a.end(), leftRange[i]); it--; ll k=it-a.begin(); //index res tree.insert(j, (res[k]-tree.getSum(j+1, dp.size())+MOD)%MOD); itDP=lower_bound(dp.begin(), dp.end(), mp(a[i],0LL)); itDP--; ll l=itDP->nd; //index pref do res; res[i]=(res[k]+tree.getSum(1,l))%MOD; } cout << res[n] << '\n'; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define st first #define nd second #define FOR(__VAR, __START, __END) for(int __VAR=__START; __VAR<=__END; __VAR++) #define FORB(__VAR, __START, __END) for(int __VAR=__START; __VAR>=__END; __VAR--) #define FORK(__VAR, __START, __END, __DIFF) for(int __VAR=__START; __VAR<=END; __VAR+=__DIFF) #define all(__VAR) (__VAR).begin(), (__VAR).end() #define rall(__VAR) (__VAR).rbegin(), (__VAR).rend() #define DEBUG(__VAR) cout << #__VAR << ": " << __VAR; template<typename __T1, typename __T2> ostream & operator<<(ostream &out, pair<__T1, __T2> &__VAR) { cout << "[" << __VAR.st << ", " << __VAR.nd << "]"; return out; } template<typename __T> ostream & operator<<(ostream &out, vector<__T> &__VAR) { cout << "["; FOR(i, 0, (int)__VAR.size()-2) cout << __VAR[i] << ", "; if(__VAR.size()>0) cout << __VAR[__VAR.size()-1]; cout << "]" << endl; return out; } const ll INF=2e18, MOD=1e9+7LL; class minmaxTree { //{min,max} na przedziale private: ll treeSize=1LL; vector<pll> v; //0 - min, 1 - max ll query(ll left, ll right, ll cur, ll p, ll q, bool type) { if(right<p || q<left) { if(type) return 0LL; return INF; } if(p<=left && right<=q) { if(type) return v[cur].nd; return v[cur].st; } ll mid=(left+right)/2LL; if(type) return max(query(left,mid,2LL*cur,p,q,type),query(mid+1LL,right,2LL*cur+1LL,p,q,type)); return min(query(left,mid,2LL*cur,p,q,type),query(mid+1LL,right,2LL*cur+1LL,p,q,type)); } public: minmaxTree(ll N) { while(treeSize<N) treeSize*=2LL; v.resize(2LL*treeSize, {INF,0LL}); } void insert(ll pos, pll val) { pos+=treeSize-1LL; v[pos]=val; while(pos/=2LL) { auto [a1,b1]=v[2LL*pos]; auto [a2,b2]=v[2LL*pos+1LL]; v[pos]={min(a1,a2),max(b1,b2)}; } } ll getMin(ll p, ll q) { return query(1LL,treeSize,1LL,p,q,false); } ll getMax(ll p, ll q) { return query(1LL,treeSize,1LL,p,q,true); } }; class segmentTree { private: ll treeSize=1; vector<ll> sum; ll query(ll left, ll right, ll cur, ll p, ll q) { if(right<p || q<left) return 0LL; if(p<=left && right<=q) return sum[cur]; ll mid=(left+right)/2LL; return (query(left,mid,2*cur,p,q)+query(mid+1,right,2*cur+1,p,q))%MOD; } public: segmentTree(ll N) { while(treeSize<N) treeSize*=2LL; sum.resize(2LL*treeSize,0); } void insert(ll pos, ll val) { pos+=treeSize-1LL; sum[pos]=val%MOD; while(pos/=2LL) sum[pos]=(sum[2*pos]+sum[2*pos+1])%MOD; } ll getSum(ll p, ll q) { return query(1,treeSize,1,p,q); } void print() { cout << "TREE: "; for(int i=1; i<2*treeSize; i++) { if((i&-i)==i) cout << endl; cout << sum[i] << ' '; } cout << endl; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; vector<ll> a(n+1, -INF), r(n+1, 0LL); for(int i=1; i<=n; i++) cin >> a[i] >> r[i]; vector<ll> rightRange(n+1, 0LL); minmaxTree rangeTree(n); for(ll i=n; i>0; i--) { auto it=upper_bound(a.begin(), a.end(), a[i]+r[i]); it--; rightRange[i]=max(a[i]+r[i], rangeTree.getMax(i, it-a.begin())); rangeTree.insert(i, {INF,rightRange[i]}); } vector<ll> leftRange(n+1, INF); for(ll i=1; i<=n; i++) { auto it=lower_bound(a.begin()+1, a.end(), a[i]-r[i]); leftRange[i]=min(a[i]-r[i], rangeTree.getMin(it-a.begin(), i)); rangeTree.insert(i, {leftRange[i], 0}); } vector<pll> dp(1, {-INF,0}); for(ll i=1; i<=n; i++) dp.pb({rightRange[i],0}); sort(dp.begin(), dp.end()); auto last=unique(dp.begin(), dp.end()); dp.erase(last, dp.end()); for(ll i=0; i<dp.size(); i++) dp[i].nd=(ll)(i+1); segmentTree tree(dp.size()); vector<ll> res(n+1, 0LL); res[0]=1LL; tree.insert(1, 1); for(ll i=1; i<=n; i++) { auto itDP=lower_bound(dp.begin(), dp.end(), mp(rightRange[i],0LL)); ll j=itDP->nd; //index dp w drzewie; auto it=lower_bound(a.begin(), a.end(), leftRange[i]); it--; ll k=it-a.begin(); //index res tree.insert(j, (res[k]-tree.getSum(j+1, dp.size())+MOD)%MOD); itDP=lower_bound(dp.begin(), dp.end(), mp(a[i],0LL)); itDP--; ll l=itDP->nd; //index pref do res; res[i]=(res[k]+tree.getSum(1,l))%MOD; } cout << res[n] << '\n'; return 0; } |