#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; struct LCA { vi par,d,jmp,in,out; vector<basic_string<int>> adj; int root; LCA(int n,int root=0) : par(n,root),d(n),jmp(n,root),in(n),out(n),adj(n),root(root) {} void add(int i) { int p = par[i]; d[i]=1+d[p]; if(d[p] - d[jmp[p]] == d[jmp[p]] - d[jmp[jmp[p]]]) jmp[i] = jmp[jmp[p]]; else jmp[i] = p; } int jump(int a, int k) { int D = max(0,d[a]-k); while(d[a]>D) { if(d[jmp[a]]>=D) a = jmp[a]; else a = par[a]; } return a; } template<typename F> pi find(int a, F f) { int steps=d[a]; while(!f(a)) { if(!f(jmp[a])) a = jmp[a]; else a = par[a]; } steps-=d[a]-1; return {a,steps}; } int lca(int a, int b) { if(d[a]<d[b]) swap(a,b); a = jump(a,d[a]-d[b]); while(a!=b) { if(jmp[a]!=jmp[b]) a=jmp[a],b=jmp[b]; else a=par[a],b=par[b]; } return a; } void addE(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } int cnt=0; void dfs(int at) { in[at]=cnt++; for(int to: adj[at]) if(to!=par[at]) { par[to]=at; add(to); dfs(to); } out[at]=cnt; } void init() { dfs(root); } }; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<array<int,2>> lr(n),par(n+1); for(auto& [l,r] : lr) cin >> l >> r; reverse(all(lr)); lr.push_back({-oo,oo}); map<int,pair<int,bool>> mp; n++; for(int i=0;i<n;++i) { auto [l,r] = lr[i]; auto it = mp.lower_bound(l); while(it!=mp.end() and it->first<=r) { auto [v,b] = it->second; par[v][b]=i; it = mp.erase(it); } mp[l]={i,0}; mp[r]={i,1}; } LCA lcal(n,n-1),lcar(n,n-1); for(int i=0;i<n-1;++i) { lcal.addE(i,par[i][0]); lcar.addE(i,par[i][1]); } lcal.init(); lcar.init(); vector<unsigned int> reaches(n); for(int i=0;i<n-1;++i) { reaches[i]++; reaches[par[i][0]]+=reaches[i]; reaches[par[i][1]]+=reaches[i]; int u=par[i][0],v=par[i][1]; while(lr[u][1]<lr[v][0]) { int ju = lcar.jmp[u], jv = lcal.jmp[v]; if(lr[ju][1]<lr[jv][0]) { if(ju<jv) { u=ju; } else v=jv; } else if(ju<v and lr[ju][1]<lr[v][0]) { u=ju; } else if(jv<u and lr[u][1]<lr[jv][0]) { v=jv; } else if(u<v) { u=lcar.par[u]; } else v = lcal.par[v]; } reaches[max(u,v)]-=reaches[i]; // not double count. } mint ans=0; for(int i=0;i<n-1;++i) { ans+=mint(1)/(reaches[i]); // reaches[i]--; } cout << ans << '\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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; struct LCA { vi par,d,jmp,in,out; vector<basic_string<int>> adj; int root; LCA(int n,int root=0) : par(n,root),d(n),jmp(n,root),in(n),out(n),adj(n),root(root) {} void add(int i) { int p = par[i]; d[i]=1+d[p]; if(d[p] - d[jmp[p]] == d[jmp[p]] - d[jmp[jmp[p]]]) jmp[i] = jmp[jmp[p]]; else jmp[i] = p; } int jump(int a, int k) { int D = max(0,d[a]-k); while(d[a]>D) { if(d[jmp[a]]>=D) a = jmp[a]; else a = par[a]; } return a; } template<typename F> pi find(int a, F f) { int steps=d[a]; while(!f(a)) { if(!f(jmp[a])) a = jmp[a]; else a = par[a]; } steps-=d[a]-1; return {a,steps}; } int lca(int a, int b) { if(d[a]<d[b]) swap(a,b); a = jump(a,d[a]-d[b]); while(a!=b) { if(jmp[a]!=jmp[b]) a=jmp[a],b=jmp[b]; else a=par[a],b=par[b]; } return a; } void addE(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } int cnt=0; void dfs(int at) { in[at]=cnt++; for(int to: adj[at]) if(to!=par[at]) { par[to]=at; add(to); dfs(to); } out[at]=cnt; } void init() { dfs(root); } }; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<array<int,2>> lr(n),par(n+1); for(auto& [l,r] : lr) cin >> l >> r; reverse(all(lr)); lr.push_back({-oo,oo}); map<int,pair<int,bool>> mp; n++; for(int i=0;i<n;++i) { auto [l,r] = lr[i]; auto it = mp.lower_bound(l); while(it!=mp.end() and it->first<=r) { auto [v,b] = it->second; par[v][b]=i; it = mp.erase(it); } mp[l]={i,0}; mp[r]={i,1}; } LCA lcal(n,n-1),lcar(n,n-1); for(int i=0;i<n-1;++i) { lcal.addE(i,par[i][0]); lcar.addE(i,par[i][1]); } lcal.init(); lcar.init(); vector<unsigned int> reaches(n); for(int i=0;i<n-1;++i) { reaches[i]++; reaches[par[i][0]]+=reaches[i]; reaches[par[i][1]]+=reaches[i]; int u=par[i][0],v=par[i][1]; while(lr[u][1]<lr[v][0]) { int ju = lcar.jmp[u], jv = lcal.jmp[v]; if(lr[ju][1]<lr[jv][0]) { if(ju<jv) { u=ju; } else v=jv; } else if(ju<v and lr[ju][1]<lr[v][0]) { u=ju; } else if(jv<u and lr[u][1]<lr[jv][0]) { v=jv; } else if(u<v) { u=lcar.par[u]; } else v = lcal.par[v]; } reaches[max(u,v)]-=reaches[i]; // not double count. } mint ans=0; for(int i=0;i<n-1;++i) { ans+=mint(1)/(reaches[i]); // reaches[i]--; } cout << ans << '\n'; } |