#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
typedef long long ll;
const int N=1e6+5;
ll A, B, l[N], r[N], ans;
map<ll, ll>m;
int n;
void add(ll x, ll y){
auto it=m.upper_bound(x);
if(it!=m.begin()){
--it;
if(it->S>=x){
if(it->S>=y)
return;
it->S=x-1;
}
++it;
}
if(it==m.end()){
m.insert({x, y});
return;
}
while(it!=m.end()){
if(it->S<=y)
m.erase(it);
else{
if(it->F<=y){
y=it->S;
m.erase(it);
}
break;
}
++it;
}
m.insert({x, y});
}
int main(){
//ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>A>>B;
for(int i=0;i<n;++i)
cin>>l[i]>>r[i];
ll x=__gcd(A, B+1);
x=A/x;
ll inf=1e18;
if(inf/x<B){
for(int i=0;i<n;++i)
ans+=r[i]-l[i]+1;
cout<<ans;
return 0;
}
B*=x;
cout<<B<<'\n';
for(int i=0;i<n;++i){
if(l[i]+B-1<=r[i]){
cout<<B;
return 0;
}
l[i]%=B;
r[i]%=B;
if(r[i]>=l[i])
add(l[i], r[i]);
else{
add(l[i], B-1);
add(0, r[i]);
}
}
auto it=m.begin();
while(it!=m.end()){
ans+=it->S-it->F+1;
++it;
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |