#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int MOD = 1000000007; const int N = 500005; // rozwiązuje równanie ax=1(%MOD), gdy MOD pierwsze int inverse(int a) { int n = MOD - 2; int r = 1; while (n) { if (n % 2) r = (r * 1LL * a) % MOD; a = (a * 1LL * a) % MOD; n >>= 1; } return r; } int n, l[N], r[N], i, j, res, act, vis[N], achiev[N]; VI graf[N]; set<ii> s; set<ii>::iterator it; void dfs(int w, int st) { vis[w] = st; achiev[st]++; for (auto& w2 : graf[w]) if (vis[w2] != st) dfs(w2, st); } int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d %d", &l[i], &r[i]); for (i=n-1;i>=0;i--) { it = s.lower_bound(ii(l[i], 0)); while (it != s.end() && it->first < r[i]) { graf[i].pb(it->second); s.erase(it++); } s.insert(ii(l[i], i)); s.insert(ii(r[i], i)); } res = 0; for (i=0;i<n;i++) vis[i] = -1; for (i=n-1;i>=0;i--) { //if (i % 10000 == 0) printf("%d\n", i); if (graf[i].size() == 0) achiev[i] = 1; else if (graf[i].size() == 1) achiev[i] = achiev[graf[i][0]] + 1; else dfs(i, i); res = (res + inverse(achiev[i])) % MOD; } printf("%d\n", res); 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int MOD = 1000000007; const int N = 500005; // rozwiązuje równanie ax=1(%MOD), gdy MOD pierwsze int inverse(int a) { int n = MOD - 2; int r = 1; while (n) { if (n % 2) r = (r * 1LL * a) % MOD; a = (a * 1LL * a) % MOD; n >>= 1; } return r; } int n, l[N], r[N], i, j, res, act, vis[N], achiev[N]; VI graf[N]; set<ii> s; set<ii>::iterator it; void dfs(int w, int st) { vis[w] = st; achiev[st]++; for (auto& w2 : graf[w]) if (vis[w2] != st) dfs(w2, st); } int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d %d", &l[i], &r[i]); for (i=n-1;i>=0;i--) { it = s.lower_bound(ii(l[i], 0)); while (it != s.end() && it->first < r[i]) { graf[i].pb(it->second); s.erase(it++); } s.insert(ii(l[i], i)); s.insert(ii(r[i], i)); } res = 0; for (i=0;i<n;i++) vis[i] = -1; for (i=n-1;i>=0;i--) { //if (i % 10000 == 0) printf("%d\n", i); if (graf[i].size() == 0) achiev[i] = 1; else if (graf[i].size() == 1) achiev[i] = achiev[graf[i][0]] + 1; else dfs(i, i); res = (res + inverse(achiev[i])) % MOD; } printf("%d\n", res); return 0; } |