//Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 20; const int mod = 1e9+7; int mul(int a, int b){ return (a*b)%mod; } void add(int &a, int b){ a += b; if (a >= mod) a-=mod; } int power(int a, int b){ if (!b) return 1ll; int k = power(a, b/2); k = mul(k, k); if (b&1) k = mul(k, a); return k; } void solve(){ int n; cin >> n; vector<T>a(n+1); for (int i = 1; i<=n; i++) cin >> a[i].first >> a[i].second; set<T>s; //l,i a.emplace_back(0, oo2); s.insert({0, n+1}); vector<vector<int>>gt(n+1), g(n+1); vector<int>fix; vector<T> nowy; for (int i = 1; i<=n; i++){ auto [l, r] = a[i]; auto it = s.upper_bound({l, oo}); if (it != s.begin()) it--; fix.clear(); nowy.assign(2, {-1, -1}); for (; it != s.end() && it->first <= r; it++){ fix.emplace_back(it->first); if (it->first < l) nowy[0] = *it; if (a[it->second].second > r) nowy[1] = {r, it->second}; } for (auto v: fix) s.erase(s.lower_bound({v, 0})); for (int rep = 0; rep < 2; rep++){ if (nowy[rep].first != -1){ s.insert(nowy[rep]); g[i].emplace_back(nowy[rep].second); //moze byc rowne n if (nowy[rep].second <= n && !(gt[nowy[rep].second].size() > 0 && gt[nowy[rep].second].back() == i)){ gt[nowy[rep].second].emplace_back(i); } } } s.insert({l, i}); // debug(s); } vector up(2, vector<vector<int>>(K, vector<int>(n+1))); for (int v = 1; v<=n; v++){ debug(v, g[v]); for (int rep = 0; rep < 2; rep++){ if (g[v][rep] > n){ for (int i = 0; i<K; i++) up[rep][i][v] = v; } else { up[rep][0][v] = g[v][rep]; for (int i = 1; i<K; i++) up[rep][i][v] = up[rep][i-1][up[rep][i-1][v]]; } } } //lewy, ma mniejszy lewy koniec vector<vector<int>>what(n+1); auto jump = [&](int f, int m, int rep){ for (int i = K-1; i>=0; i--){ if (up[rep][i][f] >= m){ f = up[rep][i][f]; } } return f; }; for (int i = 1; i<=n; i++){ if (g[i][0] > n || g[i][1] > n || g[i][0] == g[i][1]) continue; debug(i, g[i]); int left = g[i][0], right = g[i][1]; int l = 1, r = min(left, right); int ret = -1; while (r >= l){ int m = (l+r)/2; if (a[jump(left, m, 1)].second >= a[jump(right, m, 0)].first){ ret = m; l = m+1; } else r = m-1; } if (ret != -1) { debug(i, ret); what[ret].emplace_back(i); } // while (left != right){ // if (left > right) { // if (g[left].empty()) break; // left = g[left].back(); // } else { // if (g[right].empty()) break; // right = g[right][0]; // } // } // if (left != right) continue; // debug(i, left); // what[left].emplace_back(i); } // return; vector<int>dp(n+1, 1); int ans = 0; for (int v = n; v>=1; v--){ for (auto x: gt[v]) dp[v] += dp[x]; for (auto x: what[v]) dp[v] -= dp[x]; debug(v, gt[v], what[v], dp[v]); add(ans, power(dp[v], mod-2)); } cout << ans << "\n"; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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 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 | //Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 20; const int mod = 1e9+7; int mul(int a, int b){ return (a*b)%mod; } void add(int &a, int b){ a += b; if (a >= mod) a-=mod; } int power(int a, int b){ if (!b) return 1ll; int k = power(a, b/2); k = mul(k, k); if (b&1) k = mul(k, a); return k; } void solve(){ int n; cin >> n; vector<T>a(n+1); for (int i = 1; i<=n; i++) cin >> a[i].first >> a[i].second; set<T>s; //l,i a.emplace_back(0, oo2); s.insert({0, n+1}); vector<vector<int>>gt(n+1), g(n+1); vector<int>fix; vector<T> nowy; for (int i = 1; i<=n; i++){ auto [l, r] = a[i]; auto it = s.upper_bound({l, oo}); if (it != s.begin()) it--; fix.clear(); nowy.assign(2, {-1, -1}); for (; it != s.end() && it->first <= r; it++){ fix.emplace_back(it->first); if (it->first < l) nowy[0] = *it; if (a[it->second].second > r) nowy[1] = {r, it->second}; } for (auto v: fix) s.erase(s.lower_bound({v, 0})); for (int rep = 0; rep < 2; rep++){ if (nowy[rep].first != -1){ s.insert(nowy[rep]); g[i].emplace_back(nowy[rep].second); //moze byc rowne n if (nowy[rep].second <= n && !(gt[nowy[rep].second].size() > 0 && gt[nowy[rep].second].back() == i)){ gt[nowy[rep].second].emplace_back(i); } } } s.insert({l, i}); // debug(s); } vector up(2, vector<vector<int>>(K, vector<int>(n+1))); for (int v = 1; v<=n; v++){ debug(v, g[v]); for (int rep = 0; rep < 2; rep++){ if (g[v][rep] > n){ for (int i = 0; i<K; i++) up[rep][i][v] = v; } else { up[rep][0][v] = g[v][rep]; for (int i = 1; i<K; i++) up[rep][i][v] = up[rep][i-1][up[rep][i-1][v]]; } } } //lewy, ma mniejszy lewy koniec vector<vector<int>>what(n+1); auto jump = [&](int f, int m, int rep){ for (int i = K-1; i>=0; i--){ if (up[rep][i][f] >= m){ f = up[rep][i][f]; } } return f; }; for (int i = 1; i<=n; i++){ if (g[i][0] > n || g[i][1] > n || g[i][0] == g[i][1]) continue; debug(i, g[i]); int left = g[i][0], right = g[i][1]; int l = 1, r = min(left, right); int ret = -1; while (r >= l){ int m = (l+r)/2; if (a[jump(left, m, 1)].second >= a[jump(right, m, 0)].first){ ret = m; l = m+1; } else r = m-1; } if (ret != -1) { debug(i, ret); what[ret].emplace_back(i); } // while (left != right){ // if (left > right) { // if (g[left].empty()) break; // left = g[left].back(); // } else { // if (g[right].empty()) break; // right = g[right][0]; // } // } // if (left != right) continue; // debug(i, left); // what[left].emplace_back(i); } // return; vector<int>dp(n+1, 1); int ans = 0; for (int v = n; v>=1; v--){ for (auto x: gt[v]) dp[v] += dp[x]; for (auto x: what[v]) dp[v] -= dp[x]; debug(v, gt[v], what[v], dp[v]); add(ans, power(dp[v], mod-2)); } cout << ans << "\n"; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } |