/**
* @title Template
*/
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <map>
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) { lhs = rhs; return true; }
return false;
}
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) { lhs = rhs; return true; }
return false;
}
struct range {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { ++i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
constexpr iterator begin() const noexcept { return l; }
constexpr iterator end() const noexcept { return r; }
};
struct revrange {
using itr = int64_t;
struct iterator {
itr i;
constexpr iterator(itr i_) noexcept : i(i_) { }
constexpr void operator ++ () noexcept { --i; }
constexpr itr operator * () const noexcept { return i; }
constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
};
const iterator l, r;
constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
constexpr iterator begin() const noexcept { return r; }
constexpr iterator end() const noexcept { return l; }
};
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
std::vector<std::pair<i64, i64>> resolve_overlapping(std::vector<std::pair<i64, i64>> vec) {
std::sort(vec.begin(), vec.end());
i64 L = -1, R = -1;
std::vector<std::pair<i64, i64>> res;
for (auto [l, r]: vec) {
if (R < l) {
if (L != -1 && R != -1) {
res.emplace_back(L, R);
}
L = l;
R = r;
}
else {
chmax(R, r);
}
}
if (L != -1 && R != -1) {
res.emplace_back(L, R);
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t N;
i64 A, B;
std::cin >> N >> A >> B;
i64 C = (B % A) + 1;
i64 D = A / std::gcd(A, C);
std::vector<std::pair<i64, i64>> intervals;
intervals.reserve(N);
__int128_t loop = __int128_t(B) * __int128_t(D);
while (N--) {
i64 l, r;
std::cin >> l >> r;
i64 sl = l / loop;
i64 sr = r / loop;
switch (sr - sl) {
case 0:
intervals.emplace_back(l % loop, r % loop);
break;
case 1:
intervals.emplace_back(l % loop, loop - 1);
intervals.emplace_back(0, r % loop);
break;
default:
std::cout << (i64) loop << '\n';
return 0;
break;
}
}
intervals = resolve_overlapping(intervals);
std::vector<std::pair<i64, i64>> covered;
std::map<i64, std::vector<std::pair<i64, i64>>> fragments;
for (auto [l, r]: intervals) {
i64 sl = l / B;
i64 sr = r / B;
switch (sr - sl) {
case 0:
fragments[sl].emplace_back(l % B, r % B);
break;
case 1:
fragments[sl].emplace_back(l % B, B - 1);
fragments[sr].emplace_back(0, r % B);
break;
default:
covered.emplace_back(sl + 1, sr - 1);
fragments[sl].emplace_back(l % B, B - 1);
fragments[sr].emplace_back(0, r % B);
break;
}
}
covered = resolve_overlapping(covered);
i64 ans = 0, cover = 0;
for (auto [k, vec]: fragments) {
auto itr = std::upper_bound(covered.begin(), covered.end(), std::make_pair(k, i64(-1)));
if (itr != covered.begin()) {
itr = std::prev(itr);
if (itr -> second >= k) {
--cover;
continue;
}
}
vec = resolve_overlapping(vec);
for (auto [l, r]: vec) {
ans += r - l + 1;
}
}
for (auto [l, r]: covered) {
cover += r - l + 1;
}
std::cout << ans + B * cover << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
1152 KB |
Output is correct |
3 |
Correct |
7 ms |
1376 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
7 ms |
1376 KB |
Output is correct |
17 |
Correct |
66 ms |
7536 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
391 ms |
32632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1112 ms |
125948 KB |
Output is correct |
3 |
Correct |
1910 ms |
298804 KB |
Output is correct |
4 |
Correct |
1871 ms |
298784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1112 ms |
125948 KB |
Output is correct |
3 |
Correct |
1910 ms |
298804 KB |
Output is correct |
4 |
Correct |
1871 ms |
298784 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1503 ms |
204824 KB |
Output is correct |
7 |
Correct |
853 ms |
68460 KB |
Output is correct |
8 |
Correct |
1478 ms |
204828 KB |
Output is correct |
9 |
Correct |
1588 ms |
204908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1112 ms |
125948 KB |
Output is correct |
3 |
Correct |
1910 ms |
298804 KB |
Output is correct |
4 |
Correct |
1871 ms |
298784 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
121 ms |
14644 KB |
Output is correct |
7 |
Correct |
145 ms |
21304 KB |
Output is correct |
8 |
Correct |
141 ms |
21304 KB |
Output is correct |
9 |
Correct |
140 ms |
21432 KB |
Output is correct |
10 |
Correct |
110 ms |
12080 KB |
Output is correct |
11 |
Correct |
152 ms |
21304 KB |
Output is correct |
12 |
Correct |
137 ms |
21432 KB |
Output is correct |
13 |
Correct |
138 ms |
21420 KB |
Output is correct |
14 |
Correct |
60 ms |
7260 KB |
Output is correct |
15 |
Correct |
137 ms |
15148 KB |
Output is correct |
16 |
Correct |
73 ms |
6512 KB |
Output is correct |
17 |
Correct |
150 ms |
21976 KB |
Output is correct |
18 |
Correct |
1516 ms |
205380 KB |
Output is correct |
19 |
Correct |
1478 ms |
205588 KB |
Output is correct |
20 |
Correct |
1554 ms |
205592 KB |
Output is correct |
21 |
Correct |
135 ms |
22060 KB |
Output is correct |
22 |
Correct |
138 ms |
22072 KB |
Output is correct |
23 |
Correct |
170 ms |
11832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
66 ms |
5872 KB |
Output is correct |
3 |
Correct |
79 ms |
5872 KB |
Output is correct |
4 |
Correct |
1133 ms |
79712 KB |
Output is correct |
5 |
Correct |
77 ms |
5872 KB |
Output is correct |
6 |
Correct |
72 ms |
5872 KB |
Output is correct |
7 |
Correct |
76 ms |
5872 KB |
Output is correct |
8 |
Correct |
82 ms |
6836 KB |
Output is correct |
9 |
Correct |
85 ms |
6836 KB |
Output is correct |
10 |
Correct |
65 ms |
5872 KB |
Output is correct |
11 |
Correct |
64 ms |
5872 KB |
Output is correct |
12 |
Correct |
58 ms |
7260 KB |
Output is correct |
13 |
Correct |
77 ms |
6452 KB |
Output is correct |
14 |
Correct |
668 ms |
48484 KB |
Output is correct |
15 |
Correct |
62 ms |
6512 KB |
Output is correct |
16 |
Correct |
611 ms |
64648 KB |
Output is correct |
17 |
Correct |
921 ms |
75768 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
1152 KB |
Output is correct |
3 |
Correct |
7 ms |
1376 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
7 ms |
1376 KB |
Output is correct |
17 |
Correct |
66 ms |
7536 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
391 ms |
32632 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1112 ms |
125948 KB |
Output is correct |
31 |
Correct |
1910 ms |
298804 KB |
Output is correct |
32 |
Correct |
1871 ms |
298784 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1503 ms |
204824 KB |
Output is correct |
35 |
Correct |
853 ms |
68460 KB |
Output is correct |
36 |
Correct |
1478 ms |
204828 KB |
Output is correct |
37 |
Correct |
1588 ms |
204908 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
121 ms |
14644 KB |
Output is correct |
40 |
Correct |
145 ms |
21304 KB |
Output is correct |
41 |
Correct |
141 ms |
21304 KB |
Output is correct |
42 |
Correct |
140 ms |
21432 KB |
Output is correct |
43 |
Correct |
110 ms |
12080 KB |
Output is correct |
44 |
Correct |
152 ms |
21304 KB |
Output is correct |
45 |
Correct |
137 ms |
21432 KB |
Output is correct |
46 |
Correct |
138 ms |
21420 KB |
Output is correct |
47 |
Correct |
60 ms |
7260 KB |
Output is correct |
48 |
Correct |
137 ms |
15148 KB |
Output is correct |
49 |
Correct |
73 ms |
6512 KB |
Output is correct |
50 |
Correct |
150 ms |
21976 KB |
Output is correct |
51 |
Correct |
1516 ms |
205380 KB |
Output is correct |
52 |
Correct |
1478 ms |
205588 KB |
Output is correct |
53 |
Correct |
1554 ms |
205592 KB |
Output is correct |
54 |
Correct |
135 ms |
22060 KB |
Output is correct |
55 |
Correct |
138 ms |
22072 KB |
Output is correct |
56 |
Correct |
170 ms |
11832 KB |
Output is correct |
57 |
Correct |
0 ms |
384 KB |
Output is correct |
58 |
Correct |
66 ms |
5872 KB |
Output is correct |
59 |
Correct |
79 ms |
5872 KB |
Output is correct |
60 |
Correct |
1133 ms |
79712 KB |
Output is correct |
61 |
Correct |
77 ms |
5872 KB |
Output is correct |
62 |
Correct |
72 ms |
5872 KB |
Output is correct |
63 |
Correct |
76 ms |
5872 KB |
Output is correct |
64 |
Correct |
82 ms |
6836 KB |
Output is correct |
65 |
Correct |
85 ms |
6836 KB |
Output is correct |
66 |
Correct |
65 ms |
5872 KB |
Output is correct |
67 |
Correct |
64 ms |
5872 KB |
Output is correct |
68 |
Correct |
58 ms |
7260 KB |
Output is correct |
69 |
Correct |
77 ms |
6452 KB |
Output is correct |
70 |
Correct |
668 ms |
48484 KB |
Output is correct |
71 |
Correct |
62 ms |
6512 KB |
Output is correct |
72 |
Correct |
611 ms |
64648 KB |
Output is correct |
73 |
Correct |
921 ms |
75768 KB |
Output is correct |
74 |
Correct |
0 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
384 KB |
Output is correct |
76 |
Correct |
0 ms |
384 KB |
Output is correct |
77 |
Correct |
0 ms |
384 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
11 ms |
1728 KB |
Output is correct |
80 |
Correct |
717 ms |
49056 KB |
Output is correct |
81 |
Correct |
1027 ms |
80100 KB |
Output is correct |
82 |
Correct |
1554 ms |
205656 KB |
Output is correct |
83 |
Correct |
1545 ms |
205628 KB |
Output is correct |
84 |
Correct |
1506 ms |
205692 KB |
Output is correct |
85 |
Correct |
1530 ms |
205684 KB |
Output is correct |
86 |
Correct |
167 ms |
11768 KB |
Output is correct |
87 |
Correct |
0 ms |
384 KB |
Output is correct |