#include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=1e6+7, LG=20; pair<int,int>T[LIM]; int lewo[LIM], prawo[LIM], ilelewo[LIM], ileprawo[LIM], kto[LIM], tr[4*LIM], pokrywa[LIM], nxt[LIM][LG], odl[LIM], N=1; int ile[LIM]; ll pot(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; a=(a*a)%MOD; b/=2; } return ans; } void upd(int l, int r, int x) { l+=N; r+=N; tr[l]=min(tr[l], x); tr[r]=min(tr[r], x); while(l/2!=r/2) { if(l%2==0) tr[l+1]=min(tr[l+1], x); if(r%2==1) tr[r-1]=min(tr[r-1], x); l/=2; r/=2; } } int cnt(int v) { v+=N; int ans=tr[v]; while(v) { ans=min(ans, tr[v]); v/=2; } return ans; } int lca(int a, int b) { for(int i=LG-1; i>=0; --i) if(odl[nxt[a][i]]>=odl[b]) a=nxt[a][i]; for(int i=LG-1; i>=0; --i) if(odl[nxt[b][i]]>=odl[a]) b=nxt[b][i]; if(a==b) return a; for(int i=LG-1; i>=0; --i) if(nxt[a][i]!=nxt[b][i]) { a=nxt[a][i]; b=nxt[b][i]; } return nxt[a][0]; } void upd2(int v) { v+=N; while(v) { ++tr[v]; v/=2; } } int cnt2(int l, int r) { if(l>r) return 0; l+=N; r+=N; int ans=tr[l]; if(l!=r) ans+=tr[r]; while(l/2!=r/2) { if(l%2==0) ans+=tr[l+1]; if(r%2==1) ans+=tr[r-1]; l/=2; r/=2; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; while(N<2*n+2) N*=2; rep(i, n) cin >> T[i].st >> T[i].nd; T[n]={0, 2*n+1}; rep(i, n+1) kto[T[i].st]=kto[T[i].nd]=i; rep(i, 2*N) tr[i]=n; rep(i, LG) nxt[n][i]=n; for(int i=n-1; i>=0; --i) { lewo[i]=cnt(T[i].st); prawo[i]=cnt(T[i].nd); upd(T[i].st, T[i].nd, i); nxt[i][0]=lca(lewo[i], prawo[i]); for(int j=1; j<LG; ++j) nxt[i][j]=nxt[nxt[i][j-1]][j-1]; odl[i]=odl[nxt[i][0]]+1; } rep(i, 2*N) tr[i]=0; rep(i, 2*n+2) { if(T[kto[i]].nd==i) { upd2(kto[i]); continue; } ilelewo[kto[i]]=cnt2(kto[i], lewo[kto[i]]); } rep(i, 2*N) tr[i]=0; for(int i=2*n+1; i>=0; --i) { if(T[kto[i]].st==i) { upd2(kto[i]); continue; } ileprawo[kto[i]]=cnt2(kto[i], prawo[kto[i]]); } for(int i=n-1; i>=0; --i) { ilelewo[i]+=ilelewo[lewo[i]]; ileprawo[i]+=ileprawo[prawo[i]]; ile[i]=nxt[i][0]-i-1-(ilelewo[i]-ilelewo[nxt[i][0]])-(ileprawo[i]-ileprawo[nxt[i][0]]); } ll ans=0; rep(i, n) ans=(ans+pot(ile[i]+1, MOD-2))%MOD; 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 | #include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=1e6+7, LG=20; pair<int,int>T[LIM]; int lewo[LIM], prawo[LIM], ilelewo[LIM], ileprawo[LIM], kto[LIM], tr[4*LIM], pokrywa[LIM], nxt[LIM][LG], odl[LIM], N=1; int ile[LIM]; ll pot(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; a=(a*a)%MOD; b/=2; } return ans; } void upd(int l, int r, int x) { l+=N; r+=N; tr[l]=min(tr[l], x); tr[r]=min(tr[r], x); while(l/2!=r/2) { if(l%2==0) tr[l+1]=min(tr[l+1], x); if(r%2==1) tr[r-1]=min(tr[r-1], x); l/=2; r/=2; } } int cnt(int v) { v+=N; int ans=tr[v]; while(v) { ans=min(ans, tr[v]); v/=2; } return ans; } int lca(int a, int b) { for(int i=LG-1; i>=0; --i) if(odl[nxt[a][i]]>=odl[b]) a=nxt[a][i]; for(int i=LG-1; i>=0; --i) if(odl[nxt[b][i]]>=odl[a]) b=nxt[b][i]; if(a==b) return a; for(int i=LG-1; i>=0; --i) if(nxt[a][i]!=nxt[b][i]) { a=nxt[a][i]; b=nxt[b][i]; } return nxt[a][0]; } void upd2(int v) { v+=N; while(v) { ++tr[v]; v/=2; } } int cnt2(int l, int r) { if(l>r) return 0; l+=N; r+=N; int ans=tr[l]; if(l!=r) ans+=tr[r]; while(l/2!=r/2) { if(l%2==0) ans+=tr[l+1]; if(r%2==1) ans+=tr[r-1]; l/=2; r/=2; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; while(N<2*n+2) N*=2; rep(i, n) cin >> T[i].st >> T[i].nd; T[n]={0, 2*n+1}; rep(i, n+1) kto[T[i].st]=kto[T[i].nd]=i; rep(i, 2*N) tr[i]=n; rep(i, LG) nxt[n][i]=n; for(int i=n-1; i>=0; --i) { lewo[i]=cnt(T[i].st); prawo[i]=cnt(T[i].nd); upd(T[i].st, T[i].nd, i); nxt[i][0]=lca(lewo[i], prawo[i]); for(int j=1; j<LG; ++j) nxt[i][j]=nxt[nxt[i][j-1]][j-1]; odl[i]=odl[nxt[i][0]]+1; } rep(i, 2*N) tr[i]=0; rep(i, 2*n+2) { if(T[kto[i]].nd==i) { upd2(kto[i]); continue; } ilelewo[kto[i]]=cnt2(kto[i], lewo[kto[i]]); } rep(i, 2*N) tr[i]=0; for(int i=2*n+1; i>=0; --i) { if(T[kto[i]].st==i) { upd2(kto[i]); continue; } ileprawo[kto[i]]=cnt2(kto[i], prawo[kto[i]]); } for(int i=n-1; i>=0; --i) { ilelewo[i]+=ilelewo[lewo[i]]; ileprawo[i]+=ileprawo[prawo[i]]; ile[i]=nxt[i][0]-i-1-(ilelewo[i]-ilelewo[nxt[i][0]])-(ileprawo[i]-ileprawo[nxt[i][0]]); } ll ans=0; rep(i, n) ans=(ans+pot(ile[i]+1, MOD-2))%MOD; cout << ans << '\n'; } |