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 n, a, b;
bool overflow(int x, int y) {
int result = x * y;
return result / x != y;
}
int sum() {
int ans = 0;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
ans += r - l + 1;
}
return ans;
}
int solveTestCase() {
cin >> n >> a >> b;
int temp = a / __gcd(a, b + 1);
assert(a % __gcd(a, b + 1) == 0);
if (overflow(temp, b))
return cout << sum(), 0;
temp *= b;
//cerr << temp << "\n";
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 >= temp)
return cout << temp, 0;
if (l % temp <= r % temp)
v.emplace_back(l % temp, 1), v.emplace_back(r % temp + 1, 2);
else
v.emplace_back(l % temp, 1), v.emplace_back(temp, 2), v.emplace_back(0, 1), v.emplace_back(r % temp + 1, 2), assert(l % temp > r % temp + 1);
}
sort(v.begin(), v.end());
int ans = 0, d = 0, last = 0;
for (auto i : v) {
if (d == 0)
last = i.first;
if (i.second == 1)
d++;
else
d--;
assert(d >= 0);
if (d == 0)
ans += i.first - last;
}
cout << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
//cin >> t;
while (t--)
solveTestCase();
}
Compilation message (stderr)
strange_device.cpp: In function 'long long int solveTestCase()':
strange_device.cpp:65:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | 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... |