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;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long a, b;
cin >> n >> a >> b;
a /= gcd(a, b + 1);
long long period = 1e18 + 1;
if (a <= LLONG_MAX / b) {
period = a * b;
}
vector<long long> l(n), r(n);
vector<pair<long long,long long>> v;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
if (r[i] - l[i] + 1 >= period) {
cout << period << '\n';
return 0;
}
l[i] %= period;
r[i] %= period;
if (l[i] <= r[i]) {
v.emplace_back(l[i], r[i]);
} else {
v.emplace_back(l[i], period - 1);
v.emplace_back(0, r[i]);
}
}
sort(v.begin(), v.end());
long long cl = 0;
long long cr = -1;
long long ans = 0;
for (int i = 0; i < (int) v.size(); ++i) {
auto [x, y] = v[i];
if (x > cr) {
ans += cr - cl + 1;
cl = x; cr = y;
} else {
cr = max(cr, y);
}
}
ans += cr - cl + 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... |