#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
ll a, b;
cin >> q >> a >> b;
ll p = (a / __gcd(a, b + 1)) * b;
if (p <= 0) p = 2e18;
set<pair<ll, ll>> s;
function<void(ll, ll)> Add = [&] (ll l, ll r) {
auto it = s.lower_bound({l, 0});
ll mn = 2e18; ll mx = -1;
while (it != s.end() && (*it).second <= r) {
smin(mn, (*it).second);
smax(mx, (*it).first);
it = s.erase(it);
}
smin(mn, l);
smax(mx, r);
s.insert({mx, mn});
};
while (q--) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= p) {Add(0, p - 1); continue;}
l %= p;
r %= p;
if (l > r) {Add(l, p - 1); Add(0, r);}
else Add(l, r);
}
ll ans = 0;
for (auto u : s) {
ans += u.first - u.second + 1;
}
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1264 KB |
Output is correct |
3 |
Correct |
6 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
8 ms |
1192 KB |
Output is correct |
17 |
Correct |
62 ms |
6892 KB |
Output is correct |
18 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
277 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
618 ms |
62888 KB |
Output is correct |
3 |
Correct |
650 ms |
62860 KB |
Output is correct |
4 |
Correct |
664 ms |
62868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
618 ms |
62888 KB |
Output is correct |
3 |
Correct |
650 ms |
62860 KB |
Output is correct |
4 |
Correct |
664 ms |
62868 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
637 ms |
62960 KB |
Output is correct |
7 |
Correct |
640 ms |
63156 KB |
Output is correct |
8 |
Correct |
641 ms |
63124 KB |
Output is correct |
9 |
Correct |
714 ms |
62944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
618 ms |
62888 KB |
Output is correct |
3 |
Correct |
650 ms |
62860 KB |
Output is correct |
4 |
Correct |
664 ms |
62868 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
52 ms |
6880 KB |
Output is correct |
7 |
Correct |
53 ms |
6856 KB |
Output is correct |
8 |
Correct |
56 ms |
6964 KB |
Output is correct |
9 |
Correct |
63 ms |
6920 KB |
Output is correct |
10 |
Correct |
52 ms |
6912 KB |
Output is correct |
11 |
Correct |
53 ms |
6808 KB |
Output is correct |
12 |
Correct |
55 ms |
6856 KB |
Output is correct |
13 |
Correct |
62 ms |
6940 KB |
Output is correct |
14 |
Correct |
52 ms |
6860 KB |
Output is correct |
15 |
Correct |
70 ms |
6876 KB |
Output is correct |
16 |
Correct |
66 ms |
6844 KB |
Output is correct |
17 |
Correct |
60 ms |
6824 KB |
Output is correct |
18 |
Correct |
615 ms |
62988 KB |
Output is correct |
19 |
Correct |
669 ms |
63528 KB |
Output is correct |
20 |
Correct |
690 ms |
62960 KB |
Output is correct |
21 |
Correct |
57 ms |
6888 KB |
Output is correct |
22 |
Correct |
59 ms |
6888 KB |
Output is correct |
23 |
Correct |
117 ms |
684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
69 ms |
7628 KB |
Output is correct |
3 |
Correct |
70 ms |
7620 KB |
Output is correct |
4 |
Correct |
683 ms |
63052 KB |
Output is correct |
5 |
Correct |
66 ms |
7512 KB |
Output is correct |
6 |
Correct |
68 ms |
7628 KB |
Output is correct |
7 |
Correct |
80 ms |
7616 KB |
Output is correct |
8 |
Correct |
68 ms |
7640 KB |
Output is correct |
9 |
Correct |
64 ms |
7628 KB |
Output is correct |
10 |
Correct |
57 ms |
6864 KB |
Output is correct |
11 |
Correct |
60 ms |
6836 KB |
Output is correct |
12 |
Correct |
59 ms |
7620 KB |
Output is correct |
13 |
Correct |
72 ms |
7884 KB |
Output is correct |
14 |
Correct |
727 ms |
62908 KB |
Output is correct |
15 |
Correct |
57 ms |
7116 KB |
Output is correct |
16 |
Correct |
624 ms |
62956 KB |
Output is correct |
17 |
Correct |
608 ms |
63012 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1264 KB |
Output is correct |
3 |
Correct |
6 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
8 ms |
1192 KB |
Output is correct |
17 |
Correct |
62 ms |
6892 KB |
Output is correct |
18 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
448 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
372 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
277 ms |
316 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
618 ms |
62888 KB |
Output is correct |
31 |
Correct |
650 ms |
62860 KB |
Output is correct |
32 |
Correct |
664 ms |
62868 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
637 ms |
62960 KB |
Output is correct |
35 |
Correct |
640 ms |
63156 KB |
Output is correct |
36 |
Correct |
641 ms |
63124 KB |
Output is correct |
37 |
Correct |
714 ms |
62944 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
52 ms |
6880 KB |
Output is correct |
40 |
Correct |
53 ms |
6856 KB |
Output is correct |
41 |
Correct |
56 ms |
6964 KB |
Output is correct |
42 |
Correct |
63 ms |
6920 KB |
Output is correct |
43 |
Correct |
52 ms |
6912 KB |
Output is correct |
44 |
Correct |
53 ms |
6808 KB |
Output is correct |
45 |
Correct |
55 ms |
6856 KB |
Output is correct |
46 |
Correct |
62 ms |
6940 KB |
Output is correct |
47 |
Correct |
52 ms |
6860 KB |
Output is correct |
48 |
Correct |
70 ms |
6876 KB |
Output is correct |
49 |
Correct |
66 ms |
6844 KB |
Output is correct |
50 |
Correct |
60 ms |
6824 KB |
Output is correct |
51 |
Correct |
615 ms |
62988 KB |
Output is correct |
52 |
Correct |
669 ms |
63528 KB |
Output is correct |
53 |
Correct |
690 ms |
62960 KB |
Output is correct |
54 |
Correct |
57 ms |
6888 KB |
Output is correct |
55 |
Correct |
59 ms |
6888 KB |
Output is correct |
56 |
Correct |
117 ms |
684 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
69 ms |
7628 KB |
Output is correct |
59 |
Correct |
70 ms |
7620 KB |
Output is correct |
60 |
Correct |
683 ms |
63052 KB |
Output is correct |
61 |
Correct |
66 ms |
7512 KB |
Output is correct |
62 |
Correct |
68 ms |
7628 KB |
Output is correct |
63 |
Correct |
80 ms |
7616 KB |
Output is correct |
64 |
Correct |
68 ms |
7640 KB |
Output is correct |
65 |
Correct |
64 ms |
7628 KB |
Output is correct |
66 |
Correct |
57 ms |
6864 KB |
Output is correct |
67 |
Correct |
60 ms |
6836 KB |
Output is correct |
68 |
Correct |
59 ms |
7620 KB |
Output is correct |
69 |
Correct |
72 ms |
7884 KB |
Output is correct |
70 |
Correct |
727 ms |
62908 KB |
Output is correct |
71 |
Correct |
57 ms |
7116 KB |
Output is correct |
72 |
Correct |
624 ms |
62956 KB |
Output is correct |
73 |
Correct |
608 ms |
63012 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
320 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
1236 KB |
Output is correct |
80 |
Correct |
764 ms |
100072 KB |
Output is correct |
81 |
Correct |
717 ms |
100028 KB |
Output is correct |
82 |
Correct |
836 ms |
100000 KB |
Output is correct |
83 |
Correct |
751 ms |
99968 KB |
Output is correct |
84 |
Correct |
765 ms |
99896 KB |
Output is correct |
85 |
Correct |
705 ms |
99924 KB |
Output is correct |
86 |
Correct |
122 ms |
12840 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |