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;
const long long N = ((long long) 1e18) + 5;
long long gcd(long long x, long long y) {
if (x < y) swap(x, y);
while (y) {
x %= y;
swap(x, y);
}
return x;
}
vector< pair<int, int> > v;
long long ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, a, b;
cin >> n >> a >> b;
long long aa = a / gcd(a, b + 1);
long long len;
if (aa > (N / b))
len = N;
else len = a * b;
for (int i = 0; i < n; i++) {
long long bg, en;
cin >> bg >> en;
if (en - bg >= len) {
cout << len << '\n';
exit(0);
}
bg %= len;
en %= len;
if (bg > en) {
v.push_back(make_pair(bg, len - 1));
v.push_back(make_pair(0, en));
} else {
v.push_back(make_pair(bg, en));
}
}
sort (v.begin(), v.end());
long long bg = 0, en = -1;
for (pair<int, int> x : v) {
long long bgg = x.first;
long long enn = x.second;
if (bgg > en) {
ans += en - bg + 1;
bg = bgg;
en = enn;
continue;
}
if (bgg <= en) {
en = max(en, enn);
}
}
ans += en - bg + 1;
cout << ans << '\n';
}
# | 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... |