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;
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
__int128 lcm(ll a, ll b) {
return (__int128)a * b / gcd(a, b);
}
ll solve(ll n, ll a, ll b, vector<ll> l, vector<ll> r) {
ll ans = 0, need;
bool was = 0, sum = 0;
ll k = lcm(a, b + 1) / (__int128)(b + 1);
if (log2(k) + log2(b) > log2(1e18) + 1) {
sum = 1;
} else {
need = k * b;
}
vector<pair<ll, ll>> seg, u;
for (int i = 0; i < n; i++) {
if (sum) {
ans += r[i] - l[i] + 1;
} else {
if (r[i] - l[i] + 1 >= need) {
was = 1;
} else {
ll l1 = l[i] % (need), r1 = r[i] % (need);
if (l1 <= r1) {
seg.pb({l1, r1});
} else {
seg.pb({l1, need - 1});
seg.pb({0, r1});
}
}
}
}
if (was) {
return need;
}
if (sum) {
return ans;
} else {
for (auto to : seg) {
u.pb({to.first, 1});
u.pb({to.second + 1, -1});
}
sort(u.begin(), u.end());
ll bal = 0, last = -1;
for (auto to : u) {
if (bal > 0) {
ans += to.first - last;
}
bal += to.second;
last = to.first;
}
return ans;
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LCOAL
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
vector<ll> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
cout << solve(n, a, b, l, r) << endl;
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... |