This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
22/02/2020
islingr
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define eb(x...) emplace_back(x)
using ll = int64_t;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
struct event {
ll l, r;
event(ll a, ll b) : l{a}, r{b} { }
bool operator<(const event &e) const { return r < e.r; }
};
const ll inf = 1e18;
ll solve () {
int n; ll a, b; cin >> n >> a >> b;
ll g = __gcd(a, b + 1), p;
if (a / g > inf / b) p = inf;
else p = a / g * b;
vector<event> v; v.reserve(2 * n);
rep(i, 0, n) {
ll l, r; cin >> l >> r;
if (r - l + 1 < p) {
l %= p; r %= p;
if (l <= r) v.eb(l, r);
else v.eb(0, r), v.eb(l, p - 1);
} else return p;
}
sort(all(v));
ll ans = 0;
while (!v.empty()) {
event top = v.back(); v.pop_back();
while (!v.empty() && top.l <= v.back().r) {
ckmin(top.l, v.back().l);
v.pop_back();
}
ans += top.r - top.l + 1;
}
return ans;
}
signed main() { cout << solve(); }
# | 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... |