# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1095794 | LucaLucaM | Strange Device (APIO19_strange_device) | C++17 | 3 ms | 1492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using ll = long long;
using i128 = __int128;
int main() {
freopen("input.txt", "r", stdin);
int n;
ll A, B;
std::cin >> n >> A >> B;
if ((i128) A * B / std::__gcd(A, B + 1) > 1e18) {
ll answer = 0;
for (int i = 0; i < n; i++) {
ll l, r;
std::cin >> l >> r;
answer += r - l + 1;
}
std::cout << answer;
return 0;
}
ll R = (i128) A * B / std::__gcd(A, B + 1);
std::vector<std::pair<ll, int>> events;
bool coverAll = false;
for (int i = 0; i < n; i++) {
ll l, r;
std::cin >> l >> r;
if (r - l + 1 >= R) {
coverAll = true;
}
l %= R;
r %= R;
if (l <= r) {
events.push_back({l, +1});
events.push_back({r + 1, -1});
} else {
events.push_back({0, +1});
events.push_back({r + 1, -1});
events.push_back({l, +1});
events.push_back({R, -1});
}
}
if (coverAll) {
std::cout << R;
return 0;
}
events.push_back({R, 0});
std::sort(events.begin(), events.end());
i128 last = 0;
int delta = 0;
ll answer = 0;
for (const auto &[pos, add] : events) {
if (delta >= 1) {
answer += pos - last;
}
delta += add;
last = pos;
}
std::cout << answer;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |