#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using ll = long long;
using i128 = __int128;
int main() {
// freopen("input.txt", "r", stdin);
int n;
ll A, B;
std::cin >> n >> A >> B;
if ((i128) A * B / std::__gcd(A, B + 1) > 1e18) {
ll answer = 0;
for (int i = 0; i < n; i++) {
ll l, r;
std::cin >> l >> r;
answer += r - l + 1;
}
std::cout << answer;
return 0;
}
ll R = (i128) A * B / std::__gcd(A, B + 1);
std::vector<std::pair<ll, int>> events;
bool coverAll = false;
for (int i = 0; i < n; i++) {
ll l, r;
std::cin >> l >> r;
if (r - l + 1 >= R) {
coverAll = true;
}
l %= R;
r %= R;
if (l <= r) {
events.push_back({l, +1});
events.push_back({r + 1, -1});
} else {
events.push_back({0, +1});
events.push_back({r + 1, -1});
events.push_back({l, +1});
events.push_back({R, -1});
}
}
if (coverAll) {
std::cout << R;
return 0;
}
events.push_back({R, 0});
std::sort(events.begin(), events.end());
i128 last = 0;
int delta = 0;
ll answer = 0;
for (const auto &[pos, add] : events) {
if (delta >= 1) {
answer += pos - last;
}
delta += add;
last = pos;
}
std::cout << answer;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
10 ms |
1240 KB |
Output is correct |
3 |
Correct |
10 ms |
1496 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
1208 KB |
Output is correct |
17 |
Correct |
99 ms |
7304 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
772 ms |
56912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1119 ms |
68960 KB |
Output is correct |
3 |
Correct |
1083 ms |
68868 KB |
Output is correct |
4 |
Correct |
1037 ms |
69228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1119 ms |
68960 KB |
Output is correct |
3 |
Correct |
1083 ms |
68868 KB |
Output is correct |
4 |
Correct |
1037 ms |
69228 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1070 ms |
68920 KB |
Output is correct |
7 |
Correct |
1092 ms |
69052 KB |
Output is correct |
8 |
Correct |
1075 ms |
68976 KB |
Output is correct |
9 |
Correct |
1041 ms |
68688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1119 ms |
68960 KB |
Output is correct |
3 |
Correct |
1083 ms |
68868 KB |
Output is correct |
4 |
Correct |
1037 ms |
69228 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
109 ms |
7164 KB |
Output is correct |
7 |
Correct |
129 ms |
7264 KB |
Output is correct |
8 |
Correct |
100 ms |
7160 KB |
Output is correct |
9 |
Correct |
124 ms |
7356 KB |
Output is correct |
10 |
Correct |
126 ms |
7216 KB |
Output is correct |
11 |
Correct |
94 ms |
7180 KB |
Output is correct |
12 |
Correct |
110 ms |
7404 KB |
Output is correct |
13 |
Correct |
105 ms |
7352 KB |
Output is correct |
14 |
Correct |
121 ms |
7156 KB |
Output is correct |
15 |
Correct |
105 ms |
7224 KB |
Output is correct |
16 |
Correct |
109 ms |
7160 KB |
Output is correct |
17 |
Correct |
106 ms |
7348 KB |
Output is correct |
18 |
Correct |
1174 ms |
68976 KB |
Output is correct |
19 |
Correct |
1101 ms |
68864 KB |
Output is correct |
20 |
Correct |
1078 ms |
68980 KB |
Output is correct |
21 |
Correct |
110 ms |
7256 KB |
Output is correct |
22 |
Correct |
136 ms |
7232 KB |
Output is correct |
23 |
Correct |
335 ms |
26792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
110 ms |
7208 KB |
Output is correct |
3 |
Correct |
121 ms |
7284 KB |
Output is correct |
4 |
Correct |
1188 ms |
68892 KB |
Output is correct |
5 |
Correct |
109 ms |
7356 KB |
Output is correct |
6 |
Correct |
122 ms |
7356 KB |
Output is correct |
7 |
Correct |
119 ms |
7384 KB |
Output is correct |
8 |
Correct |
141 ms |
7412 KB |
Output is correct |
9 |
Correct |
108 ms |
7352 KB |
Output is correct |
10 |
Correct |
108 ms |
7352 KB |
Output is correct |
11 |
Correct |
114 ms |
7348 KB |
Output is correct |
12 |
Correct |
103 ms |
7192 KB |
Output is correct |
13 |
Correct |
107 ms |
7348 KB |
Output is correct |
14 |
Correct |
1167 ms |
68824 KB |
Output is correct |
15 |
Correct |
101 ms |
7252 KB |
Output is correct |
16 |
Correct |
1032 ms |
68980 KB |
Output is correct |
17 |
Correct |
1049 ms |
68988 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
10 ms |
1240 KB |
Output is correct |
3 |
Correct |
10 ms |
1496 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
1208 KB |
Output is correct |
17 |
Correct |
99 ms |
7304 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
772 ms |
56912 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1119 ms |
68960 KB |
Output is correct |
31 |
Correct |
1083 ms |
68868 KB |
Output is correct |
32 |
Correct |
1037 ms |
69228 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1070 ms |
68920 KB |
Output is correct |
35 |
Correct |
1092 ms |
69052 KB |
Output is correct |
36 |
Correct |
1075 ms |
68976 KB |
Output is correct |
37 |
Correct |
1041 ms |
68688 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
109 ms |
7164 KB |
Output is correct |
40 |
Correct |
129 ms |
7264 KB |
Output is correct |
41 |
Correct |
100 ms |
7160 KB |
Output is correct |
42 |
Correct |
124 ms |
7356 KB |
Output is correct |
43 |
Correct |
126 ms |
7216 KB |
Output is correct |
44 |
Correct |
94 ms |
7180 KB |
Output is correct |
45 |
Correct |
110 ms |
7404 KB |
Output is correct |
46 |
Correct |
105 ms |
7352 KB |
Output is correct |
47 |
Correct |
121 ms |
7156 KB |
Output is correct |
48 |
Correct |
105 ms |
7224 KB |
Output is correct |
49 |
Correct |
109 ms |
7160 KB |
Output is correct |
50 |
Correct |
106 ms |
7348 KB |
Output is correct |
51 |
Correct |
1174 ms |
68976 KB |
Output is correct |
52 |
Correct |
1101 ms |
68864 KB |
Output is correct |
53 |
Correct |
1078 ms |
68980 KB |
Output is correct |
54 |
Correct |
110 ms |
7256 KB |
Output is correct |
55 |
Correct |
136 ms |
7232 KB |
Output is correct |
56 |
Correct |
335 ms |
26792 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
110 ms |
7208 KB |
Output is correct |
59 |
Correct |
121 ms |
7284 KB |
Output is correct |
60 |
Correct |
1188 ms |
68892 KB |
Output is correct |
61 |
Correct |
109 ms |
7356 KB |
Output is correct |
62 |
Correct |
122 ms |
7356 KB |
Output is correct |
63 |
Correct |
119 ms |
7384 KB |
Output is correct |
64 |
Correct |
141 ms |
7412 KB |
Output is correct |
65 |
Correct |
108 ms |
7352 KB |
Output is correct |
66 |
Correct |
108 ms |
7352 KB |
Output is correct |
67 |
Correct |
114 ms |
7348 KB |
Output is correct |
68 |
Correct |
103 ms |
7192 KB |
Output is correct |
69 |
Correct |
107 ms |
7348 KB |
Output is correct |
70 |
Correct |
1167 ms |
68824 KB |
Output is correct |
71 |
Correct |
101 ms |
7252 KB |
Output is correct |
72 |
Correct |
1032 ms |
68980 KB |
Output is correct |
73 |
Correct |
1049 ms |
68988 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
10 ms |
1240 KB |
Output is correct |
80 |
Correct |
1092 ms |
68880 KB |
Output is correct |
81 |
Correct |
1038 ms |
68980 KB |
Output is correct |
82 |
Correct |
1032 ms |
69032 KB |
Output is correct |
83 |
Correct |
1081 ms |
68940 KB |
Output is correct |
84 |
Correct |
1071 ms |
68976 KB |
Output is correct |
85 |
Correct |
1081 ms |
68836 KB |
Output is correct |
86 |
Correct |
333 ms |
26684 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |