#include<bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int MAX=1e18;
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, a, b; cin >> n >> a >> b;
int mod;
if(a/__gcd(a, b+1)>(MAX+b-1)/b) mod=MAX+1;
else mod=a/__gcd(a, b+1)*b;
priority_queue<pii, vector<pii>, greater<pii>> to_ans;
int ans=0;
while(n--) {
int l, r; cin >> l >> r;
if((l+mod-1)/mod*mod+mod-1<=r) {cout << mod << '\n'; return 0;}
else if((l+mod-1)/mod*mod<=r && l%mod) to_ans.push({l%mod, mod-1}), to_ans.push({0, r%mod});
else to_ans.push({l%mod, r%mod});
}
while(!to_ans.empty()) {
int l=to_ans.top().first, r=to_ans.top().second;
to_ans.pop();
while(!to_ans.empty() && to_ans.top().first<=r) r=max(r, to_ans.top().second), to_ans.pop();
ans+=r-l+1;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
4 ms |
1060 KB |
Output is correct |
3 |
Correct |
4 ms |
984 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
976 KB |
Output is correct |
17 |
Correct |
36 ms |
4532 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
311 ms |
41204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
352 ms |
16808 KB |
Output is correct |
3 |
Correct |
359 ms |
23896 KB |
Output is correct |
4 |
Correct |
360 ms |
23876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
352 ms |
16808 KB |
Output is correct |
3 |
Correct |
359 ms |
23896 KB |
Output is correct |
4 |
Correct |
360 ms |
23876 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
376 ms |
23840 KB |
Output is correct |
7 |
Correct |
376 ms |
23872 KB |
Output is correct |
8 |
Correct |
368 ms |
23832 KB |
Output is correct |
9 |
Correct |
379 ms |
23888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
352 ms |
16808 KB |
Output is correct |
3 |
Correct |
359 ms |
23896 KB |
Output is correct |
4 |
Correct |
360 ms |
23876 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
35 ms |
5696 KB |
Output is correct |
7 |
Correct |
37 ms |
5620 KB |
Output is correct |
8 |
Correct |
36 ms |
5680 KB |
Output is correct |
9 |
Correct |
34 ms |
5600 KB |
Output is correct |
10 |
Correct |
37 ms |
5648 KB |
Output is correct |
11 |
Correct |
48 ms |
5688 KB |
Output is correct |
12 |
Correct |
39 ms |
5612 KB |
Output is correct |
13 |
Correct |
36 ms |
5644 KB |
Output is correct |
14 |
Correct |
39 ms |
5748 KB |
Output is correct |
15 |
Correct |
41 ms |
5740 KB |
Output is correct |
16 |
Correct |
36 ms |
5668 KB |
Output is correct |
17 |
Correct |
34 ms |
5688 KB |
Output is correct |
18 |
Correct |
375 ms |
23944 KB |
Output is correct |
19 |
Correct |
393 ms |
23836 KB |
Output is correct |
20 |
Correct |
338 ms |
23904 KB |
Output is correct |
21 |
Correct |
33 ms |
5700 KB |
Output is correct |
22 |
Correct |
33 ms |
5676 KB |
Output is correct |
23 |
Correct |
121 ms |
18488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
33 ms |
2876 KB |
Output is correct |
3 |
Correct |
34 ms |
5700 KB |
Output is correct |
4 |
Correct |
338 ms |
23936 KB |
Output is correct |
5 |
Correct |
32 ms |
5700 KB |
Output is correct |
6 |
Correct |
33 ms |
5676 KB |
Output is correct |
7 |
Correct |
32 ms |
5664 KB |
Output is correct |
8 |
Correct |
33 ms |
5676 KB |
Output is correct |
9 |
Correct |
33 ms |
5724 KB |
Output is correct |
10 |
Correct |
33 ms |
5660 KB |
Output is correct |
11 |
Correct |
33 ms |
5612 KB |
Output is correct |
12 |
Correct |
33 ms |
5708 KB |
Output is correct |
13 |
Correct |
37 ms |
5616 KB |
Output is correct |
14 |
Correct |
330 ms |
23856 KB |
Output is correct |
15 |
Correct |
36 ms |
5824 KB |
Output is correct |
16 |
Correct |
347 ms |
23948 KB |
Output is correct |
17 |
Correct |
356 ms |
24612 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
4 ms |
1060 KB |
Output is correct |
3 |
Correct |
4 ms |
984 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
976 KB |
Output is correct |
17 |
Correct |
36 ms |
4532 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
311 ms |
41204 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
352 ms |
16808 KB |
Output is correct |
31 |
Correct |
359 ms |
23896 KB |
Output is correct |
32 |
Correct |
360 ms |
23876 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
376 ms |
23840 KB |
Output is correct |
35 |
Correct |
376 ms |
23872 KB |
Output is correct |
36 |
Correct |
368 ms |
23832 KB |
Output is correct |
37 |
Correct |
379 ms |
23888 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
35 ms |
5696 KB |
Output is correct |
40 |
Correct |
37 ms |
5620 KB |
Output is correct |
41 |
Correct |
36 ms |
5680 KB |
Output is correct |
42 |
Correct |
34 ms |
5600 KB |
Output is correct |
43 |
Correct |
37 ms |
5648 KB |
Output is correct |
44 |
Correct |
48 ms |
5688 KB |
Output is correct |
45 |
Correct |
39 ms |
5612 KB |
Output is correct |
46 |
Correct |
36 ms |
5644 KB |
Output is correct |
47 |
Correct |
39 ms |
5748 KB |
Output is correct |
48 |
Correct |
41 ms |
5740 KB |
Output is correct |
49 |
Correct |
36 ms |
5668 KB |
Output is correct |
50 |
Correct |
34 ms |
5688 KB |
Output is correct |
51 |
Correct |
375 ms |
23944 KB |
Output is correct |
52 |
Correct |
393 ms |
23836 KB |
Output is correct |
53 |
Correct |
338 ms |
23904 KB |
Output is correct |
54 |
Correct |
33 ms |
5700 KB |
Output is correct |
55 |
Correct |
33 ms |
5676 KB |
Output is correct |
56 |
Correct |
121 ms |
18488 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
33 ms |
2876 KB |
Output is correct |
59 |
Correct |
34 ms |
5700 KB |
Output is correct |
60 |
Correct |
338 ms |
23936 KB |
Output is correct |
61 |
Correct |
32 ms |
5700 KB |
Output is correct |
62 |
Correct |
33 ms |
5676 KB |
Output is correct |
63 |
Correct |
32 ms |
5664 KB |
Output is correct |
64 |
Correct |
33 ms |
5676 KB |
Output is correct |
65 |
Correct |
33 ms |
5724 KB |
Output is correct |
66 |
Correct |
33 ms |
5660 KB |
Output is correct |
67 |
Correct |
33 ms |
5612 KB |
Output is correct |
68 |
Correct |
33 ms |
5708 KB |
Output is correct |
69 |
Correct |
37 ms |
5616 KB |
Output is correct |
70 |
Correct |
330 ms |
23856 KB |
Output is correct |
71 |
Correct |
36 ms |
5824 KB |
Output is correct |
72 |
Correct |
347 ms |
23948 KB |
Output is correct |
73 |
Correct |
356 ms |
24612 KB |
Output is correct |
74 |
Correct |
1 ms |
320 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
984 KB |
Output is correct |
80 |
Correct |
350 ms |
24992 KB |
Output is correct |
81 |
Correct |
354 ms |
25096 KB |
Output is correct |
82 |
Correct |
341 ms |
25004 KB |
Output is correct |
83 |
Correct |
344 ms |
25096 KB |
Output is correct |
84 |
Correct |
338 ms |
24924 KB |
Output is correct |
85 |
Correct |
338 ms |
24932 KB |
Output is correct |
86 |
Correct |
117 ms |
18488 KB |
Output is correct |
87 |
Correct |
0 ms |
212 KB |
Output is correct |