#include <bits/stdc++.h> #define ll long long #define st first #define nd second #define pb push_back const int N=300005, mod=1e9+7; using namespace std; vector<int> V[N], U[N]; int n; vector<pair<ll, ll> > miny; ll ans=1; void bg(int x){ int y=x-1; while(y>=0&&miny[y].st>=miny[x].st-miny[x].nd){ V[x].pb(y); U[y].pb(x); --y; } y=x+1; while(y<n&&miny[y].st<=miny[x].st+miny[x].nd){ V[x].pb(y); U[y].pb(x); ++y; } } stack<int> s; int odw[N], skl[N], nr=0, cur=1; void dfs(int x){ odw[x]=1; for(int y: V[x]){ if(!odw[y]) dfs(y); } s.push(x); } void sfd(int x, int nr){ odw[x]=2; skl[x]=nr; for(int y: U[x]){ if(odw[y]<2) sfd(y, nr); } } vector<int> S[N]; void mrg(int x){ odw[x]=3; for(int y: V[x]){ if(skl[x]!=skl[y]){ S[skl[x]].push_back(skl[y]); } if(odw[y]<3) mrg(y); } } int vis[N]; vector<int> topsort; void tsort(int x){ vis[x]=1; for(int y: S[x]){ if(!vis[y]) tsort(y); } topsort.push_back(x); } int ile[N],dp[N]; void DFS(int x){ vis[x]=2; for(int y: S[x]){ dp[y]=max(dp[y], dp[y]*(ile[x]+1)%mod); cur=max(cur, dp[y]); if(vis[y]<2){ ile[y]=ile[x]+1; DFS(y); } } } int main() { ios_base::sync_with_stdio(0); cin>>n; for(int i=1; i<=n; ++i){ ll a, r; cin>>a>>r; miny.pb({a, r}); } sort(miny.begin(), miny.end()); for(int i=0; i<n; ++i) bg(i); for(int i=0; i<n; ++i){ if(!odw[i]) dfs(i); } while(!s.empty()){ int p=s.top(); s.pop(); if(odw[p]<2){ nr++; sfd(p, nr); } } for(int i=1; i<=n; ++i){ if(odw[i]<3) mrg(i); } for(int i=1; i<=nr; ++i){ dp[i]=1; ile[i]=1; } for(int i=1; i<=nr; ++i){ if(!vis[i]){ tsort(i); } } for(int i=topsort.size()-1; i>=0; --i){ cur=1; if(vis[topsort[i]]<2){ DFS(topsort[i]); if(S[topsort[i]].size()==0) cur=2; ans=ans*cur%mod; } } cout<<ans+1; 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 | #include <bits/stdc++.h> #define ll long long #define st first #define nd second #define pb push_back const int N=300005, mod=1e9+7; using namespace std; vector<int> V[N], U[N]; int n; vector<pair<ll, ll> > miny; ll ans=1; void bg(int x){ int y=x-1; while(y>=0&&miny[y].st>=miny[x].st-miny[x].nd){ V[x].pb(y); U[y].pb(x); --y; } y=x+1; while(y<n&&miny[y].st<=miny[x].st+miny[x].nd){ V[x].pb(y); U[y].pb(x); ++y; } } stack<int> s; int odw[N], skl[N], nr=0, cur=1; void dfs(int x){ odw[x]=1; for(int y: V[x]){ if(!odw[y]) dfs(y); } s.push(x); } void sfd(int x, int nr){ odw[x]=2; skl[x]=nr; for(int y: U[x]){ if(odw[y]<2) sfd(y, nr); } } vector<int> S[N]; void mrg(int x){ odw[x]=3; for(int y: V[x]){ if(skl[x]!=skl[y]){ S[skl[x]].push_back(skl[y]); } if(odw[y]<3) mrg(y); } } int vis[N]; vector<int> topsort; void tsort(int x){ vis[x]=1; for(int y: S[x]){ if(!vis[y]) tsort(y); } topsort.push_back(x); } int ile[N],dp[N]; void DFS(int x){ vis[x]=2; for(int y: S[x]){ dp[y]=max(dp[y], dp[y]*(ile[x]+1)%mod); cur=max(cur, dp[y]); if(vis[y]<2){ ile[y]=ile[x]+1; DFS(y); } } } int main() { ios_base::sync_with_stdio(0); cin>>n; for(int i=1; i<=n; ++i){ ll a, r; cin>>a>>r; miny.pb({a, r}); } sort(miny.begin(), miny.end()); for(int i=0; i<n; ++i) bg(i); for(int i=0; i<n; ++i){ if(!odw[i]) dfs(i); } while(!s.empty()){ int p=s.top(); s.pop(); if(odw[p]<2){ nr++; sfd(p, nr); } } for(int i=1; i<=n; ++i){ if(odw[i]<3) mrg(i); } for(int i=1; i<=nr; ++i){ dp[i]=1; ile[i]=1; } for(int i=1; i<=nr; ++i){ if(!vis[i]){ tsort(i); } } for(int i=topsort.size()-1; i>=0; --i){ cur=1; if(vis[topsort[i]]<2){ DFS(topsort[i]); if(S[topsort[i]].size()==0) cur=2; ans=ans*cur%mod; } } cout<<ans+1; return 0; } |