#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e5+100; const int K = 20; const int MOD = 1e9+7; const int INF = 1e9; struct segtree { vector<int> t, lz; void init(int sz) { t.assign(4*sz+10, 0); lz.assign(4*sz+10, -1); } void push(int v) { lz[2*v]=lz[v], lz[2*v+1]=lz[v]; t[2*v]=lz[v], t[2*v+1]=lz[v]; lz[v]=-1; } void upd(int v, int l, int r, int ql, int qr, int vl) { if (l > qr || r < ql) return; if (l >= ql && r <= qr) { lz[v]=vl; t[v]=vl; if (l < r) push(v); return; } int m=(l+r)/2; if (lz[v] != -1) push(v); upd(2*v, l, m, ql, qr, vl), upd(2*v+1, m+1, r, ql, qr, vl); } int que(int v, int l, int r, int p) { if (l == r) return t[v]; int m=(l+r)/2; if (lz[v] != -1) push(v); if (p <= m) return que(2*v, l, m, p); return que(2*v+1, m+1, r, p); } }; ll n, l[N], r[N], f[N], upl[K][N], upr[K][N], pf[N], indeg[N]; pii g[N]; ll bpw(ll x, ll n) { if (n == 0) return 1; ll c=bpw(x, n/2); c=(c*c)%MOD; if (n&1) return (c*x)%MOD; return c; } int mv(int rx, int lp) { if (lp > rx) return rx; for (int i=K-1; i>=0; --i) { if (upl[i][rx] > lp) rx=upl[i][rx]; } return rx; } int com(int lx, int rx) { if (lx == rx) return lx; int lc=lx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > rx) lc=upr[j][lc]; } if (upr[0][lc] == rx) return rx; int rc=rx; for (int j=K-1; j>=0; --j) { if (upl[j][rc] > lx) rc=upl[j][rc]; } if (upl[0][rc] == lx) return lx; for (int i=K-1; i>=0; --i) { int lp=upr[i][lx]; int x=mv(rx, lp); if (x == lp) continue; if (r[lp] < l[x]) lx=lp; } return upr[0][lx]; } int com2(int lx, int rx) { if (lx == rx) return lx; int lc=lx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > rx) lc=upr[j][lc]; } if (upr[0][lc] == rx) return rx; int rc=rx; for (int j=K-1; j>=0; --j) { if (upl[j][rc] > lx) rc=upl[j][rc]; } if (upl[0][rc] == lx) return lx; for (int i=n; i>=1; --i) { int lc=lx, rc=rx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > i) lc=upr[j][lc]; if (upl[j][rc] > i) rc=upl[j][rc]; } if (upr[0][lc] == upl[0][rc] && upr[0][lc] == i) return i; } return 0; } void solve() { cin>>n; for (int i=1; i<=n; ++i) cin>>l[i]>>r[i]; l[0]=-INF, r[0]=INF; if (n == 1) { cout<<1<<"\n"; return; } segtree t; t.init(2*n+10); for (int i=1; i<=n; ++i) { int x=t.que(1, 1, 2*n, l[i]); g[i].first=x; ++indeg[x]; x=t.que(1, 1, 2*n, r[i]); g[i].second=x; ++indeg[x]; t.upd(1, 1, 2*n, l[i], r[i], i); } for (int i=1; i<=n; ++i) { assert(g[i].first != -1 && g[i].second != -1); upl[0][i]=g[i].first, upr[0][i]=g[i].second; for (int k=1; k<K; ++k) { upl[k][i]=upl[k-1][upl[k-1][i]]; upr[k][i]=upr[k-1][upr[k-1][i]]; } ++pf[i]; } queue<int> q; for (int i=1; i<=n; ++i) { if (indeg[i] == 0) q.push(i); } while (!q.empty()) { int v=q.front(); q.pop(); if (g[v].first == g[v].second) { int u=g[v].first; indeg[u]-=2; pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); } else { int u=g[v].first; --indeg[u], pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); u=g[v].second; --indeg[u], pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); pf[com(g[v].first, g[v].second)]-=pf[v]; } } ll ans=0; for (int i=1; i<=n; ++i) (ans+=bpw(pf[i], MOD-2))%=MOD; cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e5+100; const int K = 20; const int MOD = 1e9+7; const int INF = 1e9; struct segtree { vector<int> t, lz; void init(int sz) { t.assign(4*sz+10, 0); lz.assign(4*sz+10, -1); } void push(int v) { lz[2*v]=lz[v], lz[2*v+1]=lz[v]; t[2*v]=lz[v], t[2*v+1]=lz[v]; lz[v]=-1; } void upd(int v, int l, int r, int ql, int qr, int vl) { if (l > qr || r < ql) return; if (l >= ql && r <= qr) { lz[v]=vl; t[v]=vl; if (l < r) push(v); return; } int m=(l+r)/2; if (lz[v] != -1) push(v); upd(2*v, l, m, ql, qr, vl), upd(2*v+1, m+1, r, ql, qr, vl); } int que(int v, int l, int r, int p) { if (l == r) return t[v]; int m=(l+r)/2; if (lz[v] != -1) push(v); if (p <= m) return que(2*v, l, m, p); return que(2*v+1, m+1, r, p); } }; ll n, l[N], r[N], f[N], upl[K][N], upr[K][N], pf[N], indeg[N]; pii g[N]; ll bpw(ll x, ll n) { if (n == 0) return 1; ll c=bpw(x, n/2); c=(c*c)%MOD; if (n&1) return (c*x)%MOD; return c; } int mv(int rx, int lp) { if (lp > rx) return rx; for (int i=K-1; i>=0; --i) { if (upl[i][rx] > lp) rx=upl[i][rx]; } return rx; } int com(int lx, int rx) { if (lx == rx) return lx; int lc=lx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > rx) lc=upr[j][lc]; } if (upr[0][lc] == rx) return rx; int rc=rx; for (int j=K-1; j>=0; --j) { if (upl[j][rc] > lx) rc=upl[j][rc]; } if (upl[0][rc] == lx) return lx; for (int i=K-1; i>=0; --i) { int lp=upr[i][lx]; int x=mv(rx, lp); if (x == lp) continue; if (r[lp] < l[x]) lx=lp; } return upr[0][lx]; } int com2(int lx, int rx) { if (lx == rx) return lx; int lc=lx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > rx) lc=upr[j][lc]; } if (upr[0][lc] == rx) return rx; int rc=rx; for (int j=K-1; j>=0; --j) { if (upl[j][rc] > lx) rc=upl[j][rc]; } if (upl[0][rc] == lx) return lx; for (int i=n; i>=1; --i) { int lc=lx, rc=rx; for (int j=K-1; j>=0; --j) { if (upr[j][lc] > i) lc=upr[j][lc]; if (upl[j][rc] > i) rc=upl[j][rc]; } if (upr[0][lc] == upl[0][rc] && upr[0][lc] == i) return i; } return 0; } void solve() { cin>>n; for (int i=1; i<=n; ++i) cin>>l[i]>>r[i]; l[0]=-INF, r[0]=INF; if (n == 1) { cout<<1<<"\n"; return; } segtree t; t.init(2*n+10); for (int i=1; i<=n; ++i) { int x=t.que(1, 1, 2*n, l[i]); g[i].first=x; ++indeg[x]; x=t.que(1, 1, 2*n, r[i]); g[i].second=x; ++indeg[x]; t.upd(1, 1, 2*n, l[i], r[i], i); } for (int i=1; i<=n; ++i) { assert(g[i].first != -1 && g[i].second != -1); upl[0][i]=g[i].first, upr[0][i]=g[i].second; for (int k=1; k<K; ++k) { upl[k][i]=upl[k-1][upl[k-1][i]]; upr[k][i]=upr[k-1][upr[k-1][i]]; } ++pf[i]; } queue<int> q; for (int i=1; i<=n; ++i) { if (indeg[i] == 0) q.push(i); } while (!q.empty()) { int v=q.front(); q.pop(); if (g[v].first == g[v].second) { int u=g[v].first; indeg[u]-=2; pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); } else { int u=g[v].first; --indeg[u], pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); u=g[v].second; --indeg[u], pf[u]+=pf[v]; if (indeg[u] == 0) q.push(u); pf[com(g[v].first, g[v].second)]-=pf[v]; } } ll ans=0; for (int i=1; i<=n; ++i) (ans+=bpw(pf[i], MOD-2))%=MOD; cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |