#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int INFF = 2e18;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, a, b;
cin >> n >> a >> b;
/*
0 0 0 0
1 1 1 1
2 2 2 2
4 3 1 0
5 4 2 1
6 5 0 2
8 6 2 0
9 7 0 1
10 8 1 2
12 9 0 0
0 0 0 0
1 1 1 1
2 2 0 2
3 3 1 3
5 4 1 0
6 5 0 1
7 6 1 2
8 7 0 3
10 8 0 0
4 2
0 0 0 0
1 1 1 1
3 2 3 0
4 3 0 1
6 4 2 0
7 5 3 1
9 6 1 2
10 7 2 1
12 8 0 0
4 1
0 0
2 1
4 2
3 2
0 0
1 1
3 2
4 3
6 3
3 1
0 0
2 1
4 2
6 3
setiap b, nambah b + 1
*/
int len = lcm(a, b + 1) / (b + 1) * b;
set<pair<int, int>> seg;
auto insert = [&](int l, int r) {
auto it = seg.lower_bound({l, 0});
if (!seg.empty() && it != seg.begin()) --it;
int mnl = l, mxr = r;
vector<pair<int, int>> del;
for (; it != seg.end(); it++) {
if (it->fi > r) break;
if (it->fi < l && it->se >= l) {
del.push_back(*it);
mnl = min(mnl, it->fi);
mxr = max(mxr, it->se);
}
if (it->fi >= l && it->fi <= r) {
del.push_back(*it);
mnl = min(mnl, it->fi);
mxr = max(mxr, it->se);
}
}
for (auto p : del) seg.erase(p);
seg.insert({mnl, mxr});
};
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if (a <= INFF / b && r - l + 1 >= len) {
cout << len << '\n';
return 0;
}
if (a <= INFF / b) {
l %= len;
r %= len;
}
if (l <= r) {
insert(l, r);
} else {
assert(a <= INFF / b);
insert(l, len - 1);
insert(0, r);
}
}
int ans = 0;
for (auto [l, r] : seg) ans += r - l + 1;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
47 ms |
10232 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
232 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
510 ms |
62800 KB |
Output is correct |
3 |
Correct |
491 ms |
62796 KB |
Output is correct |
4 |
Correct |
636 ms |
73724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
510 ms |
62800 KB |
Output is correct |
3 |
Correct |
491 ms |
62796 KB |
Output is correct |
4 |
Correct |
636 ms |
73724 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
536 ms |
73772 KB |
Output is correct |
7 |
Correct |
562 ms |
73644 KB |
Output is correct |
8 |
Correct |
524 ms |
73688 KB |
Output is correct |
9 |
Correct |
583 ms |
73700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
510 ms |
62800 KB |
Output is correct |
3 |
Correct |
491 ms |
62796 KB |
Output is correct |
4 |
Correct |
636 ms |
73724 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
42 ms |
10228 KB |
Output is correct |
7 |
Correct |
47 ms |
10248 KB |
Output is correct |
8 |
Correct |
48 ms |
10224 KB |
Output is correct |
9 |
Correct |
50 ms |
10188 KB |
Output is correct |
10 |
Correct |
44 ms |
10216 KB |
Output is correct |
11 |
Correct |
43 ms |
10152 KB |
Output is correct |
12 |
Correct |
48 ms |
10172 KB |
Output is correct |
13 |
Correct |
48 ms |
10268 KB |
Output is correct |
14 |
Correct |
59 ms |
10224 KB |
Output is correct |
15 |
Correct |
60 ms |
10216 KB |
Output is correct |
16 |
Correct |
50 ms |
10192 KB |
Output is correct |
17 |
Correct |
47 ms |
10180 KB |
Output is correct |
18 |
Correct |
517 ms |
73640 KB |
Output is correct |
19 |
Correct |
551 ms |
73608 KB |
Output is correct |
20 |
Correct |
546 ms |
73740 KB |
Output is correct |
21 |
Correct |
57 ms |
10188 KB |
Output is correct |
22 |
Correct |
63 ms |
10256 KB |
Output is correct |
23 |
Correct |
107 ms |
12824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
50 ms |
6448 KB |
Output is correct |
3 |
Correct |
59 ms |
6560 KB |
Output is correct |
4 |
Correct |
545 ms |
62840 KB |
Output is correct |
5 |
Correct |
63 ms |
6444 KB |
Output is correct |
6 |
Correct |
51 ms |
6552 KB |
Output is correct |
7 |
Correct |
54 ms |
6476 KB |
Output is correct |
8 |
Correct |
49 ms |
6512 KB |
Output is correct |
9 |
Correct |
47 ms |
6444 KB |
Output is correct |
10 |
Correct |
43 ms |
6544 KB |
Output is correct |
11 |
Correct |
47 ms |
6464 KB |
Output is correct |
12 |
Correct |
47 ms |
6452 KB |
Output is correct |
13 |
Correct |
50 ms |
6476 KB |
Output is correct |
14 |
Correct |
632 ms |
62848 KB |
Output is correct |
15 |
Correct |
40 ms |
5836 KB |
Output is correct |
16 |
Correct |
492 ms |
73788 KB |
Output is correct |
17 |
Correct |
530 ms |
73800 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
852 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
47 ms |
10232 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 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 |
340 KB |
Output is correct |
28 |
Correct |
232 ms |
316 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
510 ms |
62800 KB |
Output is correct |
31 |
Correct |
491 ms |
62796 KB |
Output is correct |
32 |
Correct |
636 ms |
73724 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
536 ms |
73772 KB |
Output is correct |
35 |
Correct |
562 ms |
73644 KB |
Output is correct |
36 |
Correct |
524 ms |
73688 KB |
Output is correct |
37 |
Correct |
583 ms |
73700 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
42 ms |
10228 KB |
Output is correct |
40 |
Correct |
47 ms |
10248 KB |
Output is correct |
41 |
Correct |
48 ms |
10224 KB |
Output is correct |
42 |
Correct |
50 ms |
10188 KB |
Output is correct |
43 |
Correct |
44 ms |
10216 KB |
Output is correct |
44 |
Correct |
43 ms |
10152 KB |
Output is correct |
45 |
Correct |
48 ms |
10172 KB |
Output is correct |
46 |
Correct |
48 ms |
10268 KB |
Output is correct |
47 |
Correct |
59 ms |
10224 KB |
Output is correct |
48 |
Correct |
60 ms |
10216 KB |
Output is correct |
49 |
Correct |
50 ms |
10192 KB |
Output is correct |
50 |
Correct |
47 ms |
10180 KB |
Output is correct |
51 |
Correct |
517 ms |
73640 KB |
Output is correct |
52 |
Correct |
551 ms |
73608 KB |
Output is correct |
53 |
Correct |
546 ms |
73740 KB |
Output is correct |
54 |
Correct |
57 ms |
10188 KB |
Output is correct |
55 |
Correct |
63 ms |
10256 KB |
Output is correct |
56 |
Correct |
107 ms |
12824 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
50 ms |
6448 KB |
Output is correct |
59 |
Correct |
59 ms |
6560 KB |
Output is correct |
60 |
Correct |
545 ms |
62840 KB |
Output is correct |
61 |
Correct |
63 ms |
6444 KB |
Output is correct |
62 |
Correct |
51 ms |
6552 KB |
Output is correct |
63 |
Correct |
54 ms |
6476 KB |
Output is correct |
64 |
Correct |
49 ms |
6512 KB |
Output is correct |
65 |
Correct |
47 ms |
6444 KB |
Output is correct |
66 |
Correct |
43 ms |
6544 KB |
Output is correct |
67 |
Correct |
47 ms |
6464 KB |
Output is correct |
68 |
Correct |
47 ms |
6452 KB |
Output is correct |
69 |
Correct |
50 ms |
6476 KB |
Output is correct |
70 |
Correct |
632 ms |
62848 KB |
Output is correct |
71 |
Correct |
40 ms |
5836 KB |
Output is correct |
72 |
Correct |
492 ms |
73788 KB |
Output is correct |
73 |
Correct |
530 ms |
73800 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
324 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
7 ms |
1212 KB |
Output is correct |
80 |
Correct |
556 ms |
78964 KB |
Output is correct |
81 |
Correct |
536 ms |
79644 KB |
Output is correct |
82 |
Correct |
619 ms |
79692 KB |
Output is correct |
83 |
Correct |
598 ms |
79672 KB |
Output is correct |
84 |
Correct |
615 ms |
78888 KB |
Output is correct |
85 |
Correct |
582 ms |
78904 KB |
Output is correct |
86 |
Correct |
102 ms |
12828 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |