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;
using ll = long long;
const ll inf = 1e18;
void add (set<pair<ll, ll>>& st, ll l, ll r) {
auto it = st.lower_bound({l, 0}), tmp = it;
if (it != st.begin() && (--tmp)->second >= l) {
l = tmp->first;
r = max(r, tmp->second);
st.erase(tmp);
}
while (it != st.end()) {
if (r >= it->first) {
r = max(r, it->second);
it = st.erase(it);
} else break;
}
st.insert({l, r});
}
int main () {
ll n, a, b;
cin >> n >> a >> b;
ll mod;
if (a / gcd(a, b+1) > inf / b) mod = inf;
else mod = a / gcd(a, b+1) * b;
set<pair<ll, ll>> st;
while (n--) {
ll l, r;
cin >> l >> r;
if (r-l+1 >= mod) {
cout << mod << '\n';
return 0;
}
if (l % mod > r % mod) {
add(st, 0, r % mod);
add(st, l % mod, mod-1);
} else add(st, l % mod, r % mod);
}
ll ans = 0;
for (auto& [l, r] : st) 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... |