#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long a, b;
cin >> n >> a >> b;
a /= gcd(a, b + 1);
long long period = 1e18 + 1;
if (a <= LLONG_MAX / b) {
period = a * b;
}
vector<long long> l(n), r(n);
vector<pair<long long,long long>> v;
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
if (r[i] - l[i] + 1 >= period) {
cout << period << '\n';
return 0;
}
l[i] %= period;
r[i] %= period;
if (l[i] <= r[i]) {
v.emplace_back(l[i], r[i]);
} else {
v.emplace_back(l[i], period - 1);
v.emplace_back(0, r[i]);
}
}
sort(v.begin(), v.end());
long long cl = 0;
long long cr = -1;
long long ans = 0;
for (int i = 0; i < (int) v.size(); ++i) {
auto [x, y] = v[i];
if (x > cr) {
ans += cr - cl + 1;
cl = x; cr = y;
} else {
cr = max(cr, y);
}
}
ans += cr - cl + 1;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1224 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
8 ms |
1212 KB |
Output is correct |
17 |
Correct |
68 ms |
7256 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 |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
332 KB |
Output is correct |
5 |
Correct |
314 ms |
56932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
431 ms |
40480 KB |
Output is correct |
3 |
Correct |
478 ms |
68804 KB |
Output is correct |
4 |
Correct |
477 ms |
68820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
431 ms |
40480 KB |
Output is correct |
3 |
Correct |
478 ms |
68804 KB |
Output is correct |
4 |
Correct |
477 ms |
68820 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
501 ms |
68824 KB |
Output is correct |
7 |
Correct |
456 ms |
68840 KB |
Output is correct |
8 |
Correct |
460 ms |
68844 KB |
Output is correct |
9 |
Correct |
489 ms |
68756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
431 ms |
40480 KB |
Output is correct |
3 |
Correct |
478 ms |
68804 KB |
Output is correct |
4 |
Correct |
477 ms |
68820 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
55 ms |
7292 KB |
Output is correct |
7 |
Correct |
56 ms |
7256 KB |
Output is correct |
8 |
Correct |
72 ms |
7252 KB |
Output is correct |
9 |
Correct |
43 ms |
7236 KB |
Output is correct |
10 |
Correct |
41 ms |
7208 KB |
Output is correct |
11 |
Correct |
42 ms |
7232 KB |
Output is correct |
12 |
Correct |
42 ms |
7256 KB |
Output is correct |
13 |
Correct |
43 ms |
7216 KB |
Output is correct |
14 |
Correct |
39 ms |
7236 KB |
Output is correct |
15 |
Correct |
52 ms |
7228 KB |
Output is correct |
16 |
Correct |
44 ms |
7232 KB |
Output is correct |
17 |
Correct |
43 ms |
7236 KB |
Output is correct |
18 |
Correct |
446 ms |
68784 KB |
Output is correct |
19 |
Correct |
421 ms |
68764 KB |
Output is correct |
20 |
Correct |
490 ms |
68848 KB |
Output is correct |
21 |
Correct |
60 ms |
7260 KB |
Output is correct |
22 |
Correct |
55 ms |
7276 KB |
Output is correct |
23 |
Correct |
142 ms |
23720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
45 ms |
7236 KB |
Output is correct |
3 |
Correct |
45 ms |
7248 KB |
Output is correct |
4 |
Correct |
496 ms |
68864 KB |
Output is correct |
5 |
Correct |
45 ms |
7260 KB |
Output is correct |
6 |
Correct |
49 ms |
7204 KB |
Output is correct |
7 |
Correct |
49 ms |
7224 KB |
Output is correct |
8 |
Correct |
44 ms |
7204 KB |
Output is correct |
9 |
Correct |
44 ms |
7236 KB |
Output is correct |
10 |
Correct |
43 ms |
7164 KB |
Output is correct |
11 |
Correct |
45 ms |
7196 KB |
Output is correct |
12 |
Correct |
42 ms |
7180 KB |
Output is correct |
13 |
Correct |
46 ms |
7260 KB |
Output is correct |
14 |
Correct |
457 ms |
68860 KB |
Output is correct |
15 |
Correct |
51 ms |
7240 KB |
Output is correct |
16 |
Correct |
547 ms |
68780 KB |
Output is correct |
17 |
Correct |
492 ms |
68880 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 |
5 ms |
1224 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
8 ms |
1212 KB |
Output is correct |
17 |
Correct |
68 ms |
7256 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 |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 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 |
332 KB |
Output is correct |
28 |
Correct |
314 ms |
56932 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
431 ms |
40480 KB |
Output is correct |
31 |
Correct |
478 ms |
68804 KB |
Output is correct |
32 |
Correct |
477 ms |
68820 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
501 ms |
68824 KB |
Output is correct |
35 |
Correct |
456 ms |
68840 KB |
Output is correct |
36 |
Correct |
460 ms |
68844 KB |
Output is correct |
37 |
Correct |
489 ms |
68756 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
55 ms |
7292 KB |
Output is correct |
40 |
Correct |
56 ms |
7256 KB |
Output is correct |
41 |
Correct |
72 ms |
7252 KB |
Output is correct |
42 |
Correct |
43 ms |
7236 KB |
Output is correct |
43 |
Correct |
41 ms |
7208 KB |
Output is correct |
44 |
Correct |
42 ms |
7232 KB |
Output is correct |
45 |
Correct |
42 ms |
7256 KB |
Output is correct |
46 |
Correct |
43 ms |
7216 KB |
Output is correct |
47 |
Correct |
39 ms |
7236 KB |
Output is correct |
48 |
Correct |
52 ms |
7228 KB |
Output is correct |
49 |
Correct |
44 ms |
7232 KB |
Output is correct |
50 |
Correct |
43 ms |
7236 KB |
Output is correct |
51 |
Correct |
446 ms |
68784 KB |
Output is correct |
52 |
Correct |
421 ms |
68764 KB |
Output is correct |
53 |
Correct |
490 ms |
68848 KB |
Output is correct |
54 |
Correct |
60 ms |
7260 KB |
Output is correct |
55 |
Correct |
55 ms |
7276 KB |
Output is correct |
56 |
Correct |
142 ms |
23720 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
45 ms |
7236 KB |
Output is correct |
59 |
Correct |
45 ms |
7248 KB |
Output is correct |
60 |
Correct |
496 ms |
68864 KB |
Output is correct |
61 |
Correct |
45 ms |
7260 KB |
Output is correct |
62 |
Correct |
49 ms |
7204 KB |
Output is correct |
63 |
Correct |
49 ms |
7224 KB |
Output is correct |
64 |
Correct |
44 ms |
7204 KB |
Output is correct |
65 |
Correct |
44 ms |
7236 KB |
Output is correct |
66 |
Correct |
43 ms |
7164 KB |
Output is correct |
67 |
Correct |
45 ms |
7196 KB |
Output is correct |
68 |
Correct |
42 ms |
7180 KB |
Output is correct |
69 |
Correct |
46 ms |
7260 KB |
Output is correct |
70 |
Correct |
457 ms |
68860 KB |
Output is correct |
71 |
Correct |
51 ms |
7240 KB |
Output is correct |
72 |
Correct |
547 ms |
68780 KB |
Output is correct |
73 |
Correct |
492 ms |
68880 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
316 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
1136 KB |
Output is correct |
80 |
Correct |
503 ms |
68760 KB |
Output is correct |
81 |
Correct |
520 ms |
68808 KB |
Output is correct |
82 |
Correct |
528 ms |
68760 KB |
Output is correct |
83 |
Correct |
482 ms |
68824 KB |
Output is correct |
84 |
Correct |
476 ms |
68852 KB |
Output is correct |
85 |
Correct |
506 ms |
68748 KB |
Output is correct |
86 |
Correct |
135 ms |
23768 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |