#include <bits/stdc++.h>
using namespace std;
#define int long long
int co[2000010], d[2000010];
void update(int l, int r) {
// cout << "updating: " << l << " " << r << "\n";
d[l]++;
d[r]--;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, A, B, ans = 0;
cin >> n >> A >> B;
map<int, bool> mp;
int g = gcd(A, B + 1);
int mul = (A + g - 1) / g;
if (3E18 / mul < B) mul = 3E18;
else mul = (A / g) * B;
// cout << mul << endl;
vector<pair<int, int>> ends;
vector<int> sorted;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 >= mul) {
cout << mul << endl;
return 0;
}
l %= mul, r %= mul;
// cout << l << ' ' << r << '\n';
ends.emplace_back(l, r + 1);
sorted.push_back(l);
sorted.push_back(r + 1);
}
sorted.push_back(0);
sorted.push_back(mul);
sort(sorted.begin(), sorted.end());
sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
for (int i = 0; i < sorted.size(); i++) co[i+1] = sorted[i];
int mx = 0;
for (int i = 0; i < ends.size(); i++) {
ends[i].first = lower_bound(sorted.begin(), sorted.end(), ends[i].first) - sorted.begin() + 1;
ends[i].second = lower_bound(sorted.begin(), sorted.end(), ends[i].second) - sorted.begin() + 1;
if (ends[i].first > ends[i].second) {
update(ends[i].first, sorted.size());
update(1, ends[i].second);
} else update(ends[i].first, ends[i].second);
}
for (int i = 1; i <= sorted.size(); i++) d[i] += d[i-1];
for (int i = 1; i < sorted.size(); i++) {
// cout << co[i] << ' ' << co[i+1] << ' ' << d[i] << '\n';
if (d[i] > 0) ans += co[i+1] - co[i];
}
cout << ans << '\n';
return 0;
}
Compilation message
strange_device.cpp: In function 'int32_t main()':
strange_device.cpp:43:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < sorted.size(); i++) co[i+1] = sorted[i];
| ~~^~~~~~~~~~~~~~~
strange_device.cpp:45:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < ends.size(); i++) {
| ~~^~~~~~~~~~~~~
strange_device.cpp:53:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 1; i <= sorted.size(); i++) d[i] += d[i-1];
| ~~^~~~~~~~~~~~~~~~
strange_device.cpp:54:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 1; i < sorted.size(); i++) {
| ~~^~~~~~~~~~~~~~~
strange_device.cpp:44:9: warning: unused variable 'mx' [-Wunused-variable]
44 | int mx = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1364 KB |
Output is correct |
3 |
Correct |
7 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1360 KB |
Output is correct |
17 |
Correct |
66 ms |
8376 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
5 |
Correct |
325 ms |
57076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
555 ms |
47816 KB |
Output is correct |
3 |
Correct |
597 ms |
84776 KB |
Output is correct |
4 |
Correct |
555 ms |
84740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
555 ms |
47816 KB |
Output is correct |
3 |
Correct |
597 ms |
84776 KB |
Output is correct |
4 |
Correct |
555 ms |
84740 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
545 ms |
84620 KB |
Output is correct |
7 |
Correct |
559 ms |
84764 KB |
Output is correct |
8 |
Correct |
568 ms |
84756 KB |
Output is correct |
9 |
Correct |
630 ms |
84728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
555 ms |
47816 KB |
Output is correct |
3 |
Correct |
597 ms |
84776 KB |
Output is correct |
4 |
Correct |
555 ms |
84740 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
55 ms |
8860 KB |
Output is correct |
7 |
Correct |
60 ms |
8932 KB |
Output is correct |
8 |
Correct |
50 ms |
8936 KB |
Output is correct |
9 |
Correct |
56 ms |
8896 KB |
Output is correct |
10 |
Correct |
56 ms |
8924 KB |
Output is correct |
11 |
Correct |
57 ms |
8896 KB |
Output is correct |
12 |
Correct |
56 ms |
8872 KB |
Output is correct |
13 |
Correct |
59 ms |
8956 KB |
Output is correct |
14 |
Correct |
58 ms |
8932 KB |
Output is correct |
15 |
Correct |
64 ms |
8860 KB |
Output is correct |
16 |
Correct |
62 ms |
8912 KB |
Output is correct |
17 |
Correct |
52 ms |
8856 KB |
Output is correct |
18 |
Correct |
565 ms |
84708 KB |
Output is correct |
19 |
Correct |
568 ms |
84672 KB |
Output is correct |
20 |
Correct |
643 ms |
84780 KB |
Output is correct |
21 |
Correct |
67 ms |
8880 KB |
Output is correct |
22 |
Correct |
54 ms |
8968 KB |
Output is correct |
23 |
Correct |
139 ms |
23820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
66 ms |
7084 KB |
Output is correct |
3 |
Correct |
60 ms |
8904 KB |
Output is correct |
4 |
Correct |
655 ms |
84848 KB |
Output is correct |
5 |
Correct |
62 ms |
8936 KB |
Output is correct |
6 |
Correct |
61 ms |
8928 KB |
Output is correct |
7 |
Correct |
64 ms |
9128 KB |
Output is correct |
8 |
Correct |
68 ms |
8856 KB |
Output is correct |
9 |
Correct |
61 ms |
8948 KB |
Output is correct |
10 |
Correct |
69 ms |
8964 KB |
Output is correct |
11 |
Correct |
60 ms |
8880 KB |
Output is correct |
12 |
Correct |
53 ms |
9008 KB |
Output is correct |
13 |
Correct |
62 ms |
8948 KB |
Output is correct |
14 |
Correct |
637 ms |
84712 KB |
Output is correct |
15 |
Correct |
65 ms |
8868 KB |
Output is correct |
16 |
Correct |
561 ms |
84628 KB |
Output is correct |
17 |
Correct |
563 ms |
84684 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1364 KB |
Output is correct |
3 |
Correct |
7 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1360 KB |
Output is correct |
17 |
Correct |
66 ms |
8376 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
328 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 |
340 KB |
Output is correct |
28 |
Correct |
325 ms |
57076 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
555 ms |
47816 KB |
Output is correct |
31 |
Correct |
597 ms |
84776 KB |
Output is correct |
32 |
Correct |
555 ms |
84740 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
545 ms |
84620 KB |
Output is correct |
35 |
Correct |
559 ms |
84764 KB |
Output is correct |
36 |
Correct |
568 ms |
84756 KB |
Output is correct |
37 |
Correct |
630 ms |
84728 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
55 ms |
8860 KB |
Output is correct |
40 |
Correct |
60 ms |
8932 KB |
Output is correct |
41 |
Correct |
50 ms |
8936 KB |
Output is correct |
42 |
Correct |
56 ms |
8896 KB |
Output is correct |
43 |
Correct |
56 ms |
8924 KB |
Output is correct |
44 |
Correct |
57 ms |
8896 KB |
Output is correct |
45 |
Correct |
56 ms |
8872 KB |
Output is correct |
46 |
Correct |
59 ms |
8956 KB |
Output is correct |
47 |
Correct |
58 ms |
8932 KB |
Output is correct |
48 |
Correct |
64 ms |
8860 KB |
Output is correct |
49 |
Correct |
62 ms |
8912 KB |
Output is correct |
50 |
Correct |
52 ms |
8856 KB |
Output is correct |
51 |
Correct |
565 ms |
84708 KB |
Output is correct |
52 |
Correct |
568 ms |
84672 KB |
Output is correct |
53 |
Correct |
643 ms |
84780 KB |
Output is correct |
54 |
Correct |
67 ms |
8880 KB |
Output is correct |
55 |
Correct |
54 ms |
8968 KB |
Output is correct |
56 |
Correct |
139 ms |
23820 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
66 ms |
7084 KB |
Output is correct |
59 |
Correct |
60 ms |
8904 KB |
Output is correct |
60 |
Correct |
655 ms |
84848 KB |
Output is correct |
61 |
Correct |
62 ms |
8936 KB |
Output is correct |
62 |
Correct |
61 ms |
8928 KB |
Output is correct |
63 |
Correct |
64 ms |
9128 KB |
Output is correct |
64 |
Correct |
68 ms |
8856 KB |
Output is correct |
65 |
Correct |
61 ms |
8948 KB |
Output is correct |
66 |
Correct |
69 ms |
8964 KB |
Output is correct |
67 |
Correct |
60 ms |
8880 KB |
Output is correct |
68 |
Correct |
53 ms |
9008 KB |
Output is correct |
69 |
Correct |
62 ms |
8948 KB |
Output is correct |
70 |
Correct |
637 ms |
84712 KB |
Output is correct |
71 |
Correct |
65 ms |
8868 KB |
Output is correct |
72 |
Correct |
561 ms |
84628 KB |
Output is correct |
73 |
Correct |
563 ms |
84684 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
1396 KB |
Output is correct |
80 |
Correct |
650 ms |
84748 KB |
Output is correct |
81 |
Correct |
651 ms |
84788 KB |
Output is correct |
82 |
Correct |
639 ms |
84708 KB |
Output is correct |
83 |
Correct |
617 ms |
84700 KB |
Output is correct |
84 |
Correct |
627 ms |
84704 KB |
Output is correct |
85 |
Correct |
627 ms |
84816 KB |
Output is correct |
86 |
Correct |
131 ms |
23736 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |