#include <bits/stdc++.h> using namespace std; typedef long long ll; const int base = (1<<20); int tree[2*base]; void change(int a, int b, int x){ a += base-1; b += base+1; while (b-a > 1){ if (!(a%2)) tree[a+1] = x; if (b%2) tree[b-1] = x; a /= 2; b /= 2; } } int query(int v){ v += base; int wy = 0; while (v){ wy = max(wy,tree[v]); v /= 2; } return wy; } pair<int,int> kraw[500001]; vector<int> sas[500001]; ll deg[500001]; bitset<50001> dp[50001]; const ll mod = 1e9+7; ll fastpot(ll a, ll b){ ll w = 1; while (b){ if (b%2) w = (w*a)%mod; a = (a*a)%mod; b /= 2; } return w%mod; } ll inv(ll x){ return fastpot(x,mod-2); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll i; int n; cin>>n; pair<int,int> tab[n+1]; vector<int> perm; for (i = 1; i <= n; i++){ int a,b; cin>>a>>b; perm.push_back(i); kraw[i] = {query(a),query(b)}; sas[i].push_back(kraw[i].first); sas[i].push_back(kraw[i].second); deg[kraw[i].first]++; deg[kraw[i].second]++; tab[i] = {a,b}; change(a,b,i); } queue<int> Q; for (i = 1; i <= n; i++){ dp[i][i] = 1; if (!deg[i]){ Q.push(i); } } ll wy = 0; while (Q.size()){ int v = Q.front(); Q.pop(); wy = (wy+inv(dp[v].count()))%mod; for (int u : sas[v]){ if (!u) continue; deg[u]--; dp[u] |= dp[v]; if (!deg[u]) Q.push(u); } } cout<<wy<<"\n"; 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 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int base = (1<<20); int tree[2*base]; void change(int a, int b, int x){ a += base-1; b += base+1; while (b-a > 1){ if (!(a%2)) tree[a+1] = x; if (b%2) tree[b-1] = x; a /= 2; b /= 2; } } int query(int v){ v += base; int wy = 0; while (v){ wy = max(wy,tree[v]); v /= 2; } return wy; } pair<int,int> kraw[500001]; vector<int> sas[500001]; ll deg[500001]; bitset<50001> dp[50001]; const ll mod = 1e9+7; ll fastpot(ll a, ll b){ ll w = 1; while (b){ if (b%2) w = (w*a)%mod; a = (a*a)%mod; b /= 2; } return w%mod; } ll inv(ll x){ return fastpot(x,mod-2); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll i; int n; cin>>n; pair<int,int> tab[n+1]; vector<int> perm; for (i = 1; i <= n; i++){ int a,b; cin>>a>>b; perm.push_back(i); kraw[i] = {query(a),query(b)}; sas[i].push_back(kraw[i].first); sas[i].push_back(kraw[i].second); deg[kraw[i].first]++; deg[kraw[i].second]++; tab[i] = {a,b}; change(a,b,i); } queue<int> Q; for (i = 1; i <= n; i++){ dp[i][i] = 1; if (!deg[i]){ Q.push(i); } } ll wy = 0; while (Q.size()){ int v = Q.front(); Q.pop(); wy = (wy+inv(dp[v].count()))%mod; for (int u : sas[v]){ if (!u) continue; deg[u]--; dp[u] |= dp[v]; if (!deg[u]) Q.push(u); } } cout<<wy<<"\n"; return 0; } |