#include <iostream> #include <vector> #include <map> #include <unordered_set> using namespace std; const long long MOD = 1e9 + 7; int inv(int number) { if (number <= 1) { return number; } return MOD - MOD / number * inv(MOD % number) % MOD; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin >> n; vector<pair<int, int>> shelfs; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; shelfs.push_back({a, b}); } long long result = 0; map<int, unordered_set<int>> streams; for (int i = shelfs.size() - 1; i >= 0; i--) { auto beginIter = streams.lower_bound(shelfs[i].first); auto endIter = streams.upper_bound(shelfs[i].second); unordered_set<int> faucets; for (auto iter = beginIter; iter != endIter;) { for (int faucet : iter->second) { faucets.insert(faucet); } iter = streams.erase(iter); } faucets.insert(i); streams[shelfs[i].first] = faucets; streams[shelfs[i].second] = faucets; result = (result + inv(faucets.size())) % MOD; } cout << result << endl; }
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 | #include <iostream> #include <vector> #include <map> #include <unordered_set> using namespace std; const long long MOD = 1e9 + 7; int inv(int number) { if (number <= 1) { return number; } return MOD - MOD / number * inv(MOD % number) % MOD; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin >> n; vector<pair<int, int>> shelfs; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; shelfs.push_back({a, b}); } long long result = 0; map<int, unordered_set<int>> streams; for (int i = shelfs.size() - 1; i >= 0; i--) { auto beginIter = streams.lower_bound(shelfs[i].first); auto endIter = streams.upper_bound(shelfs[i].second); unordered_set<int> faucets; for (auto iter = beginIter; iter != endIter;) { for (int faucet : iter->second) { faucets.insert(faucet); } iter = streams.erase(iter); } faucets.insert(i); streams[shelfs[i].first] = faucets; streams[shelfs[i].second] = faucets; result = (result + inv(faucets.size())) % MOD; } cout << result << endl; } |