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 rep(i,n) for(int i=0; i<n; i++)
#define all(x) x.begin(),x.end()
using ll=long long;
const ll INF=1LL<<61;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,A,B;
cin>>n>>A>>B;
ll g=__gcd(A,B+1);
A/=g;
ll rp=A*B;
if((INF/A)/B==0LL){
rp=INF;
}
vector<pair<ll,ll>> segs;
rep(i,n){
ll l,r;
cin>>l>>r;
r++;
if(r-l>=rp){
cout<<rp<<"\n";
return 0;
}
l%=rp;
r%=rp;
if(l<r){
segs.emplace_back(pair<ll,ll>(l,r));
}
if(l>r){
segs.emplace_back(pair<ll,ll>(l,rp));
segs.emplace_back(pair<ll,ll>(0,r));
}
}
sort(all(segs));
ll lf=0,ri=0;
ll ans=0;
for(auto &[nl,nr]:segs){
if(nl<ri){
ri=max(ri,nr);
}
else{
ans+=ri-lf;
lf=nl;
ri=nr;
}
}
ans+=ri-lf;
cout<<ans<<"\n";
}
# | 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... |