#pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(1) /////////////////// const int MAXN=3e5+10,base=(1<<19),mod=1e9+7; int t[base*2],l[MAXN],r[MAXN],dp[MAXN]; ll a[MAXN],b[MAXN]; vector<pair<int,int>> mx,mn; void update(int u,int v){ u+=base; while(u){ t[u]+=v; if(t[u]>=mod) t[u]-=mod; if(t[u]<0) t[u]+=mod; u/=2; } } int get(int l,int r){ l+=base; r+=base; int res=t[l]; if(l!=r){ res+=t[r]; if(res>=mod) res-=mod; } while(l/2!=r/2){ if(l%2==0){ res+=t[l+1]; if(res>=mod) res-=mod; } if(r%2==1){ res+=t[r-1]; if(res>=mod) res-=mod; } l/=2; r/=2; } return res; } void rem(int lo,int hi,int val){ if(val>=lo&&val<=hi){ update(val,-dp[val-1]); } } void add(int lo,int hi,int val){ if(val>=lo&&val<=hi){ update(val,dp[val-1]); } } int32_t main() { fastio; int n,lo,hi,mid,ans; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]>>b[i]; for(int i=1;i<=n;i++){ lo=1,hi=i; l[i]=i; while(lo<=hi){ mid=(lo+hi)/2; if(a[mid]>=a[i]-b[i]) l[i]=mid,hi=mid-1; else lo=mid+1; } lo=i,hi=n; r[i]=i; while(lo<=hi){ mid=(lo+hi)/2; if(a[mid]<=a[i]+b[i]) r[i]=mid,lo=mid+1; else hi=mid-1; } } dp[0]=1; mn.eb(mp(0,0)); mx.eb(mp(mod,0)); for(int i=1;i<=n;i++){ dp[i]=dp[i-1]; if(i>1){ lo=0,hi=size(mx)-1; ans=-1; while(lo<=hi){ mid=(lo+hi)/2; if(mx[mid].e1<i) ans=mid,hi=mid-1; else lo=mid+1; } if(ans!=-1){ dp[i]+=get(mx[ans-1].e2+1,i); if(dp[i]>=mod) dp[i]-=mod; } } while(mn.back().e1>=l[i]){ rem(mn[size(mn)-2].e2+1,mn.back().e2,mn.back().e1); mn.pop_back(); } while(mx.back().e1<=r[i]){ mx.pop_back(); } mx.eb(mp(r[i],i)); add(mn.back().e2+1,i,l[i]); mn.eb(mp(l[i],i)); } int res=0; int maxl=n+1; for(int i=n;i>=0;i--){ if(maxl==i+1){ res+=dp[i]; if(res>=mod) res-=mod; } maxl=min(maxl,l[i]); } cout<<res<<'\n'; }
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 | #pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(1) /////////////////// const int MAXN=3e5+10,base=(1<<19),mod=1e9+7; int t[base*2],l[MAXN],r[MAXN],dp[MAXN]; ll a[MAXN],b[MAXN]; vector<pair<int,int>> mx,mn; void update(int u,int v){ u+=base; while(u){ t[u]+=v; if(t[u]>=mod) t[u]-=mod; if(t[u]<0) t[u]+=mod; u/=2; } } int get(int l,int r){ l+=base; r+=base; int res=t[l]; if(l!=r){ res+=t[r]; if(res>=mod) res-=mod; } while(l/2!=r/2){ if(l%2==0){ res+=t[l+1]; if(res>=mod) res-=mod; } if(r%2==1){ res+=t[r-1]; if(res>=mod) res-=mod; } l/=2; r/=2; } return res; } void rem(int lo,int hi,int val){ if(val>=lo&&val<=hi){ update(val,-dp[val-1]); } } void add(int lo,int hi,int val){ if(val>=lo&&val<=hi){ update(val,dp[val-1]); } } int32_t main() { fastio; int n,lo,hi,mid,ans; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]>>b[i]; for(int i=1;i<=n;i++){ lo=1,hi=i; l[i]=i; while(lo<=hi){ mid=(lo+hi)/2; if(a[mid]>=a[i]-b[i]) l[i]=mid,hi=mid-1; else lo=mid+1; } lo=i,hi=n; r[i]=i; while(lo<=hi){ mid=(lo+hi)/2; if(a[mid]<=a[i]+b[i]) r[i]=mid,lo=mid+1; else hi=mid-1; } } dp[0]=1; mn.eb(mp(0,0)); mx.eb(mp(mod,0)); for(int i=1;i<=n;i++){ dp[i]=dp[i-1]; if(i>1){ lo=0,hi=size(mx)-1; ans=-1; while(lo<=hi){ mid=(lo+hi)/2; if(mx[mid].e1<i) ans=mid,hi=mid-1; else lo=mid+1; } if(ans!=-1){ dp[i]+=get(mx[ans-1].e2+1,i); if(dp[i]>=mod) dp[i]-=mod; } } while(mn.back().e1>=l[i]){ rem(mn[size(mn)-2].e2+1,mn.back().e2,mn.back().e1); mn.pop_back(); } while(mx.back().e1<=r[i]){ mx.pop_back(); } mx.eb(mp(r[i],i)); add(mn.back().e2+1,i,l[i]); mn.eb(mp(l[i],i)); } int res=0; int maxl=n+1; for(int i=n;i>=0;i--){ if(maxl==i+1){ res+=dp[i]; if(res>=mod) res-=mod; } maxl=min(maxl,l[i]); } cout<<res<<'\n'; } |