#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define all(x) x.begin(),x.end()
using ll=long long;
const ll INF=1LL<<61;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,A,B;
cin>>n>>A>>B;
ll g=__gcd(A,B+1);
A/=g;
ll rp=A*B;
if((INF/A)/B==0LL){
rp=INF;
}
vector<pair<ll,ll>> segs;
rep(i,n){
ll l,r;
cin>>l>>r;
r++;
if(r-l>=rp){
cout<<rp<<"\n";
return 0;
}
l%=rp;
r%=rp;
if(l<r){
segs.emplace_back(pair<ll,ll>(l,r));
}
if(l>r){
segs.emplace_back(pair<ll,ll>(l,rp));
segs.emplace_back(pair<ll,ll>(0,r));
}
}
sort(all(segs));
ll lf=0,ri=0;
ll ans=0;
for(auto &[nl,nr]:segs){
if(nl<ri){
ri=max(ri,nr);
}
else{
ans+=ri-lf;
lf=nl;
ri=nr;
}
}
ans+=ri-lf;
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
4 ms |
1112 KB |
Output is correct |
17 |
Correct |
36 ms |
5836 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
220 ms |
35552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
351 ms |
44280 KB |
Output is correct |
3 |
Correct |
297 ms |
44696 KB |
Output is correct |
4 |
Correct |
314 ms |
53600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
351 ms |
44280 KB |
Output is correct |
3 |
Correct |
297 ms |
44696 KB |
Output is correct |
4 |
Correct |
314 ms |
53600 KB |
Output is correct |
5 |
Correct |
0 ms |
680 KB |
Output is correct |
6 |
Correct |
306 ms |
53404 KB |
Output is correct |
7 |
Correct |
321 ms |
53216 KB |
Output is correct |
8 |
Correct |
320 ms |
53488 KB |
Output is correct |
9 |
Correct |
333 ms |
53100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
351 ms |
44280 KB |
Output is correct |
3 |
Correct |
297 ms |
44696 KB |
Output is correct |
4 |
Correct |
314 ms |
53600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
31 ms |
5604 KB |
Output is correct |
7 |
Correct |
35 ms |
5836 KB |
Output is correct |
8 |
Correct |
30 ms |
5836 KB |
Output is correct |
9 |
Correct |
37 ms |
5860 KB |
Output is correct |
10 |
Correct |
31 ms |
5836 KB |
Output is correct |
11 |
Correct |
33 ms |
5616 KB |
Output is correct |
12 |
Correct |
31 ms |
5832 KB |
Output is correct |
13 |
Correct |
32 ms |
5664 KB |
Output is correct |
14 |
Correct |
30 ms |
5836 KB |
Output is correct |
15 |
Correct |
36 ms |
5832 KB |
Output is correct |
16 |
Correct |
41 ms |
5828 KB |
Output is correct |
17 |
Correct |
32 ms |
5840 KB |
Output is correct |
18 |
Correct |
315 ms |
53184 KB |
Output is correct |
19 |
Correct |
316 ms |
53768 KB |
Output is correct |
20 |
Correct |
327 ms |
53232 KB |
Output is correct |
21 |
Correct |
33 ms |
5844 KB |
Output is correct |
22 |
Correct |
28 ms |
5832 KB |
Output is correct |
23 |
Correct |
88 ms |
19260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
37 ms |
5596 KB |
Output is correct |
3 |
Correct |
38 ms |
5588 KB |
Output is correct |
4 |
Correct |
371 ms |
48852 KB |
Output is correct |
5 |
Correct |
38 ms |
5660 KB |
Output is correct |
6 |
Correct |
45 ms |
5564 KB |
Output is correct |
7 |
Correct |
35 ms |
5580 KB |
Output is correct |
8 |
Correct |
47 ms |
5572 KB |
Output is correct |
9 |
Correct |
51 ms |
5832 KB |
Output is correct |
10 |
Correct |
48 ms |
5836 KB |
Output is correct |
11 |
Correct |
40 ms |
5816 KB |
Output is correct |
12 |
Correct |
28 ms |
5844 KB |
Output is correct |
13 |
Correct |
45 ms |
5732 KB |
Output is correct |
14 |
Correct |
332 ms |
51308 KB |
Output is correct |
15 |
Correct |
41 ms |
5796 KB |
Output is correct |
16 |
Correct |
314 ms |
53348 KB |
Output is correct |
17 |
Correct |
307 ms |
53108 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
4 ms |
1112 KB |
Output is correct |
17 |
Correct |
36 ms |
5836 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
220 ms |
35552 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
351 ms |
44280 KB |
Output is correct |
31 |
Correct |
297 ms |
44696 KB |
Output is correct |
32 |
Correct |
314 ms |
53600 KB |
Output is correct |
33 |
Correct |
0 ms |
680 KB |
Output is correct |
34 |
Correct |
306 ms |
53404 KB |
Output is correct |
35 |
Correct |
321 ms |
53216 KB |
Output is correct |
36 |
Correct |
320 ms |
53488 KB |
Output is correct |
37 |
Correct |
333 ms |
53100 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
31 ms |
5604 KB |
Output is correct |
40 |
Correct |
35 ms |
5836 KB |
Output is correct |
41 |
Correct |
30 ms |
5836 KB |
Output is correct |
42 |
Correct |
37 ms |
5860 KB |
Output is correct |
43 |
Correct |
31 ms |
5836 KB |
Output is correct |
44 |
Correct |
33 ms |
5616 KB |
Output is correct |
45 |
Correct |
31 ms |
5832 KB |
Output is correct |
46 |
Correct |
32 ms |
5664 KB |
Output is correct |
47 |
Correct |
30 ms |
5836 KB |
Output is correct |
48 |
Correct |
36 ms |
5832 KB |
Output is correct |
49 |
Correct |
41 ms |
5828 KB |
Output is correct |
50 |
Correct |
32 ms |
5840 KB |
Output is correct |
51 |
Correct |
315 ms |
53184 KB |
Output is correct |
52 |
Correct |
316 ms |
53768 KB |
Output is correct |
53 |
Correct |
327 ms |
53232 KB |
Output is correct |
54 |
Correct |
33 ms |
5844 KB |
Output is correct |
55 |
Correct |
28 ms |
5832 KB |
Output is correct |
56 |
Correct |
88 ms |
19260 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
5596 KB |
Output is correct |
59 |
Correct |
38 ms |
5588 KB |
Output is correct |
60 |
Correct |
371 ms |
48852 KB |
Output is correct |
61 |
Correct |
38 ms |
5660 KB |
Output is correct |
62 |
Correct |
45 ms |
5564 KB |
Output is correct |
63 |
Correct |
35 ms |
5580 KB |
Output is correct |
64 |
Correct |
47 ms |
5572 KB |
Output is correct |
65 |
Correct |
51 ms |
5832 KB |
Output is correct |
66 |
Correct |
48 ms |
5836 KB |
Output is correct |
67 |
Correct |
40 ms |
5816 KB |
Output is correct |
68 |
Correct |
28 ms |
5844 KB |
Output is correct |
69 |
Correct |
45 ms |
5732 KB |
Output is correct |
70 |
Correct |
332 ms |
51308 KB |
Output is correct |
71 |
Correct |
41 ms |
5796 KB |
Output is correct |
72 |
Correct |
314 ms |
53348 KB |
Output is correct |
73 |
Correct |
307 ms |
53108 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
344 KB |
Output is correct |
76 |
Correct |
1 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
3 ms |
1204 KB |
Output is correct |
80 |
Correct |
377 ms |
54036 KB |
Output is correct |
81 |
Correct |
378 ms |
53932 KB |
Output is correct |
82 |
Correct |
367 ms |
53248 KB |
Output is correct |
83 |
Correct |
350 ms |
52988 KB |
Output is correct |
84 |
Correct |
352 ms |
53400 KB |
Output is correct |
85 |
Correct |
328 ms |
53408 KB |
Output is correct |
86 |
Correct |
95 ms |
19864 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |