# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
723176 | culver0412 | Strange Device (APIO19_strange_device) | C++17 | 0 ms | 0 KiB |
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;
int main(){
long long int n,a,b;
cin >> n >> a >> b;
long long int x=a/gcd(a,b+1)*b;
pair<long long int,long long int> ranges;
for(int i=0;i<n;i++){
long long int l,r;
cin >> l >> r;
if(r-l+1>=x){
cout << x << endl
return 0;
}
l%=x;
r%=x;
if(l<=r){
ranges.push_back({l,r});
}
else{
ranges.push_back({0,r});
ranges.push_back({l,x-1});
}
}
sort(ranges.begin(),ranges.end());
long long int curl=ranges[0].first,curr=ranges[0].second,ans=0;
for(int i=1;i<ranges.size();i++){
pair<long long int,long long int> range=ranges[i];
if(curr<range.first){
ans+=(curr-curl+1);
curl=range.first;
curr=range.second;
}
else{
curr=max(curr,range.second);
}
}
ans+=(curr-curl+1);
cout << ans << endl;
}