#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;
typedef long long llong;
typedef pair<llong, llong> pll;
const llong inf = 1e18 + 1;
llong mul(llong x, llong y) {
llong r = 0;
while (y) {
if (x > inf) return inf;
if (y & 1) r += x;
if (r > inf) return inf;
x += x;
y >>= 1;
}
return r;
}
llong gcd(llong x, llong y) {
for (; y; swap(x, y)) x %= y;
return x;
}
set<pll> sp;
void add(llong s, llong e, llong M) {
if (M <= e - s + 1) {
printf("%lld\n", M);
exit(0);
}
s %= M;
e %= M;
if (e < s) {
add(s, M - 1, M);
add(0, e, M);
return;
}
while (1) {
auto it = sp.lower_bound(pll(e + 2, -1));
if (it == sp.begin()) break;
--it;
if (it->second + 1 < s) break;
s = min(s, it->first);
e = max(e, it->second);
sp.erase(it);
}
sp.emplace(s, e);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
llong A, B;
cin >> n >> A >> B;
llong G = gcd(A, B + 1);
llong M = mul(A / G, B);
for (int i = 0; i < n; ++i) {
llong L, R;
cin >> L >> R;
add(L, R, M);
}
llong ans = 0;
for (pll i : sp) ans += i.second - i.first + 1;
printf("%lld\n", ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
760 KB |
Output is correct |
3 |
Correct |
8 ms |
760 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
276 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
760 KB |
Output is correct |
17 |
Correct |
63 ms |
808 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
455 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
602 ms |
1204 KB |
Output is correct |
3 |
Correct |
593 ms |
824 KB |
Output is correct |
4 |
Correct |
591 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
602 ms |
1204 KB |
Output is correct |
3 |
Correct |
593 ms |
824 KB |
Output is correct |
4 |
Correct |
591 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
600 ms |
2664 KB |
Output is correct |
7 |
Correct |
593 ms |
2564 KB |
Output is correct |
8 |
Correct |
600 ms |
2556 KB |
Output is correct |
9 |
Correct |
607 ms |
2556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
602 ms |
1204 KB |
Output is correct |
3 |
Correct |
593 ms |
824 KB |
Output is correct |
4 |
Correct |
591 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
292 KB |
Output is correct |
6 |
Correct |
60 ms |
900 KB |
Output is correct |
7 |
Correct |
63 ms |
4096 KB |
Output is correct |
8 |
Correct |
63 ms |
4096 KB |
Output is correct |
9 |
Correct |
64 ms |
4080 KB |
Output is correct |
10 |
Correct |
63 ms |
4060 KB |
Output is correct |
11 |
Correct |
63 ms |
4060 KB |
Output is correct |
12 |
Correct |
62 ms |
4092 KB |
Output is correct |
13 |
Correct |
63 ms |
4076 KB |
Output is correct |
14 |
Correct |
63 ms |
4216 KB |
Output is correct |
15 |
Correct |
64 ms |
4104 KB |
Output is correct |
16 |
Correct |
64 ms |
4092 KB |
Output is correct |
17 |
Correct |
62 ms |
4088 KB |
Output is correct |
18 |
Correct |
594 ms |
2600 KB |
Output is correct |
19 |
Correct |
600 ms |
2680 KB |
Output is correct |
20 |
Correct |
599 ms |
2604 KB |
Output is correct |
21 |
Correct |
70 ms |
4088 KB |
Output is correct |
22 |
Correct |
63 ms |
4088 KB |
Output is correct |
23 |
Correct |
221 ms |
12920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
64 ms |
2936 KB |
Output is correct |
3 |
Correct |
63 ms |
3964 KB |
Output is correct |
4 |
Correct |
603 ms |
2520 KB |
Output is correct |
5 |
Correct |
66 ms |
3712 KB |
Output is correct |
6 |
Correct |
64 ms |
3744 KB |
Output is correct |
7 |
Correct |
65 ms |
3968 KB |
Output is correct |
8 |
Correct |
66 ms |
4092 KB |
Output is correct |
9 |
Correct |
63 ms |
4088 KB |
Output is correct |
10 |
Correct |
63 ms |
4092 KB |
Output is correct |
11 |
Correct |
63 ms |
4216 KB |
Output is correct |
12 |
Correct |
63 ms |
4088 KB |
Output is correct |
13 |
Correct |
65 ms |
4092 KB |
Output is correct |
14 |
Correct |
618 ms |
2652 KB |
Output is correct |
15 |
Correct |
62 ms |
3576 KB |
Output is correct |
16 |
Correct |
595 ms |
2680 KB |
Output is correct |
17 |
Correct |
600 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
760 KB |
Output is correct |
3 |
Correct |
8 ms |
760 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
276 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
760 KB |
Output is correct |
17 |
Correct |
63 ms |
808 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
455 ms |
3420 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
602 ms |
1204 KB |
Output is correct |
31 |
Correct |
593 ms |
824 KB |
Output is correct |
32 |
Correct |
591 ms |
760 KB |
Output is correct |
33 |
Correct |
2 ms |
380 KB |
Output is correct |
34 |
Correct |
600 ms |
2664 KB |
Output is correct |
35 |
Correct |
593 ms |
2564 KB |
Output is correct |
36 |
Correct |
600 ms |
2556 KB |
Output is correct |
37 |
Correct |
607 ms |
2556 KB |
Output is correct |
38 |
Correct |
2 ms |
292 KB |
Output is correct |
39 |
Correct |
60 ms |
900 KB |
Output is correct |
40 |
Correct |
63 ms |
4096 KB |
Output is correct |
41 |
Correct |
63 ms |
4096 KB |
Output is correct |
42 |
Correct |
64 ms |
4080 KB |
Output is correct |
43 |
Correct |
63 ms |
4060 KB |
Output is correct |
44 |
Correct |
63 ms |
4060 KB |
Output is correct |
45 |
Correct |
62 ms |
4092 KB |
Output is correct |
46 |
Correct |
63 ms |
4076 KB |
Output is correct |
47 |
Correct |
63 ms |
4216 KB |
Output is correct |
48 |
Correct |
64 ms |
4104 KB |
Output is correct |
49 |
Correct |
64 ms |
4092 KB |
Output is correct |
50 |
Correct |
62 ms |
4088 KB |
Output is correct |
51 |
Correct |
594 ms |
2600 KB |
Output is correct |
52 |
Correct |
600 ms |
2680 KB |
Output is correct |
53 |
Correct |
599 ms |
2604 KB |
Output is correct |
54 |
Correct |
70 ms |
4088 KB |
Output is correct |
55 |
Correct |
63 ms |
4088 KB |
Output is correct |
56 |
Correct |
221 ms |
12920 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
64 ms |
2936 KB |
Output is correct |
59 |
Correct |
63 ms |
3964 KB |
Output is correct |
60 |
Correct |
603 ms |
2520 KB |
Output is correct |
61 |
Correct |
66 ms |
3712 KB |
Output is correct |
62 |
Correct |
64 ms |
3744 KB |
Output is correct |
63 |
Correct |
65 ms |
3968 KB |
Output is correct |
64 |
Correct |
66 ms |
4092 KB |
Output is correct |
65 |
Correct |
63 ms |
4088 KB |
Output is correct |
66 |
Correct |
63 ms |
4092 KB |
Output is correct |
67 |
Correct |
63 ms |
4216 KB |
Output is correct |
68 |
Correct |
63 ms |
4088 KB |
Output is correct |
69 |
Correct |
65 ms |
4092 KB |
Output is correct |
70 |
Correct |
618 ms |
2652 KB |
Output is correct |
71 |
Correct |
62 ms |
3576 KB |
Output is correct |
72 |
Correct |
595 ms |
2680 KB |
Output is correct |
73 |
Correct |
600 ms |
2680 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
3 ms |
376 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
380 KB |
Output is correct |
79 |
Correct |
9 ms |
760 KB |
Output is correct |
80 |
Correct |
602 ms |
2764 KB |
Output is correct |
81 |
Correct |
610 ms |
2716 KB |
Output is correct |
82 |
Correct |
603 ms |
2704 KB |
Output is correct |
83 |
Correct |
609 ms |
2708 KB |
Output is correct |
84 |
Correct |
600 ms |
2808 KB |
Output is correct |
85 |
Correct |
606 ms |
2724 KB |
Output is correct |
86 |
Correct |
198 ms |
6640 KB |
Output is correct |
87 |
Correct |
2 ms |
384 KB |
Output is correct |