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 int long long
int gcd(int a, int b) {
while (b) b ^= a ^= b ^= a %= b;
return a;
}
void solve() {
int n, a, b;
cin >> n >> a >> b;
int g = gcd(a, b + 1);
priority_queue<pair<int, bool>, vector<pair<int, bool> >, greater<pair<int, bool> > > pq;
int mod;
if (log2(a) + log2(b) - log2(g) > log2(2e18)) mod = 2e18;
else mod = a / g * b;
while (n--) {
int l, r;
cin >> l >> r;
if (r - l + 1 >= mod) {
cout << mod << endl;
return;
}
l %= mod; r %= mod;
if (l > r) {
pq.push({l, true});
pq.push({mod, false});
pq.push({0, true});
pq.push({r + 1, false});
} else {
pq.push({l, true});
pq.push({r + 1, false});
}
}
int prev = 0, ans = 0;
int cnt = 0;
while (!pq.empty()) {
pair<int, bool> cur = pq.top();
pq.pop();
if (cnt) ans += cur.first - prev;
prev = cur.first;
cnt += (cur.second ? 1 : -1);
}
cout << ans << endl;
}
signed main() {
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... |