#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 5e5+7, MOD = 1e9+7; int inv(int a, int w = MOD-2){ int res = 1; while(w){ if(w&1) res = (res * a)%MOD; a = (a*a)%MOD; w /= 2; } return res; } vector<int> graf[mxN]; pair<int,int> x[mxN]; int dp[mxN]; bitset<64> bs[mxN]; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; set<pair<int,int>> s; rep(i,1,n) cin >> x[i].st >> x[i].nd; reverse(x+1,x+n+1); rep(i,1,n){ auto [a,b] = x[i]; auto it = s.upper_bound({a,0}); while(it != s.end()){ if((*it).st < a || b < (*it).st) break; graf[i].pb((*it).nd); //cout << (*it).nd << " " << i << "\n"; //cout << i << " " << (*it).nd << "\n"; it = s.erase(it); } s.insert({a, i}); s.insert({b, i}); } rep(i,1,n){ sort(all(graf[i])); graf[i].erase(unique(all(graf[i])), graf[i].end()); } for(int k=1;k<=n;k+=64){ //przedzial od k do k+63 rep(i,1,n) bs[i].reset(); rep(i,1,n){ if(k <= i && i <= k+63) bs[i][i-k] = 1; for(auto j : graf[i]) bs[i] |= bs[j]; dp[i] += bs[i].count(); } } int res = 0; rep(i,1,n) res = (res + inv(dp[i]))%MOD; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 5e5+7, MOD = 1e9+7; int inv(int a, int w = MOD-2){ int res = 1; while(w){ if(w&1) res = (res * a)%MOD; a = (a*a)%MOD; w /= 2; } return res; } vector<int> graf[mxN]; pair<int,int> x[mxN]; int dp[mxN]; bitset<64> bs[mxN]; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; set<pair<int,int>> s; rep(i,1,n) cin >> x[i].st >> x[i].nd; reverse(x+1,x+n+1); rep(i,1,n){ auto [a,b] = x[i]; auto it = s.upper_bound({a,0}); while(it != s.end()){ if((*it).st < a || b < (*it).st) break; graf[i].pb((*it).nd); //cout << (*it).nd << " " << i << "\n"; //cout << i << " " << (*it).nd << "\n"; it = s.erase(it); } s.insert({a, i}); s.insert({b, i}); } rep(i,1,n){ sort(all(graf[i])); graf[i].erase(unique(all(graf[i])), graf[i].end()); } for(int k=1;k<=n;k+=64){ //przedzial od k do k+63 rep(i,1,n) bs[i].reset(); rep(i,1,n){ if(k <= i && i <= k+63) bs[i][i-k] = 1; for(auto j : graf[i]) bs[i] |= bs[j]; dp[i] += bs[i].count(); } } int res = 0; rep(i,1,n) res = (res + inv(dp[i]))%MOD; cout << res << "\n"; } |