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;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxN = 1e6 + 20;
const long long inf = 1e18L + 20;
long long L[maxN];
long long R[maxN];
long long mul(long long x, long long y) {
if (x > inf / y) {
return inf;
}
else {
return x * y;
}
}
void just_do_it() {
int N;
long long A, B;
cin >> N >> A >> B;
for (int i = 0; i < N; i++) {
cin >> L[i] >> R[i];
}
long long cycle = mul(A / __gcd(B + 1, A), B);
if (cycle == inf) {
long long res = 0;
for (int i = 0; i < N; i++) {
res += R[i] - L[i] + 1;
}
cout << res;
return;
}
deque<pair<long long, long long>> q;
for (int i = 0; i < N; i++) {
if (R[i] - L[i] + 1 >= cycle) {
cout << cycle;
return;
}
long long x = L[i] % cycle;
long long y = R[i] % cycle;
if (x <= y) {
q.push_back({x, y});
}
else {
q.push_back({x, cycle - 1});
q.push_back({0, y});
}
}
sort(q.begin(), q.end());
long long res = 0;
while (!q.empty()) {
if ((int)q.size() >= 2 && q[1].first <= q[0].second) {
pair<long long, long long> merged = {q[0].first, max(q[0].second, q[1].second)};
q.pop_front();
q.pop_front();
q.push_front(merged);
}
else {
res += q[0].second - q[0].first + 1;
q.pop_front();
}
}
cout << res;
}
# | 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... |