#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head // note : [l ,r] means a[l+1 ... r] here struct segcover { map<PII,int> sg; int L_,R_; void init(int L,int R) { sg[{L-1,L-1}]=-1; sg[{R+1,R+1}]=-1; L_=L-1; R_=R+1; } int query(int x) { auto pl=--sg.lower_bound({x,L_}); return pl->se; } void add(int l,int r,int id,function<void(int,int,int)>delseg=[](int l,int r,int id) {}, function<void(int,int,int)> addseg=[](int l,int r,int id) {}) { auto split=[&](int p) { auto pl=--sg.lower_bound({p,L_}); if (pl->fi.se>p) { int l=pl->fi.fi,r=pl->fi.se,id=pl->se; sg.erase(pl); sg[{l,p}]=id; sg[{p,r}]=id; } }; split(l); split(r); auto pl=sg.lower_bound({l,L_}); while (1) { auto pr=pl; ++pr; if (pl->fi.fi>=r) break; delseg(pl->fi.fi,pl->fi.se,pl->se); sg.erase(pl); pl=pr; } addseg(l,r,id); sg[{l,r}]=id; } }oo; const int N=501000; int n,a[N],b[N],L[N],R[N],dp[N],cc[N],st; ll ret=0; int main() { scanf("%d",&n); oo.init(-2,2*n+2); oo.add(-1,2*n+1,0); rep(i,1,n+1) { scanf("%d%d",&a[i],&b[i]); L[i]=oo.query(a[i]); R[i]=oo.query(b[i]); oo.add(a[i],b[i],i); } per(i,1,n+1) { cc[i]++; int u=L[i],v=R[i]; cc[L[i]]+=cc[i]; cc[R[i]]+=cc[i]; while (u!=v) { if (u>v) u=R[u]; else v=L[v]; st++; } cc[u]-=cc[i]; ret+=powmod(cc[i],mod-2); //printf("%d %d\n",i,cc[i]); } printf("%lld\n",ret%mod); //fprintf(stderr,"%d\n",st); }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head // note : [l ,r] means a[l+1 ... r] here struct segcover { map<PII,int> sg; int L_,R_; void init(int L,int R) { sg[{L-1,L-1}]=-1; sg[{R+1,R+1}]=-1; L_=L-1; R_=R+1; } int query(int x) { auto pl=--sg.lower_bound({x,L_}); return pl->se; } void add(int l,int r,int id,function<void(int,int,int)>delseg=[](int l,int r,int id) {}, function<void(int,int,int)> addseg=[](int l,int r,int id) {}) { auto split=[&](int p) { auto pl=--sg.lower_bound({p,L_}); if (pl->fi.se>p) { int l=pl->fi.fi,r=pl->fi.se,id=pl->se; sg.erase(pl); sg[{l,p}]=id; sg[{p,r}]=id; } }; split(l); split(r); auto pl=sg.lower_bound({l,L_}); while (1) { auto pr=pl; ++pr; if (pl->fi.fi>=r) break; delseg(pl->fi.fi,pl->fi.se,pl->se); sg.erase(pl); pl=pr; } addseg(l,r,id); sg[{l,r}]=id; } }oo; const int N=501000; int n,a[N],b[N],L[N],R[N],dp[N],cc[N],st; ll ret=0; int main() { scanf("%d",&n); oo.init(-2,2*n+2); oo.add(-1,2*n+1,0); rep(i,1,n+1) { scanf("%d%d",&a[i],&b[i]); L[i]=oo.query(a[i]); R[i]=oo.query(b[i]); oo.add(a[i],b[i],i); } per(i,1,n+1) { cc[i]++; int u=L[i],v=R[i]; cc[L[i]]+=cc[i]; cc[R[i]]+=cc[i]; while (u!=v) { if (u>v) u=R[u]; else v=L[v]; st++; } cc[u]-=cc[i]; ret+=powmod(cc[i],mod-2); //printf("%d %d\n",i,cc[i]); } printf("%lld\n",ret%mod); //fprintf(stderr,"%d\n",st); } |