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>
typedef long long ll;
#define N 20005
#define ff first
#define ss second
#define pb push_back
using namespace std;
ll gcd(ll a,ll b){
while(a!=0 && b!=0){
if(a>b){
a=a%b;
}
else b=b%a;
}
return max(a,b);
}
vector<pair<pair<ll,ll>,pair<ll,ll> > >v;
int main(){
ll n,a,b;
cin >> n >> a >> b;
ll k=gcd(a,(b+1));
k=a/k;
ll y=1e18;
for(int i=1;i<=n;i++){
ll l,r;
cin >> l >> r;
if(y/b>=k){
if(k*b<=(r-l+1)){
cout << k*b;
return 0;
}
}
ll x1=(l/b)%k;
ll y1=l%b;
ll x2=(r/b)%k;
ll y2=r%b;
// cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
if((x1==x2 && y2<y1) || x2<x1){
v.pb({{0,0},{x2,y2}});
v.pb({{x1,y1},{k-1,b-1}});
}
else v.pb({{x1,y1},{x2,y2}});
}
sort(v.begin(),v.end());
ll ans=0;
pair<ll,ll>e={-1,-1};
for(int i=0;i<v.size();i++){
ll x1=v[i].ff.ff;
ll y1=v[i].ff.ss;
ll x2=v[i].ss.ff;
ll y2=v[i].ss.ss;
if(e.ff>x2 || (e.ff==x2 && e.ss>=y2)){
continue;
}
if(x1>e.ff || (x1==e.ff && y1>e.ss)){
ans+=((x2-x1)*b+y2-y1+1);
e={x2,y2};
continue;
}
ans+=((x2-e.ff)*b+y2-e.ss);
e={x2,y2};
}
cout << ans;
}
Compilation message (stderr)
strange_device.cpp: In function 'int main()':
strange_device.cpp:47:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0;i<v.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... |