#include <bits/stdc++.h> using namespace std; const int MAKS = 1<<19; const long long P = 1e9 + 7; int n; int tree[2*MAKS]; int dp[MAKS]; vector <int> graph[500100]; vector <int> trans[500100]; int in[MAKS]; int odw[500100]; long long result = 0; int read(int poz){ poz += MAKS; int wyn = 0; while(poz>0){ wyn = max(wyn, tree[poz]); poz /= 2; } return wyn; } void add(int tleft, int tright, int left, int right, int val, int poz){ if(tleft <= left && right <= tright){ tree[poz] = val; return; } if(tleft <= (left+right)/2){ add(tleft, tright, left, (left+right)/2, val, 2*poz); } if((left+right)/2 < tright){ add(tleft, tright, (left+right)/2 + 1, right, val, 2*poz+1); } } long long odwr(long long x, int n){ if(n==1) return x; long long y = odwr(x, n/2); y = (y*y)%P; if(n % 2 == 0) return y; return (y*x) % P; } void dfs(int v){ odw[v] = 1; for(int i: trans[v]){ if(odw[i] == 0) dfs(i); } } int main(){ cin>>n; long long factorial = 1; for(long long i = 1; i <= n; i++) factorial = (factorial * i) % P; long long odw_factorial = odwr(factorial, P-2); for(int i=1;i<=n;i++){ int l,r; cin>>l>>r; int a1 = read(l); int a2 = read(r); if(a1 != 0){ graph[i].push_back(a1); trans[a1].push_back(i); in[a1]++; } if(a2 != 0 && a1 != a2){ graph[i].push_back(a2); trans[a2].push_back(i); in[a2]++; } add(l+MAKS, r+MAKS, MAKS, 2*MAKS-1, i, 1); } for(int i=1;i<=n;i++){ dfs(i); long long sum = 0; for(int j=1;j<=n;j++){ if(odw[j] == 1){ odw[j] = 0; sum++; } } result += odwr(sum, P-2); result %= P; } cout<<result; }
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 | #include <bits/stdc++.h> using namespace std; const int MAKS = 1<<19; const long long P = 1e9 + 7; int n; int tree[2*MAKS]; int dp[MAKS]; vector <int> graph[500100]; vector <int> trans[500100]; int in[MAKS]; int odw[500100]; long long result = 0; int read(int poz){ poz += MAKS; int wyn = 0; while(poz>0){ wyn = max(wyn, tree[poz]); poz /= 2; } return wyn; } void add(int tleft, int tright, int left, int right, int val, int poz){ if(tleft <= left && right <= tright){ tree[poz] = val; return; } if(tleft <= (left+right)/2){ add(tleft, tright, left, (left+right)/2, val, 2*poz); } if((left+right)/2 < tright){ add(tleft, tright, (left+right)/2 + 1, right, val, 2*poz+1); } } long long odwr(long long x, int n){ if(n==1) return x; long long y = odwr(x, n/2); y = (y*y)%P; if(n % 2 == 0) return y; return (y*x) % P; } void dfs(int v){ odw[v] = 1; for(int i: trans[v]){ if(odw[i] == 0) dfs(i); } } int main(){ cin>>n; long long factorial = 1; for(long long i = 1; i <= n; i++) factorial = (factorial * i) % P; long long odw_factorial = odwr(factorial, P-2); for(int i=1;i<=n;i++){ int l,r; cin>>l>>r; int a1 = read(l); int a2 = read(r); if(a1 != 0){ graph[i].push_back(a1); trans[a1].push_back(i); in[a1]++; } if(a2 != 0 && a1 != a2){ graph[i].push_back(a2); trans[a2].push_back(i); in[a2]++; } add(l+MAKS, r+MAKS, MAKS, 2*MAKS-1, i, 1); } for(int i=1;i<=n;i++){ dfs(i); long long sum = 0; for(int j=1;j<=n;j++){ if(odw[j] == 1){ odw[j] = 0; sum++; } } result += odwr(sum, P-2); result %= P; } cout<<result; } |