#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18 + 2;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
// every A / gcd(A, B+1) is the same
int n;
ll A, B;
cin >> n >> A >> B;
// B++;
ll one = A;
if (one >= (INF + B - 1) / B) one = INF;
else one *= B;
// cout << one << '\n';
vector<pair<int, int>> sweep;
for (int i = 1;i <= n;i++) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= one) {
cout << one << '\n';
return 0;
}
l %= one, r %= one;
// cout << l << ' ' << r << '\n';
if (l <= r) {
sweep.push_back({ l, 1 });
sweep.push_back({ r + 1, -1 });
}
else {
sweep.push_back({ 0, 1 });
sweep.push_back({ r + 1, -1 });
sweep.push_back({ l, 1 });
sweep.push_back({ one, -1 });
}
}
sort(sweep.begin(), sweep.end());
ll ans = 0;
int now = 0;
for (int i = 0, j;i < (int)sweep.size();i = j) {
for (j = i;j < (int)sweep.size() && sweep[i].first == sweep[j].first; j++) {
now += sweep[j].second;
}
if (now > 0) {
ans += sweep[j].first - sweep[i].first;
}
}
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... |