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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=3e18;
const ll mod=1e9+7;
ll n,A,B;
vector<pli>vec;
void solve()
{
cin >> n >> A >> B;
ll x=(A/__gcd(A,B+1))*B;
bool ok=true;
ll sum=0;
for(int i=1;i<=n;i++)
{
ll l,r;
cin >> l >> r;
sum+=r-l+1;
if((r-l+1)>=x)
{
ok=false;
}
l%=x;
r%=x;
if(l<=r)
{
vec.pb({l,r});
}
else
{
vec.pb({l,x-1});
vec.pb({0,r});
}
}
ll cc=A/(__gcd(A,B+1));
if(inf/cc<B)
{
cout << sum;
return;
}
if(ok==false)
{
cout << x;
return;
}
sort(vec.begin(),vec.end());
ll s = 0, hi = -1;
for(int i = 0; i < vec.size(); i++){
if(vec[i].fi > hi){
s += (vec[i].se - vec[i].fi + 1);
hi = vec[i].se;
}
else
{
s+=max(0ll,vec[i].se-hi);
hi=max(hi,vec[i].se);
}
}
cout << s << "\n";
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
strange_device.cpp: In function 'void solve()':
strange_device.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < vec.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... |