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>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long int ll;
typedef long double ldb;
ll gcd(ll x,ll y){if(!x||!y) return x+y; return x%y==0?y:gcd(y,x%y);}
ll n, A, B, inf=2e18, ans;
pair<ll, ll> wow[2111111];
int main(void)
{
fio; ll i, j, u, v, cnt=0;
cin>>n>>A>>B; ll g=gcd(A, B+1);
__int128 cc=(A/g); cc=cc*B;
for(i=1 ; i<=n ; i++)
{
cin>>u>>v;
if(cc>=inf) { wow[++cnt]=make_pair(u, v); continue; }
u=(ll)(u%cc); v=(ll)(v%cc);
if(u<=v) wow[++cnt]=make_pair(u, v);
if(u>v)
{
wow[++cnt]=make_pair(u, (ll)cc-1LL);
wow[++cnt]=make_pair(0LL, v);
}
}
sort(wow+1, wow+cnt+1);
for(i=1 ; i<=cnt ; i++)
{
j=i; ll rng=wow[i].second;
while(j<=cnt && wow[j].first<=rng)
{ rng=max(rng, wow[j].second); j++; }
j--; ans+=(rng-wow[i].first+1); i=j;
}
cout<<ans; return 0;
}
# | 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... |