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 <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
while (b > 0) {
swap(a, b);
b %= a;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
long long a, b;
cin >> n >> a >> b;
long long md = (long long) 1e18 + 1;
a /= gcd(a, b + 1);
if (a <= numeric_limits<long long>::max() / b) {
md = a * b;
}
vector< pair<long long, long long> > v;
for (int i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
long long x = l % md, y = r % md;
if (x <= y) {
v.push_back({x, y});
} else {
v.push_back({x, md - 1});
v.push_back({0, y});
}
}
sort(v.begin(), v.end());
long long ans = 0;
long long l = v[0].first, r = v[0].second;
for (auto& p : v) {
if (p.first > r) {
ans += r - l + 1;
l = p.first;
r = p.second;
} else {
r = max(r, p.second);
}
}
ans += r - l + 1;
cout << ans << '\n';
return 0;
}
# | 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... |