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
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int MAXT = 1e18 + 5;
int gcd(int a, int b) {
if(a % b == 0) {
return b;
}
return gcd(b, a % b);
}
void Solve() {
int n, a, b;
cin >> n >> a >> b;
int repetition = a / gcd(a, b + 1);
vector<pair<int, int> > v(n + 1);
int sum = 0;
for(int i = 1; i <= n; i++) {
cin >> v[i].first >> v[i].second;
sum += (v[i].second - v[i].first + 1);
}
if(b > MAXT / repetition) {
cout << sum << "\n";
return;
}
int repetition2 = a / gcd(a, b + 1) * b;
vector<pair<int, int> > temp;
for(int i = 1; i <= n; i++) {
if(v[i].second - v[i].first + 1 >= repetition2) {
cout << repetition2 << "\n";
return;
}
int startpoint = v[i].first % repetition2;
int endpoint = v[i].second % repetition2;
if(startpoint <= endpoint) {
temp.push_back({startpoint, endpoint});
}
else {
temp.push_back({startpoint, repetition2 - 1});
temp.push_back({0LL, endpoint});
}
}
sort(temp.begin(), temp.end());
int ans = 0, left = 0, right = -1;
for(int i = 0; i < temp.size(); i++) {
if(temp[i].first > right) {
ans += (right - left + 1);
left = temp[i].first;
right = temp[i].second;
}
else {
right = max(right, temp[i].second);
}
}
ans += (right - left + 1);
cout << ans << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message (stderr)
strange_device.cpp: In function 'void Solve()':
strange_device.cpp:49:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 0; i < temp.size(); i++) {
| ~~^~~~~~~~~~~~~
# | 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... |