This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
_________ .---""" """---.
:______.-': : .--------------. :
| ______ | | : : |
|:______B:| | | Little Error: | |
|:______B:| | | | |
|:______B:| | | Power not | |
| | | | found. | |
|:_____: | | | | |
| == | | : : |
| O | : '--------------' :
| o | :'---...______...---'
| o |-._.-i___/' \._
|'-.____o_| '-. '-...______...-' `-._
:_________: `.____________________ `-.___.-.
.'.eeeeeeeeeeeeeeeeee.'. :___:
fsc .'.eeeeeeeeeeeeeeeeeeeeee.'.
:____________________________:
*/
//in the name of god
//if you read this code please search about imam hussain
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<ll,ll>
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define read freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout)
const int maxn=2e5+5;
const int mod=1e9+7;
const int inf=1e9;
const int del=728729;
const int Pr2 = 1030294297 ;
ll poww(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));}
ll gcd(ll a, ll b) {
if (b == 0){
return a;
}
return gcd(b, a % b);
}
set<pii> st ;
ll n ;
ll a ;
ll b ;
ll l ;
ll r ;
ll lp ;
ll rp ;
ll ans ;
ll p ;
ll w ;
ll k ;
int main(){
migmig ;
cin >> n >> a >> b ;
if(a/gcd(a , b + 1) > inf / b){
p = inf ;
}else{
p = a / gcd(a , b + 1) * b ;
}
for(int tst=0 ; tst< n ; tst++){
cin>>l>>r ;
if(k){
continue ;
}
if(r - l + 1 >= p ){
k = 1 ;
ans = p ;
continue ;
}
l = l % p ;
r = r % p ;
if(r >= l){
st.insert({l , r}) ;
}else{
st.insert({0 , r}) ;
st.insert({l , p - 1}) ;
}
}
if(k){
cout<<ans ;
return 0;
}
st.insert({inf + 1 , inf + 1}) ;
lp = st.begin() -> X ;
rp = st.begin() -> Y ;
//cout<<lp <<" " << rp ;
auto it = next(st.begin()) ;
while(it != st.end()){
if(it -> X > rp){
//cout<<lp <<" " << rp ;
ans += rp - lp + 1 ;
lp = it -> X;
rp = it -> Y ;
//cout<<lp <<" " << rp ;
}else{
rp = max(1LL*rp ,1LL* it -> Y) ;
//cout<<lp <<" " << rp ;
}
it++ ;
}
cout<<ans << endl ;
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... |