#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1e18 + 100;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 n, A, B;
std::cin >> n >> A >> B;
i64 gcd = std::__gcd(A, B + 1);
i64 cycle = A / gcd;
if (A / gcd > inf / B) {
cycle = inf;
} else {
cycle *= B;
}
std::vector<std::pair<i64, i64>> v;
for (int i = 0; i < n; i++) {
i64 l, r;
std::cin >> l >> r;
if (r - l + 1 >= cycle) {
v.emplace_back(0, cycle - 1);
}
l %= cycle;
r %= cycle;
if (l <= r) {
v.emplace_back(l, r);
} else {
v.emplace_back(0, r);
v.emplace_back(l, cycle - 1);
}
}
sort(v.begin(), v.end());
std::vector<std::pair<i64, i64>> nw;
nw.push_back(v[0]);
for (int i = 1; i < int(v.size()); i++) {
if (nw[nw.size() - 1].second >= v[i].first) {
nw[nw.size() - 1].first = std::min(nw[nw.size() - 1].first, v[i].first);
nw[nw.size() - 1].second = std::max(nw[nw.size() - 1].second, v[i].second);
} else {
nw.emplace_back(v[i]);
}
}
i64 ans = 0;
for (auto [l, r] : nw) {
ans += r - l + 1;
}
std::cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
872 KB |
Output is correct |
3 |
Correct |
6 ms |
848 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
832 KB |
Output is correct |
17 |
Correct |
54 ms |
4840 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
349 ms |
16820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
500 ms |
40568 KB |
Output is correct |
3 |
Correct |
500 ms |
40512 KB |
Output is correct |
4 |
Correct |
503 ms |
40720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
500 ms |
40568 KB |
Output is correct |
3 |
Correct |
500 ms |
40512 KB |
Output is correct |
4 |
Correct |
503 ms |
40720 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
490 ms |
40612 KB |
Output is correct |
7 |
Correct |
510 ms |
40544 KB |
Output is correct |
8 |
Correct |
492 ms |
40684 KB |
Output is correct |
9 |
Correct |
655 ms |
40564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
500 ms |
40568 KB |
Output is correct |
3 |
Correct |
500 ms |
40512 KB |
Output is correct |
4 |
Correct |
503 ms |
40720 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
50 ms |
4960 KB |
Output is correct |
7 |
Correct |
51 ms |
4908 KB |
Output is correct |
8 |
Correct |
47 ms |
4904 KB |
Output is correct |
9 |
Correct |
50 ms |
4924 KB |
Output is correct |
10 |
Correct |
49 ms |
4920 KB |
Output is correct |
11 |
Correct |
60 ms |
4912 KB |
Output is correct |
12 |
Correct |
49 ms |
4876 KB |
Output is correct |
13 |
Correct |
51 ms |
4924 KB |
Output is correct |
14 |
Correct |
48 ms |
4836 KB |
Output is correct |
15 |
Correct |
58 ms |
4848 KB |
Output is correct |
16 |
Correct |
53 ms |
4880 KB |
Output is correct |
17 |
Correct |
51 ms |
4936 KB |
Output is correct |
18 |
Correct |
500 ms |
40612 KB |
Output is correct |
19 |
Correct |
487 ms |
40668 KB |
Output is correct |
20 |
Correct |
544 ms |
40592 KB |
Output is correct |
21 |
Correct |
53 ms |
4936 KB |
Output is correct |
22 |
Correct |
54 ms |
4864 KB |
Output is correct |
23 |
Correct |
150 ms |
8592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
53 ms |
4944 KB |
Output is correct |
3 |
Correct |
52 ms |
4868 KB |
Output is correct |
4 |
Correct |
577 ms |
40596 KB |
Output is correct |
5 |
Correct |
63 ms |
4920 KB |
Output is correct |
6 |
Correct |
65 ms |
4904 KB |
Output is correct |
7 |
Correct |
63 ms |
4924 KB |
Output is correct |
8 |
Correct |
53 ms |
4944 KB |
Output is correct |
9 |
Correct |
51 ms |
4892 KB |
Output is correct |
10 |
Correct |
54 ms |
4956 KB |
Output is correct |
11 |
Correct |
52 ms |
4948 KB |
Output is correct |
12 |
Correct |
45 ms |
4880 KB |
Output is correct |
13 |
Correct |
52 ms |
4920 KB |
Output is correct |
14 |
Correct |
582 ms |
40548 KB |
Output is correct |
15 |
Correct |
51 ms |
4920 KB |
Output is correct |
16 |
Correct |
497 ms |
40684 KB |
Output is correct |
17 |
Correct |
482 ms |
40464 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
872 KB |
Output is correct |
3 |
Correct |
6 ms |
848 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
6 ms |
832 KB |
Output is correct |
17 |
Correct |
54 ms |
4840 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
349 ms |
16820 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
500 ms |
40568 KB |
Output is correct |
31 |
Correct |
500 ms |
40512 KB |
Output is correct |
32 |
Correct |
503 ms |
40720 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
490 ms |
40612 KB |
Output is correct |
35 |
Correct |
510 ms |
40544 KB |
Output is correct |
36 |
Correct |
492 ms |
40684 KB |
Output is correct |
37 |
Correct |
655 ms |
40564 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
50 ms |
4960 KB |
Output is correct |
40 |
Correct |
51 ms |
4908 KB |
Output is correct |
41 |
Correct |
47 ms |
4904 KB |
Output is correct |
42 |
Correct |
50 ms |
4924 KB |
Output is correct |
43 |
Correct |
49 ms |
4920 KB |
Output is correct |
44 |
Correct |
60 ms |
4912 KB |
Output is correct |
45 |
Correct |
49 ms |
4876 KB |
Output is correct |
46 |
Correct |
51 ms |
4924 KB |
Output is correct |
47 |
Correct |
48 ms |
4836 KB |
Output is correct |
48 |
Correct |
58 ms |
4848 KB |
Output is correct |
49 |
Correct |
53 ms |
4880 KB |
Output is correct |
50 |
Correct |
51 ms |
4936 KB |
Output is correct |
51 |
Correct |
500 ms |
40612 KB |
Output is correct |
52 |
Correct |
487 ms |
40668 KB |
Output is correct |
53 |
Correct |
544 ms |
40592 KB |
Output is correct |
54 |
Correct |
53 ms |
4936 KB |
Output is correct |
55 |
Correct |
54 ms |
4864 KB |
Output is correct |
56 |
Correct |
150 ms |
8592 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
53 ms |
4944 KB |
Output is correct |
59 |
Correct |
52 ms |
4868 KB |
Output is correct |
60 |
Correct |
577 ms |
40596 KB |
Output is correct |
61 |
Correct |
63 ms |
4920 KB |
Output is correct |
62 |
Correct |
65 ms |
4904 KB |
Output is correct |
63 |
Correct |
63 ms |
4924 KB |
Output is correct |
64 |
Correct |
53 ms |
4944 KB |
Output is correct |
65 |
Correct |
51 ms |
4892 KB |
Output is correct |
66 |
Correct |
54 ms |
4956 KB |
Output is correct |
67 |
Correct |
52 ms |
4948 KB |
Output is correct |
68 |
Correct |
45 ms |
4880 KB |
Output is correct |
69 |
Correct |
52 ms |
4920 KB |
Output is correct |
70 |
Correct |
582 ms |
40548 KB |
Output is correct |
71 |
Correct |
51 ms |
4920 KB |
Output is correct |
72 |
Correct |
497 ms |
40684 KB |
Output is correct |
73 |
Correct |
482 ms |
40464 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
1 ms |
316 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
6 ms |
1232 KB |
Output is correct |
80 |
Correct |
569 ms |
60488 KB |
Output is correct |
81 |
Correct |
581 ms |
60580 KB |
Output is correct |
82 |
Correct |
556 ms |
60560 KB |
Output is correct |
83 |
Correct |
540 ms |
60672 KB |
Output is correct |
84 |
Correct |
549 ms |
60552 KB |
Output is correct |
85 |
Correct |
539 ms |
60524 KB |
Output is correct |
86 |
Correct |
162 ms |
18484 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |