Submission #407036

#TimeUsernameProblemLanguageResultExecution timeMemory
407036tengiz05Strange Device (APIO19_strange_device)C++17
65 / 100
566 ms40640 KiB
#include <bits/stdc++.h> using i64 = long long; constexpr i64 inf = 1e18 + 100; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); i64 n, A, B; std::cin >> n >> A >> B; i64 gcd = std::__gcd(A, B + 1); i64 cycle = A / gcd; if (cycle > inf / B) { cycle = inf; } else { cycle *= B; } std::vector<std::pair<i64, i64>> v; for (int i = 0; i < n; i++) { i64 l, r; std::cin >> l >> r; l %= cycle; r %= cycle; if (r - l + 1 >= cycle) { std::cout << cycle << "\n"; return 0; } if (l <= r) { v.emplace_back(l, r); } else { v.emplace_back(0, r); v.emplace_back(l, cycle - 1); } } sort(v.begin(), v.end()); std::vector<std::pair<i64, i64>> nw; nw.push_back(v[0]); for (int i = 1; i < int(v.size()); i++) { if (nw[nw.size() - 1].second >= v[i].first) { nw[nw.size() - 1].first = std::min(nw[nw.size() - 1].first, v[i].first); nw[nw.size() - 1].second = std::max(nw[nw.size() - 1].second, v[i].second); } else { nw.emplace_back(v[i]); } } i64 ans = 0; for (auto [l, r] : nw) { ans += r - l + 1; } std::cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...