#include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int bs = 1<<20; int mx[bs<<1]; void update(int p, int k, int val) { for(p+=bs-1,k+=bs+1;p+1!=k;p/=2,k/=2) { if((p&1)==0) mx[p+1] = val; if((k&1)==1) mx[k-1] = val; } } int query(int ind) { int r = mx[ind+=bs]; for(ind/=2;ind;ind/=2) { r = max(r,mx[ind]); } return r; } const int P = 1e9+7; ll mul(ll a, ll b) { return a*b%P; } const int C = 1024; int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n; cin >> n; vector<ll> inv(n+1); inv[1] = 1; forn(i,2,n) inv[i] = mul(P/i,P-inv[P%i]); vector<vector<int>> W(n); forn(i,1,n) { int l,r; cin >> l >> r; int L = query(l); int R = query(r); update(l,r,i); if(L) W[L-1].push_back(i-1); if(R) W[R-1].push_back(i-1); } vector<bitset<C>> dp(n, 0); Vi cnt(n, 0); for(int b = 0; b < n; b += C) { forr(i,n,0) { dp[i] = 0; if(b <= i && i < b + C) dp[i][i-b] = 1; for(auto v : W[i]) dp[i] |= dp[v]; } rep(i,0,n) cnt[i] += dp[i].count(); } for(auto&v:cnt) v = inv[v]; cout << accumulate(all(cnt),0LL) % P << '\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 | #include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int bs = 1<<20; int mx[bs<<1]; void update(int p, int k, int val) { for(p+=bs-1,k+=bs+1;p+1!=k;p/=2,k/=2) { if((p&1)==0) mx[p+1] = val; if((k&1)==1) mx[k-1] = val; } } int query(int ind) { int r = mx[ind+=bs]; for(ind/=2;ind;ind/=2) { r = max(r,mx[ind]); } return r; } const int P = 1e9+7; ll mul(ll a, ll b) { return a*b%P; } const int C = 1024; int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n; cin >> n; vector<ll> inv(n+1); inv[1] = 1; forn(i,2,n) inv[i] = mul(P/i,P-inv[P%i]); vector<vector<int>> W(n); forn(i,1,n) { int l,r; cin >> l >> r; int L = query(l); int R = query(r); update(l,r,i); if(L) W[L-1].push_back(i-1); if(R) W[R-1].push_back(i-1); } vector<bitset<C>> dp(n, 0); Vi cnt(n, 0); for(int b = 0; b < n; b += C) { forr(i,n,0) { dp[i] = 0; if(b <= i && i < b + C) dp[i][i-b] = 1; for(auto v : W[i]) dp[i] |= dp[v]; } rep(i,0,n) cnt[i] += dp[i].count(); } for(auto&v:cnt) v = inv[v]; cout << accumulate(all(cnt),0LL) % P << '\n'; return 0; } |