#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> original;
int main() {
ll n, a, b;
cin >> n >> a >> b;
vector<pair<ll, ll>> v(n);
for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
for (int i = 0; i < n; i++) {
original.push_back(v[i].first);
original.push_back(v[i].second);
original.push_back(v[i].second + 1);
}
sort(original.begin(), original.end());
original.resize(unique(original.begin(), original.end()) - original.begin());
map<ll, ll> mp;
for (int i = 0; i < original.size(); i++) mp[original[i]] = i;
ll gc = __gcd(a, b + 1);
bool smallEnough = (__int128_t(a) / gc * __int128_t(b) <= LONG_LONG_MAX);
ll cy = smallEnough ? a / gc * b : 0;
vector<int> pre(original.size() + 1);
for (int i = 0; i < n; i++) {
auto& p = v[i];
ll l = p.first, r = p.second;
if (smallEnough && r - l + 1 >= cy) {
cerr << "FULL" << endl;
cout << cy << endl;
return 0;
}
auto add = [&](ll l, ll r) {
pre[mp[l]]++;
pre[mp[r + 1]]--;
};
if (smallEnough) {
l %= cy;
r %= cy;
if (l > r) {
add(l, cy - 1);
add(0, r);
} else {
add(l, r);
}
} else {
add(l, r);
}
}
// for (int i = 1; i < pre.size(); i++) pre[i] += pre[i - 1];
int cur = 0;
ll ans = 0;
for (int i = 0; i < pre.size() - 1; i++) {
cur += pre[i];
if (cur > 0) ans += original[i + 1] - original[i];
}
cout << ans << endl;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < original.size(); i++) mp[original[i]] = i;
| ~~^~~~~~~~~~~~~~~~~
strange_device.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < pre.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
20 ms |
2624 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1836 ms |
168608 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1836 ms |
168608 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1836 ms |
168608 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
212 ms |
23708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
20 ms |
2624 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |