#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define ii pair<ll,ll>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
ll l[1000005],r[1000005];
vii vec;
int main(){
boost;
ll n,A,B;
cin>>n>>A>>B;
for (int i = 0; i < n; ++i){
cin>>l[i]>>r[i];
//if(!(A&1))l[i]*=2LL,r[i]*=2LL;
}
if(A<2){
cout<<1;return 0;
}
A/=__gcd(A,(B+1));
//if(!(A&1))A/=2;
for (int i = 0; i < n; ++i)
{
if((r[i]-l[i]+1)>=A){vec.pb(ii(0,A-1));break;}
if((r[i]%A)<(l[i]%A)){
ll lefty=l[i]%A,righty=A-1;
vec.pb(ii(lefty,righty));
lefty=0,righty=r[i]%A;
vec.pb(ii(lefty,righty));
continue;
}
vec.pb(ii(l[i]%A,r[i]%A));
}
sort(all(vec));
vii final;
ll lefty=vec[0].fi,righty=vec[0].se;
for (int i = 1; i < sz(vec); ++i)
{
if(vec[i].fi<=righty){
righty=max(righty,vec[i].se);
}
else{
final.pb(ii(lefty,righty));
lefty=vec[i].fi,righty=vec[i].se;
}
}
final.pb(ii(lefty,righty));
ll ans=0;
for(auto it:final)
ans+=(it.se-it.fi+1);
cout<<ans;
return 0;
}
//long long
//array bounds
//special cases
//binary search
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
647 ms |
56464 KB |
Output is correct |
3 |
Correct |
654 ms |
56956 KB |
Output is correct |
4 |
Correct |
642 ms |
56868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
647 ms |
56464 KB |
Output is correct |
3 |
Correct |
654 ms |
56956 KB |
Output is correct |
4 |
Correct |
642 ms |
56868 KB |
Output is correct |
5 |
Correct |
11 ms |
376 KB |
Output is correct |
6 |
Incorrect |
630 ms |
40120 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
647 ms |
56464 KB |
Output is correct |
3 |
Correct |
654 ms |
56956 KB |
Output is correct |
4 |
Correct |
642 ms |
56868 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Incorrect |
62 ms |
5080 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
60 ms |
2888 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |