#include<bits/stdc++.h> using namespace std; int tab[1000009], col[500009]; vector<int> graf[500009]; queue<int> q; const long long M = 1e9 + 7; long long fast_pow(long long a, long long p) { if(p == 0) return 1; if(p == 1) return a; long long b = fast_pow(a, p/2); if(p % 2 == 1) b = (((b * b) % M) * a) % M; else b = (b * b) % M; return b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b, c, wyn = 0; cin>>n; for(int i=1; i<=n; i++) { cin>>a>>b; if(tab[a] > 0) graf[tab[a]].push_back(i); if(tab[b] > 0) graf[tab[b]].push_back(i); for(int j=a; j<=b; j++) tab[j] = i; } for(int i=1; i<=n; i++) { col[i] = i; q.push(i); a = 0; while(!q.empty()) { b = q.front(); q.pop(); a++; for(int j=0; j<graf[b].size(); j++) { c = graf[b][j]; if(col[c] < i) { col[c] = i; q.push(c); } } } wyn = (wyn + fast_pow(a, M-2)) % M; } cout<<wyn; return 0; }
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 | #include<bits/stdc++.h> using namespace std; int tab[1000009], col[500009]; vector<int> graf[500009]; queue<int> q; const long long M = 1e9 + 7; long long fast_pow(long long a, long long p) { if(p == 0) return 1; if(p == 1) return a; long long b = fast_pow(a, p/2); if(p % 2 == 1) b = (((b * b) % M) * a) % M; else b = (b * b) % M; return b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b, c, wyn = 0; cin>>n; for(int i=1; i<=n; i++) { cin>>a>>b; if(tab[a] > 0) graf[tab[a]].push_back(i); if(tab[b] > 0) graf[tab[b]].push_back(i); for(int j=a; j<=b; j++) tab[j] = i; } for(int i=1; i<=n; i++) { col[i] = i; q.push(i); a = 0; while(!q.empty()) { b = q.front(); q.pop(); a++; for(int j=0; j<graf[b].size(); j++) { c = graf[b][j]; if(col[c] < i) { col[c] = i; q.push(c); } } } wyn = (wyn + fast_pow(a, M-2)) % M; } cout<<wyn; return 0; } |