結果
問題 | No.1059 素敵な集合 |
ユーザー | kimiyuki |
提出日時 | 2020-05-22 22:15:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 573 ms / 2,000 ms |
コード長 | 3,155 bytes |
コンパイル時間 | 2,476 ms |
コンパイル使用メモリ | 216,288 KB |
実行使用メモリ | 172,792 KB |
最終ジャッジ日時 | 2024-07-23 09:30:35 |
合計ジャッジ時間 | 5,442 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 573 ms
172,792 KB |
testcase_02 | AC | 40 ms
15,404 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 30 ms
12,668 KB |
testcase_07 | AC | 31 ms
12,696 KB |
testcase_08 | AC | 42 ms
18,408 KB |
testcase_09 | AC | 8 ms
6,944 KB |
testcase_10 | AC | 83 ms
31,608 KB |
testcase_11 | AC | 35 ms
15,568 KB |
testcase_12 | AC | 16 ms
7,912 KB |
testcase_13 | AC | 75 ms
29,008 KB |
testcase_14 | AC | 3 ms
6,944 KB |
testcase_15 | AC | 146 ms
54,564 KB |
testcase_16 | AC | 36 ms
15,208 KB |
testcase_17 | AC | 36 ms
15,152 KB |
testcase_18 | AC | 12 ms
7,912 KB |
testcase_19 | AC | 483 ms
159,252 KB |
testcase_20 | AC | 498 ms
151,368 KB |
testcase_21 | AC | 142 ms
49,108 KB |
ソースコード
#line 1 "main.cpp" #include <bits/stdc++.h> #line 2 "/home/user/GitHub/competitive-programming-library/utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) #line 3 "/home/user/GitHub/competitive-programming-library/data_structure/union_find_tree.hpp" /** * @brief Union-Find Tree * @docs data_structure/union_find_tree.md * @note union-by-size + path-compression */ struct union_find_tree { std::vector<int> data; union_find_tree() = default; explicit union_find_tree(std::size_t n) : data(n, -1) {} bool is_root(int i) { return data[i] < 0; } int find_root(int i) { return is_root(i) ? i : (data[i] = find_root(data[i])); } int tree_size(int i) { return - data[find_root(i)]; } int unite_trees(int i, int j) { i = find_root(i); j = find_root(j); if (i != j) { if (tree_size(i) < tree_size(j)) std::swap(i, j); data[i] += data[j]; data[j] = i; } return i; } bool is_same(int i, int j) { return find_root(i) == find_root(j); } }; #line 8 "/home/user/GitHub/competitive-programming-library/graph/kruskal.hpp" /** * @brief minimum spanning tree / 最小全域木 (Kruskal's method) * @note $O(E \log E)$ * @note it becomes a forest if the given graph is not connected * @return a list of indices of edges */ template <typename T> std::vector<int> compute_minimum_spanning_tree(int n, std::vector<std::tuple<int, int, T> > edges) { std::vector<int> order(edges.size()); std::iota(ALL(order), 0); std::sort(ALL(order), [&](int i, int j) { return std::make_pair(std::get<2>(edges[i]), i) < std::make_pair(std::get<2>(edges[j]), j); }); std::vector<int> tree; union_find_tree uft(n); for (int i : order) { int x = std::get<0>(edges[i]); int y = std::get<1>(edges[i]); if (not uft.is_same(x, y)) { uft.unite_trees(x, y); tree.push_back(i); } } return tree; } #line 4 "main.cpp" using namespace std; int64_t solve(int l, int r) { // [l, r) vector<tuple<int, int, int64_t> > edges; auto use = [&](int i, int j) { edges.emplace_back(i - l, j - l, min(i % j, j % i)); }; REP3 (i, l, r) { for (int j = i; j < r; j += i) { REP (k, 2) if (i < j + k and j + k < r) { use(i, j + k); } } } int64_t ans = 0; vector<int> used = compute_minimum_spanning_tree(r - l, edges); for (int i : used) { ans += get<2>(edges[i]); } return ans; } // generated by online-judge-template-generator v4.1.0 (https://github.com/kmyk/online-judge-template-generator) int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr char endl = '\n'; int64_t L, R; cin >> L >> R; ++ R; auto ans = solve(L, R); cout << ans << endl; return 0; }