#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
#define mp make_pair
#define pb push_back
#define sz(x) ((int) size(x))
const int infI = 1e9 + 7;
const ll infL = 3e18;
const int N = 1000001;
ll L[N], R[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
ll A, B;
cin >> A >> B;
for (int i = 0; i < n; ++i) {
cin >> L[i] >> R[i];
}
ll d = gcd(B + 1, A);
ll c = A / d;
if (LLONG_MAX / B <= c) {
ll ans = 0;
for (int i = 0; i < n; ++i) {
ans += R[i] - L[i] + 1;
}
cout << ans;
return 0;
}
vector<pair<ll, int>> v;
ll Z = B * c;
for (int i = 0; i < n; ++i) {
if (R[i] / Z == L[i] / Z) {
v.emplace_back(L[i] % Z, 0);
v.emplace_back(R[i] % Z, 1);
} else {
if ((R[i] - L[i] + 1) / B >= c) {
cout << Z;
return 0;
} else {
ll nxt = (L[i] + Z - 1) / Z * Z;
assert(nxt <= R[i]);
assert(R[i] / Z * Z == nxt);
v.emplace_back(L[i] % Z, 0);
v.emplace_back(Z - 1, 1);
v.emplace_back(0, 0);
v.emplace_back(R[i] % Z, 1);
}
}
}
ll last = 0;
int opened = 0;
sort(all(v));
ll ans = 0;
for (auto [x, t] : v) {
if (t == 0) {
++opened;
if (opened == 1) {
last = x;
}
} else {
--opened;
if (opened == 0) {
ans += x - last + 1;
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
1496 KB |
Output is correct |
3 |
Correct |
5 ms |
1496 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
1496 KB |
Output is correct |
17 |
Correct |
68 ms |
9756 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
362 ms |
74184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
542 ms |
86088 KB |
Output is correct |
3 |
Correct |
548 ms |
86104 KB |
Output is correct |
4 |
Correct |
502 ms |
86068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
542 ms |
86088 KB |
Output is correct |
3 |
Correct |
548 ms |
86104 KB |
Output is correct |
4 |
Correct |
502 ms |
86068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
547 ms |
86164 KB |
Output is correct |
7 |
Correct |
508 ms |
86172 KB |
Output is correct |
8 |
Correct |
545 ms |
86132 KB |
Output is correct |
9 |
Correct |
615 ms |
86120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
542 ms |
86088 KB |
Output is correct |
3 |
Correct |
548 ms |
86104 KB |
Output is correct |
4 |
Correct |
502 ms |
86068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
48 ms |
9760 KB |
Output is correct |
7 |
Correct |
59 ms |
9788 KB |
Output is correct |
8 |
Correct |
49 ms |
9772 KB |
Output is correct |
9 |
Correct |
57 ms |
9892 KB |
Output is correct |
10 |
Correct |
48 ms |
9796 KB |
Output is correct |
11 |
Correct |
57 ms |
9788 KB |
Output is correct |
12 |
Correct |
48 ms |
9816 KB |
Output is correct |
13 |
Correct |
61 ms |
9756 KB |
Output is correct |
14 |
Correct |
48 ms |
9816 KB |
Output is correct |
15 |
Correct |
62 ms |
9880 KB |
Output is correct |
16 |
Correct |
54 ms |
9764 KB |
Output is correct |
17 |
Correct |
53 ms |
9756 KB |
Output is correct |
18 |
Correct |
563 ms |
86068 KB |
Output is correct |
19 |
Correct |
498 ms |
86048 KB |
Output is correct |
20 |
Correct |
615 ms |
86148 KB |
Output is correct |
21 |
Correct |
56 ms |
9768 KB |
Output is correct |
22 |
Correct |
48 ms |
9844 KB |
Output is correct |
23 |
Correct |
146 ms |
34732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
53 ms |
9824 KB |
Output is correct |
3 |
Correct |
58 ms |
9764 KB |
Output is correct |
4 |
Correct |
657 ms |
86136 KB |
Output is correct |
5 |
Correct |
55 ms |
9804 KB |
Output is correct |
6 |
Correct |
74 ms |
9820 KB |
Output is correct |
7 |
Correct |
62 ms |
9820 KB |
Output is correct |
8 |
Correct |
56 ms |
9796 KB |
Output is correct |
9 |
Correct |
55 ms |
9804 KB |
Output is correct |
10 |
Correct |
59 ms |
9760 KB |
Output is correct |
11 |
Correct |
54 ms |
9748 KB |
Output is correct |
12 |
Correct |
46 ms |
9840 KB |
Output is correct |
13 |
Correct |
67 ms |
9896 KB |
Output is correct |
14 |
Correct |
592 ms |
86052 KB |
Output is correct |
15 |
Correct |
71 ms |
9752 KB |
Output is correct |
16 |
Correct |
483 ms |
86048 KB |
Output is correct |
17 |
Correct |
531 ms |
86132 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
1496 KB |
Output is correct |
3 |
Correct |
5 ms |
1496 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
1496 KB |
Output is correct |
17 |
Correct |
68 ms |
9756 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
362 ms |
74184 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
542 ms |
86088 KB |
Output is correct |
31 |
Correct |
548 ms |
86104 KB |
Output is correct |
32 |
Correct |
502 ms |
86068 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
547 ms |
86164 KB |
Output is correct |
35 |
Correct |
508 ms |
86172 KB |
Output is correct |
36 |
Correct |
545 ms |
86132 KB |
Output is correct |
37 |
Correct |
615 ms |
86120 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
48 ms |
9760 KB |
Output is correct |
40 |
Correct |
59 ms |
9788 KB |
Output is correct |
41 |
Correct |
49 ms |
9772 KB |
Output is correct |
42 |
Correct |
57 ms |
9892 KB |
Output is correct |
43 |
Correct |
48 ms |
9796 KB |
Output is correct |
44 |
Correct |
57 ms |
9788 KB |
Output is correct |
45 |
Correct |
48 ms |
9816 KB |
Output is correct |
46 |
Correct |
61 ms |
9756 KB |
Output is correct |
47 |
Correct |
48 ms |
9816 KB |
Output is correct |
48 |
Correct |
62 ms |
9880 KB |
Output is correct |
49 |
Correct |
54 ms |
9764 KB |
Output is correct |
50 |
Correct |
53 ms |
9756 KB |
Output is correct |
51 |
Correct |
563 ms |
86068 KB |
Output is correct |
52 |
Correct |
498 ms |
86048 KB |
Output is correct |
53 |
Correct |
615 ms |
86148 KB |
Output is correct |
54 |
Correct |
56 ms |
9768 KB |
Output is correct |
55 |
Correct |
48 ms |
9844 KB |
Output is correct |
56 |
Correct |
146 ms |
34732 KB |
Output is correct |
57 |
Correct |
0 ms |
328 KB |
Output is correct |
58 |
Correct |
53 ms |
9824 KB |
Output is correct |
59 |
Correct |
58 ms |
9764 KB |
Output is correct |
60 |
Correct |
657 ms |
86136 KB |
Output is correct |
61 |
Correct |
55 ms |
9804 KB |
Output is correct |
62 |
Correct |
74 ms |
9820 KB |
Output is correct |
63 |
Correct |
62 ms |
9820 KB |
Output is correct |
64 |
Correct |
56 ms |
9796 KB |
Output is correct |
65 |
Correct |
55 ms |
9804 KB |
Output is correct |
66 |
Correct |
59 ms |
9760 KB |
Output is correct |
67 |
Correct |
54 ms |
9748 KB |
Output is correct |
68 |
Correct |
46 ms |
9840 KB |
Output is correct |
69 |
Correct |
67 ms |
9896 KB |
Output is correct |
70 |
Correct |
592 ms |
86052 KB |
Output is correct |
71 |
Correct |
71 ms |
9752 KB |
Output is correct |
72 |
Correct |
483 ms |
86048 KB |
Output is correct |
73 |
Correct |
531 ms |
86132 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
328 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
5 ms |
1496 KB |
Output is correct |
80 |
Correct |
618 ms |
86032 KB |
Output is correct |
81 |
Correct |
639 ms |
86040 KB |
Output is correct |
82 |
Correct |
597 ms |
86144 KB |
Output is correct |
83 |
Correct |
618 ms |
86128 KB |
Output is correct |
84 |
Correct |
590 ms |
86080 KB |
Output is correct |
85 |
Correct |
588 ms |
86068 KB |
Output is correct |
86 |
Correct |
141 ms |
34748 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |