#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10, inf=2e18;
int n, a, b;
int l[N], r[N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> a >> b;
for (int i=1; i<=n; ++i) cin >> l[i] >> r[i];
int mod=a/__gcd(b+1, a);
if (mod>inf/b){
int ans=0;
for (int i=1; i<=n; ++i) ans+=r[i]-l[i]+1;
cout << ans << '\n';
return 0;
}
mod*=b;
vector<pair<int, int>> v;
for (int i=1; i<=n; ++i){
if (r[i]-l[i]+1>=mod){
cout << mod << '\n';
return 0;
}
l[i]%=mod; r[i]%=mod;
if (l[i]>r[i]){
v.emplace_back(l[i], mod-1);
v.emplace_back(0, r[i]);
}else{
v.emplace_back(l[i], r[i]);
}
}
sort(v.begin(), v.end());
int ans=0;
int ll=0, rr=-1;
for (auto &i:v){
if (rr+1>=i.first) rr=max(rr, i.second);
else{
ans+=rr-ll+1;
ll=i.first, rr=i.second;
}
}
cout << ans+rr-ll+1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
4 ms |
3164 KB |
Output is correct |
3 |
Correct |
5 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3164 KB |
Output is correct |
17 |
Correct |
35 ms |
12708 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
1 ms |
2400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2400 KB |
Output is correct |
2 |
Correct |
1 ms |
2404 KB |
Output is correct |
3 |
Correct |
1 ms |
2528 KB |
Output is correct |
4 |
Correct |
1 ms |
2404 KB |
Output is correct |
5 |
Correct |
223 ms |
59624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2404 KB |
Output is correct |
2 |
Correct |
322 ms |
71008 KB |
Output is correct |
3 |
Correct |
313 ms |
70628 KB |
Output is correct |
4 |
Correct |
303 ms |
70996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2404 KB |
Output is correct |
2 |
Correct |
322 ms |
71008 KB |
Output is correct |
3 |
Correct |
313 ms |
70628 KB |
Output is correct |
4 |
Correct |
303 ms |
70996 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
324 ms |
71752 KB |
Output is correct |
7 |
Correct |
320 ms |
71516 KB |
Output is correct |
8 |
Correct |
309 ms |
70620 KB |
Output is correct |
9 |
Correct |
336 ms |
71652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2404 KB |
Output is correct |
2 |
Correct |
322 ms |
71008 KB |
Output is correct |
3 |
Correct |
313 ms |
70628 KB |
Output is correct |
4 |
Correct |
303 ms |
70996 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
31 ms |
12660 KB |
Output is correct |
7 |
Correct |
44 ms |
12496 KB |
Output is correct |
8 |
Correct |
30 ms |
12524 KB |
Output is correct |
9 |
Correct |
32 ms |
12496 KB |
Output is correct |
10 |
Correct |
32 ms |
12904 KB |
Output is correct |
11 |
Correct |
33 ms |
12492 KB |
Output is correct |
12 |
Correct |
31 ms |
12496 KB |
Output is correct |
13 |
Correct |
37 ms |
12492 KB |
Output is correct |
14 |
Correct |
35 ms |
12516 KB |
Output is correct |
15 |
Correct |
36 ms |
12656 KB |
Output is correct |
16 |
Correct |
34 ms |
12496 KB |
Output is correct |
17 |
Correct |
33 ms |
12652 KB |
Output is correct |
18 |
Correct |
327 ms |
70216 KB |
Output is correct |
19 |
Correct |
309 ms |
71104 KB |
Output is correct |
20 |
Correct |
350 ms |
70372 KB |
Output is correct |
21 |
Correct |
35 ms |
12496 KB |
Output is correct |
22 |
Correct |
31 ms |
12632 KB |
Output is correct |
23 |
Correct |
92 ms |
33368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
35 ms |
12652 KB |
Output is correct |
3 |
Correct |
35 ms |
12660 KB |
Output is correct |
4 |
Correct |
359 ms |
70884 KB |
Output is correct |
5 |
Correct |
35 ms |
12644 KB |
Output is correct |
6 |
Correct |
43 ms |
12680 KB |
Output is correct |
7 |
Correct |
33 ms |
12492 KB |
Output is correct |
8 |
Correct |
34 ms |
12548 KB |
Output is correct |
9 |
Correct |
35 ms |
12496 KB |
Output is correct |
10 |
Correct |
35 ms |
12500 KB |
Output is correct |
11 |
Correct |
34 ms |
12504 KB |
Output is correct |
12 |
Correct |
35 ms |
13012 KB |
Output is correct |
13 |
Correct |
36 ms |
12640 KB |
Output is correct |
14 |
Correct |
349 ms |
70740 KB |
Output is correct |
15 |
Correct |
36 ms |
12648 KB |
Output is correct |
16 |
Correct |
329 ms |
69956 KB |
Output is correct |
17 |
Correct |
309 ms |
70404 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
4 ms |
3164 KB |
Output is correct |
3 |
Correct |
5 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3164 KB |
Output is correct |
17 |
Correct |
35 ms |
12708 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2508 KB |
Output is correct |
23 |
Correct |
1 ms |
2400 KB |
Output is correct |
24 |
Correct |
1 ms |
2400 KB |
Output is correct |
25 |
Correct |
1 ms |
2404 KB |
Output is correct |
26 |
Correct |
1 ms |
2528 KB |
Output is correct |
27 |
Correct |
1 ms |
2404 KB |
Output is correct |
28 |
Correct |
223 ms |
59624 KB |
Output is correct |
29 |
Correct |
1 ms |
2404 KB |
Output is correct |
30 |
Correct |
322 ms |
71008 KB |
Output is correct |
31 |
Correct |
313 ms |
70628 KB |
Output is correct |
32 |
Correct |
303 ms |
70996 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
324 ms |
71752 KB |
Output is correct |
35 |
Correct |
320 ms |
71516 KB |
Output is correct |
36 |
Correct |
309 ms |
70620 KB |
Output is correct |
37 |
Correct |
336 ms |
71652 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
31 ms |
12660 KB |
Output is correct |
40 |
Correct |
44 ms |
12496 KB |
Output is correct |
41 |
Correct |
30 ms |
12524 KB |
Output is correct |
42 |
Correct |
32 ms |
12496 KB |
Output is correct |
43 |
Correct |
32 ms |
12904 KB |
Output is correct |
44 |
Correct |
33 ms |
12492 KB |
Output is correct |
45 |
Correct |
31 ms |
12496 KB |
Output is correct |
46 |
Correct |
37 ms |
12492 KB |
Output is correct |
47 |
Correct |
35 ms |
12516 KB |
Output is correct |
48 |
Correct |
36 ms |
12656 KB |
Output is correct |
49 |
Correct |
34 ms |
12496 KB |
Output is correct |
50 |
Correct |
33 ms |
12652 KB |
Output is correct |
51 |
Correct |
327 ms |
70216 KB |
Output is correct |
52 |
Correct |
309 ms |
71104 KB |
Output is correct |
53 |
Correct |
350 ms |
70372 KB |
Output is correct |
54 |
Correct |
35 ms |
12496 KB |
Output is correct |
55 |
Correct |
31 ms |
12632 KB |
Output is correct |
56 |
Correct |
92 ms |
33368 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
35 ms |
12652 KB |
Output is correct |
59 |
Correct |
35 ms |
12660 KB |
Output is correct |
60 |
Correct |
359 ms |
70884 KB |
Output is correct |
61 |
Correct |
35 ms |
12644 KB |
Output is correct |
62 |
Correct |
43 ms |
12680 KB |
Output is correct |
63 |
Correct |
33 ms |
12492 KB |
Output is correct |
64 |
Correct |
34 ms |
12548 KB |
Output is correct |
65 |
Correct |
35 ms |
12496 KB |
Output is correct |
66 |
Correct |
35 ms |
12500 KB |
Output is correct |
67 |
Correct |
34 ms |
12504 KB |
Output is correct |
68 |
Correct |
35 ms |
13012 KB |
Output is correct |
69 |
Correct |
36 ms |
12640 KB |
Output is correct |
70 |
Correct |
349 ms |
70740 KB |
Output is correct |
71 |
Correct |
36 ms |
12648 KB |
Output is correct |
72 |
Correct |
329 ms |
69956 KB |
Output is correct |
73 |
Correct |
309 ms |
70404 KB |
Output is correct |
74 |
Correct |
1 ms |
2392 KB |
Output is correct |
75 |
Correct |
1 ms |
2396 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2396 KB |
Output is correct |
78 |
Correct |
0 ms |
2396 KB |
Output is correct |
79 |
Correct |
4 ms |
3160 KB |
Output is correct |
80 |
Correct |
364 ms |
71312 KB |
Output is correct |
81 |
Correct |
375 ms |
70592 KB |
Output is correct |
82 |
Correct |
358 ms |
70732 KB |
Output is correct |
83 |
Correct |
346 ms |
71104 KB |
Output is correct |
84 |
Correct |
348 ms |
71236 KB |
Output is correct |
85 |
Correct |
347 ms |
70264 KB |
Output is correct |
86 |
Correct |
95 ms |
32600 KB |
Output is correct |
87 |
Correct |
1 ms |
2396 KB |
Output is correct |