#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) ((int)a.size()) template<class T, class U> ostream& operator<<(ostream &stream, pair<T, U> &p) { stream << "(" << p.ST << "," << p.ND << ")"; return stream; } template<class T> ostream& operator<<(ostream &stream, vector<T> &v) { stream << "["; for(auto elem : v) { stream << elem << ", "; } stream << "]"; return stream; } const int mod = 1e9+7; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> a(n), r(n); map<ll, int> m; for(int i=0; i < n; i++) { cin >> a[i] >> r[i]; m[a[i]-r[i]] = 0; m[a[i]] = 0; m[a[i]+r[i]] = 0; } int num = 0; for(auto &x : m) { x.ND = num++; } vector<pii> v(n); vector<int> p(n); for(int i=0; i < n; i++) { v[i] = {m[a[i]-r[i]], m[a[i]+r[i]]}; p[i] = m[a[i]]; } bool flag = true; while(flag) { flag = false; stack<pii> s; for(int i=0; i < n; i++) { while(!s.empty() && v[i].ST <= s.top().ND) { int j = s.top().ST; if(v[j].ST < v[i].ST || v[i].ND < v[j].ND) { v[i] = {min(v[i].ST, v[j].ST), max(v[i].ND, v[j].ND)}; flag = true; } s.pop(); } s.push({i, p[i]}); } while(!s.empty()) { s.pop(); } for(int i=n-1; i >= 0; i--) { while(!s.empty() && s.top().ND <= v[i].ND) { int j = s.top().ST; if(v[j].ST < v[i].ST || v[i].ND < v[j].ND) { v[i] = {min(v[i].ST, v[j].ST), max(v[i].ND, v[j].ND)}; flag = true; } s.pop(); } s.push({i, p[i]}); } } vector<pii> seg(n); for(int i=0; i < n; i++) { int le=0, ri = i; while(le < ri) { int mid = (le+ri)/2; if(p[mid] < v[i].ST) { le=mid+1; } else { ri=mid; } } seg[i].ST = le; le=i, ri=n-1; while(le < ri) { int mid = (le+ri+1)/2; if(v[i].ND < p[mid]) { ri=mid-1; } else { le=mid; } } seg[i].ND = ri; } // cout << seg << "\n"; vector<vector<ll>> dp(n+1, vector<ll>(2, 0)); vector<pii> g(n, {-1, -1}); //parent, end dp[0][0] = 1; int ptr = 0; sort(seg.begin(), seg.end(), [](pii &a, pii &b){return pii(a.ND,a.ST) < pii(b.ND,b.ST);}); for(int i=0; i < n; i++) { dp[i+1][0] = (dp[i][1]+dp[i][0])%mod; dp[i+1][1] = 0; pii s = {-1, -1}; while(ptr < n && seg[ptr].ND == i) { s = seg[ptr]; for(int j=s.ST+1; j <= s.ND+1 && j < n; j++) { if(g[j].ST < s.ST) { g[j].ST = s.ST; } } if(g[s.ST].ST == -1) { for(int j=s.ST-1; j >= 0; j--) { if(g[j].ND >= s.ST-1) { g[s.ST].ST = j; g[s.ST].ND = s.ND; break; } } } ptr++; } while(s.ST != -1) { dp[i+1][1]+=dp[s.ST][0]; s = g[s.ST]; } dp[i+1][1] = (dp[i+1][1])%mod; // cout << i << " " << dp[i+1] << "\n"; } cout << ((dp[n][0]+dp[n][1])%mod+mod)%mod << "\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) ((int)a.size()) template<class T, class U> ostream& operator<<(ostream &stream, pair<T, U> &p) { stream << "(" << p.ST << "," << p.ND << ")"; return stream; } template<class T> ostream& operator<<(ostream &stream, vector<T> &v) { stream << "["; for(auto elem : v) { stream << elem << ", "; } stream << "]"; return stream; } const int mod = 1e9+7; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> a(n), r(n); map<ll, int> m; for(int i=0; i < n; i++) { cin >> a[i] >> r[i]; m[a[i]-r[i]] = 0; m[a[i]] = 0; m[a[i]+r[i]] = 0; } int num = 0; for(auto &x : m) { x.ND = num++; } vector<pii> v(n); vector<int> p(n); for(int i=0; i < n; i++) { v[i] = {m[a[i]-r[i]], m[a[i]+r[i]]}; p[i] = m[a[i]]; } bool flag = true; while(flag) { flag = false; stack<pii> s; for(int i=0; i < n; i++) { while(!s.empty() && v[i].ST <= s.top().ND) { int j = s.top().ST; if(v[j].ST < v[i].ST || v[i].ND < v[j].ND) { v[i] = {min(v[i].ST, v[j].ST), max(v[i].ND, v[j].ND)}; flag = true; } s.pop(); } s.push({i, p[i]}); } while(!s.empty()) { s.pop(); } for(int i=n-1; i >= 0; i--) { while(!s.empty() && s.top().ND <= v[i].ND) { int j = s.top().ST; if(v[j].ST < v[i].ST || v[i].ND < v[j].ND) { v[i] = {min(v[i].ST, v[j].ST), max(v[i].ND, v[j].ND)}; flag = true; } s.pop(); } s.push({i, p[i]}); } } vector<pii> seg(n); for(int i=0; i < n; i++) { int le=0, ri = i; while(le < ri) { int mid = (le+ri)/2; if(p[mid] < v[i].ST) { le=mid+1; } else { ri=mid; } } seg[i].ST = le; le=i, ri=n-1; while(le < ri) { int mid = (le+ri+1)/2; if(v[i].ND < p[mid]) { ri=mid-1; } else { le=mid; } } seg[i].ND = ri; } // cout << seg << "\n"; vector<vector<ll>> dp(n+1, vector<ll>(2, 0)); vector<pii> g(n, {-1, -1}); //parent, end dp[0][0] = 1; int ptr = 0; sort(seg.begin(), seg.end(), [](pii &a, pii &b){return pii(a.ND,a.ST) < pii(b.ND,b.ST);}); for(int i=0; i < n; i++) { dp[i+1][0] = (dp[i][1]+dp[i][0])%mod; dp[i+1][1] = 0; pii s = {-1, -1}; while(ptr < n && seg[ptr].ND == i) { s = seg[ptr]; for(int j=s.ST+1; j <= s.ND+1 && j < n; j++) { if(g[j].ST < s.ST) { g[j].ST = s.ST; } } if(g[s.ST].ST == -1) { for(int j=s.ST-1; j >= 0; j--) { if(g[j].ND >= s.ST-1) { g[s.ST].ST = j; g[s.ST].ND = s.ND; break; } } } ptr++; } while(s.ST != -1) { dp[i+1][1]+=dp[s.ST][0]; s = g[s.ST]; } dp[i+1][1] = (dp[i+1][1])%mod; // cout << i << " " << dp[i+1] << "\n"; } cout << ((dp[n][0]+dp[n][1])%mod+mod)%mod << "\n"; } |