#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 2e18 + 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 (cycle > 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;
l %= cycle;
r %= cycle;
if (r - l + 1 >= cycle) {
std::cout << cycle << "\n";
return 0;
}
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
848 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 |
5 ms |
848 KB |
Output is correct |
17 |
Correct |
53 ms |
4884 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
365 ms |
16848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
499 ms |
40500 KB |
Output is correct |
3 |
Correct |
491 ms |
40680 KB |
Output is correct |
4 |
Correct |
500 ms |
40592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
499 ms |
40500 KB |
Output is correct |
3 |
Correct |
491 ms |
40680 KB |
Output is correct |
4 |
Correct |
500 ms |
40592 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
492 ms |
40552 KB |
Output is correct |
7 |
Correct |
493 ms |
40712 KB |
Output is correct |
8 |
Correct |
504 ms |
40708 KB |
Output is correct |
9 |
Correct |
526 ms |
40628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
499 ms |
40500 KB |
Output is correct |
3 |
Correct |
491 ms |
40680 KB |
Output is correct |
4 |
Correct |
500 ms |
40592 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
61 ms |
4920 KB |
Output is correct |
7 |
Correct |
68 ms |
4912 KB |
Output is correct |
8 |
Correct |
49 ms |
4864 KB |
Output is correct |
9 |
Correct |
48 ms |
4924 KB |
Output is correct |
10 |
Correct |
48 ms |
4932 KB |
Output is correct |
11 |
Correct |
50 ms |
4920 KB |
Output is correct |
12 |
Correct |
47 ms |
4840 KB |
Output is correct |
13 |
Correct |
50 ms |
4840 KB |
Output is correct |
14 |
Correct |
55 ms |
4940 KB |
Output is correct |
15 |
Correct |
65 ms |
4884 KB |
Output is correct |
16 |
Correct |
50 ms |
4924 KB |
Output is correct |
17 |
Correct |
49 ms |
4916 KB |
Output is correct |
18 |
Correct |
492 ms |
40588 KB |
Output is correct |
19 |
Correct |
476 ms |
40696 KB |
Output is correct |
20 |
Correct |
552 ms |
40548 KB |
Output is correct |
21 |
Correct |
54 ms |
4888 KB |
Output is correct |
22 |
Correct |
54 ms |
4928 KB |
Output is correct |
23 |
Correct |
150 ms |
8624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
52 ms |
4920 KB |
Output is correct |
3 |
Correct |
60 ms |
4936 KB |
Output is correct |
4 |
Correct |
616 ms |
40508 KB |
Output is correct |
5 |
Correct |
53 ms |
4952 KB |
Output is correct |
6 |
Correct |
55 ms |
4928 KB |
Output is correct |
7 |
Correct |
50 ms |
4924 KB |
Output is correct |
8 |
Correct |
51 ms |
4948 KB |
Output is correct |
9 |
Correct |
55 ms |
4888 KB |
Output is correct |
10 |
Correct |
51 ms |
4840 KB |
Output is correct |
11 |
Correct |
51 ms |
4956 KB |
Output is correct |
12 |
Correct |
48 ms |
4948 KB |
Output is correct |
13 |
Correct |
50 ms |
4888 KB |
Output is correct |
14 |
Correct |
523 ms |
40564 KB |
Output is correct |
15 |
Correct |
60 ms |
4896 KB |
Output is correct |
16 |
Correct |
482 ms |
40540 KB |
Output is correct |
17 |
Correct |
496 ms |
40552 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
848 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 |
5 ms |
848 KB |
Output is correct |
17 |
Correct |
53 ms |
4884 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |