1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include<bits/stdc++.h> using namespace std; using ll=long long; #define mp make_pair char IO; ll rd(){ ll s=0; while(!isdigit(IO=getchar())); do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return s; } int n,c,i; ll A,B,ans,r=-1,L,R; pair <ll,ll> S[2000010]; int main(){ for(n=rd(),A=rd(),B=rd(),A=min(1.0*B*(A/__gcd(A,B+1)),1e18),i=1;i<=n;++i) { L=rd(),R=rd(); if(R-L+1>=A) return printf("%lld\n",A),0; L%=A,R%=A; L<=R?S[++c]=mp(L,R):(S[++c]=mp(L,A-1),S[++c]=mp(0,R)); } for(sort(S+1,S+c+1),i=1;i<=c;++i) if(r<=S[i].second) ans+=S[i].second-max(r,S[i].first-1),r=S[i].second; printf("%lld\n",ans); }
|