#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int n, a, b;
int gcd(int x, int y){
int a=x, b=y;
if(a<b){
swap(a, b);
}
if(b==0){return a;}
return gcd(b, a%b);
}
int32_t main(){
INIT
cin>>n>>a>>b;
int k=a/gcd(a, b+1);
int m=0;
if( (( ((ll)(1e9))*((ll)(1e9)) )/(b))>=(k) ){
m=k*(b);
}
else{
m=((ll)2*((ll)(1e9))*((ll)(1e9)));
}
//cout<<(m)<<"\n";
set<pii> s;
int l, r;
for(int i=1; i<=n; i++){
cin>>l>>r;
s.insert( {(l%m), min(m-1, (l%m)+r-l )} );
if( (r-l+1)>=m ){
cout<<m<<"\n";
return 0;
}
if( r>(l+(m-1-l%m) ) ){
s.insert({0, r%m});
}
}
l=0, r=-1;
int res=0;
for(auto it=s.begin(); it!=s.end(); it++){
//cout<<l<<" "<<r<<"\n";
if(r==-1){
r=it->sc; l=it->ft;
continue;
}
if(it->ft>r){
res+=(r-l+1);
l=it->ft, r=it->sc;
continue;
}
r=max(r, it->sc);
}
//cout<<l<<" "<<r<<"\n";
res+=(r-l+1);
cout<<res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
7 ms |
1044 KB |
Output is correct |
3 |
Correct |
6 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
63 ms |
6764 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
307 ms |
1004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
660 ms |
63080 KB |
Output is correct |
3 |
Correct |
700 ms |
63340 KB |
Output is correct |
4 |
Correct |
669 ms |
63324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
660 ms |
63080 KB |
Output is correct |
3 |
Correct |
700 ms |
63340 KB |
Output is correct |
4 |
Correct |
669 ms |
63324 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
664 ms |
63084 KB |
Output is correct |
7 |
Correct |
670 ms |
63084 KB |
Output is correct |
8 |
Correct |
669 ms |
63260 KB |
Output is correct |
9 |
Correct |
726 ms |
63212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
660 ms |
63080 KB |
Output is correct |
3 |
Correct |
700 ms |
63340 KB |
Output is correct |
4 |
Correct |
669 ms |
63324 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
60 ms |
6752 KB |
Output is correct |
7 |
Correct |
60 ms |
6764 KB |
Output is correct |
8 |
Correct |
64 ms |
6764 KB |
Output is correct |
9 |
Correct |
65 ms |
6804 KB |
Output is correct |
10 |
Correct |
62 ms |
6892 KB |
Output is correct |
11 |
Correct |
63 ms |
6892 KB |
Output is correct |
12 |
Correct |
62 ms |
6764 KB |
Output is correct |
13 |
Correct |
64 ms |
6764 KB |
Output is correct |
14 |
Correct |
61 ms |
6764 KB |
Output is correct |
15 |
Correct |
71 ms |
6764 KB |
Output is correct |
16 |
Correct |
64 ms |
6764 KB |
Output is correct |
17 |
Correct |
62 ms |
6764 KB |
Output is correct |
18 |
Correct |
649 ms |
63212 KB |
Output is correct |
19 |
Correct |
678 ms |
63340 KB |
Output is correct |
20 |
Correct |
748 ms |
63340 KB |
Output is correct |
21 |
Correct |
63 ms |
6764 KB |
Output is correct |
22 |
Correct |
62 ms |
6764 KB |
Output is correct |
23 |
Correct |
122 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
66 ms |
6892 KB |
Output is correct |
3 |
Correct |
70 ms |
6892 KB |
Output is correct |
4 |
Correct |
713 ms |
63308 KB |
Output is correct |
5 |
Correct |
66 ms |
6764 KB |
Output is correct |
6 |
Correct |
67 ms |
6764 KB |
Output is correct |
7 |
Correct |
66 ms |
6764 KB |
Output is correct |
8 |
Correct |
66 ms |
6764 KB |
Output is correct |
9 |
Correct |
64 ms |
6892 KB |
Output is correct |
10 |
Correct |
66 ms |
6764 KB |
Output is correct |
11 |
Correct |
62 ms |
6764 KB |
Output is correct |
12 |
Correct |
62 ms |
6764 KB |
Output is correct |
13 |
Correct |
64 ms |
6764 KB |
Output is correct |
14 |
Correct |
685 ms |
63276 KB |
Output is correct |
15 |
Correct |
61 ms |
6892 KB |
Output is correct |
16 |
Correct |
659 ms |
63084 KB |
Output is correct |
17 |
Correct |
655 ms |
63340 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
7 ms |
1044 KB |
Output is correct |
3 |
Correct |
6 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
6 ms |
1004 KB |
Output is correct |
17 |
Correct |
63 ms |
6764 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
307 ms |
1004 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
660 ms |
63080 KB |
Output is correct |
31 |
Correct |
700 ms |
63340 KB |
Output is correct |
32 |
Correct |
669 ms |
63324 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
664 ms |
63084 KB |
Output is correct |
35 |
Correct |
670 ms |
63084 KB |
Output is correct |
36 |
Correct |
669 ms |
63260 KB |
Output is correct |
37 |
Correct |
726 ms |
63212 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
60 ms |
6752 KB |
Output is correct |
40 |
Correct |
60 ms |
6764 KB |
Output is correct |
41 |
Correct |
64 ms |
6764 KB |
Output is correct |
42 |
Correct |
65 ms |
6804 KB |
Output is correct |
43 |
Correct |
62 ms |
6892 KB |
Output is correct |
44 |
Correct |
63 ms |
6892 KB |
Output is correct |
45 |
Correct |
62 ms |
6764 KB |
Output is correct |
46 |
Correct |
64 ms |
6764 KB |
Output is correct |
47 |
Correct |
61 ms |
6764 KB |
Output is correct |
48 |
Correct |
71 ms |
6764 KB |
Output is correct |
49 |
Correct |
64 ms |
6764 KB |
Output is correct |
50 |
Correct |
62 ms |
6764 KB |
Output is correct |
51 |
Correct |
649 ms |
63212 KB |
Output is correct |
52 |
Correct |
678 ms |
63340 KB |
Output is correct |
53 |
Correct |
748 ms |
63340 KB |
Output is correct |
54 |
Correct |
63 ms |
6764 KB |
Output is correct |
55 |
Correct |
62 ms |
6764 KB |
Output is correct |
56 |
Correct |
122 ms |
620 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
66 ms |
6892 KB |
Output is correct |
59 |
Correct |
70 ms |
6892 KB |
Output is correct |
60 |
Correct |
713 ms |
63308 KB |
Output is correct |
61 |
Correct |
66 ms |
6764 KB |
Output is correct |
62 |
Correct |
67 ms |
6764 KB |
Output is correct |
63 |
Correct |
66 ms |
6764 KB |
Output is correct |
64 |
Correct |
66 ms |
6764 KB |
Output is correct |
65 |
Correct |
64 ms |
6892 KB |
Output is correct |
66 |
Correct |
66 ms |
6764 KB |
Output is correct |
67 |
Correct |
62 ms |
6764 KB |
Output is correct |
68 |
Correct |
62 ms |
6764 KB |
Output is correct |
69 |
Correct |
64 ms |
6764 KB |
Output is correct |
70 |
Correct |
685 ms |
63276 KB |
Output is correct |
71 |
Correct |
61 ms |
6892 KB |
Output is correct |
72 |
Correct |
659 ms |
63084 KB |
Output is correct |
73 |
Correct |
655 ms |
63340 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
7 ms |
1388 KB |
Output is correct |
80 |
Correct |
720 ms |
67180 KB |
Output is correct |
81 |
Correct |
682 ms |
67280 KB |
Output is correct |
82 |
Correct |
714 ms |
67292 KB |
Output is correct |
83 |
Correct |
691 ms |
63340 KB |
Output is correct |
84 |
Correct |
739 ms |
67052 KB |
Output is correct |
85 |
Correct |
694 ms |
67052 KB |
Output is correct |
86 |
Correct |
136 ms |
4332 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |