結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-07-27 23:38:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,500 ms
コード長 4,420 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 80,648 KB
実行使用メモリ 57,236 KB
最終ジャッジ日時 2023-10-13 18:17:40
合計ジャッジ時間 5,668 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,628 KB
testcase_01 AC 16 ms
7,444 KB
testcase_02 AC 14 ms
7,520 KB
testcase_03 AC 15 ms
7,456 KB
testcase_04 AC 15 ms
7,432 KB
testcase_05 AC 15 ms
7,428 KB
testcase_06 AC 16 ms
7,656 KB
testcase_07 AC 10 ms
7,484 KB
testcase_08 AC 10 ms
7,484 KB
testcase_09 AC 10 ms
7,460 KB
testcase_10 AC 10 ms
7,484 KB
testcase_11 AC 11 ms
7,440 KB
testcase_12 AC 11 ms
7,432 KB
testcase_13 AC 10 ms
7,660 KB
testcase_14 AC 10 ms
7,484 KB
testcase_15 AC 10 ms
7,416 KB
testcase_16 AC 11 ms
7,484 KB
testcase_17 AC 11 ms
7,432 KB
testcase_18 AC 11 ms
7,480 KB
testcase_19 AC 11 ms
7,448 KB
testcase_20 AC 10 ms
7,484 KB
testcase_21 AC 10 ms
7,492 KB
testcase_22 AC 10 ms
7,572 KB
testcase_23 AC 10 ms
7,464 KB
testcase_24 AC 11 ms
7,664 KB
testcase_25 AC 11 ms
7,488 KB
testcase_26 AC 10 ms
7,460 KB
testcase_27 AC 10 ms
7,632 KB
testcase_28 AC 10 ms
7,500 KB
testcase_29 AC 10 ms
7,436 KB
testcase_30 AC 8 ms
7,524 KB
testcase_31 AC 8 ms
7,552 KB
testcase_32 AC 7 ms
7,584 KB
testcase_33 AC 8 ms
7,512 KB
testcase_34 AC 10 ms
7,824 KB
testcase_35 AC 7 ms
7,732 KB
testcase_36 AC 10 ms
7,924 KB
testcase_37 AC 9 ms
7,792 KB
testcase_38 AC 12 ms
8,608 KB
testcase_39 AC 23 ms
15,848 KB
testcase_40 AC 11 ms
8,460 KB
testcase_41 AC 130 ms
54,752 KB
testcase_42 AC 12 ms
8,612 KB
testcase_43 AC 131 ms
57,236 KB
testcase_44 AC 116 ms
54,752 KB
testcase_45 AC 126 ms
57,112 KB
testcase_46 AC 117 ms
54,812 KB
testcase_47 AC 127 ms
57,088 KB
testcase_48 AC 49 ms
27,896 KB
testcase_49 AC 122 ms
56,876 KB
testcase_50 AC 124 ms
56,768 KB
testcase_51 AC 165 ms
7,444 KB
testcase_52 AC 101 ms
42,752 KB
testcase_53 AC 85 ms
40,272 KB
testcase_54 AC 95 ms
42,792 KB
testcase_55 AC 12 ms
7,580 KB
testcase_56 AC 11 ms
7,704 KB
testcase_57 AC 11 ms
7,580 KB
testcase_58 AC 11 ms
7,776 KB
testcase_59 AC 12 ms
7,908 KB
testcase_60 AC 13 ms
8,024 KB
testcase_61 AC 10 ms
7,636 KB
testcase_62 AC 11 ms
7,760 KB
testcase_63 AC 8 ms
7,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cstdint>
#include <iostream>
#include <vector>

using Value = uint32_t;
using Weight = int64_t;

// nm の最大値
constexpr size_t MAX_NM = 50000;
constexpr size_t BIT_NUM = 30;

namespace binary_trie {
    // ノードは整数 ID で管理
    using Node = size_t;

    constexpr Node null_node = ~size_t(0);

    // Binary Trie のノード数の上界
    constexpr size_t MAX_NODE_NUM = 3 * (MAX_NM + 1) * BIT_NUM + 1;

    // child[0][i] = ノード i の左子
    // child[1][i] = ノード i の右子
    std::array<std::array<Node, MAX_NODE_NUM>, 2> child{};
    // weight[i] = ノード i とその親を結ぶ辺の重み 
    std::array<Weight, MAX_NODE_NUM> weight{};

    // 使用しているノードの個数
    size_t node_num = 0;

    Node create_node() {
        const Node new_node = node_num++;
        // 以前のテストケースの情報が残っている可能性があるので、初期化する
        child[0][new_node] = child[1][new_node] = null_node;
        weight[new_node] = 0;
        return new_node;
    }
    void clear_nodes() {
        node_num = 0;
    }
}

// @returns floor(log_2(v))
size_t bit_width(Value v) {
    size_t x = 0;
    while (Value(1) << (x + 1) <= v) ++x;
    return x;
}
// @returns true if the k-th bit of v is 1, false otherwise
bool get_bit(Value v, size_t k) {
    return (v >> k) & 1;
}

int64_t solve(
    const size_t n, const size_t m,
    const std::vector<Value>& R,
    const std::vector<Value>& C,
    const std::vector<std::vector<Value>>& A) {

    static constexpr Weight INF = Weight(1) << 30;

    for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) {
        if (A[0][0] ^ A[0][j] ^ A[i][0] ^ A[i][j]) {
            return -1;
        }
    }

    using binary_trie::Node;
    using binary_trie::clear_nodes, binary_trie::create_node;
    using binary_trie::null_node, binary_trie::child, binary_trie::weight, binary_trie::node_num;

    clear_nodes();
    const Node root = create_node();

    // f(x) += W * [(x ^ Y) > Z] for all x
    auto add_weight = [root](const Value Y, const Value Z, const Weight W) {
        const Value YZ = Y ^ Z;

        Node cur = root;
        for (size_t bit = BIT_NUM; bit-- > 0;) {
            const bool edge_label = get_bit(YZ, bit);
            if (child[edge_label][cur] == null_node) {
                child[edge_label][cur] = create_node();
            }
            Node nxt = child[edge_label][cur];
            // 対応する Z の bit が 0 のとき、cur の子のうち nxt でない方の子部分木にコスト W を加算
            if (not get_bit(Z, bit)) {
                weight[cur] += W;
                weight[nxt] -= W;
            }
            cur = nxt;
        }
    };
    auto add_weights = [add_weight](const Value Y, const Value v) {
        if (v > 0) {
            add_weight(Y, 0, 1);
            add_weight(Y, v, 1);
            add_weight(Y, 2 * (1 << bit_width(v)) - 1, INF);
        } else {
            add_weight(Y, 0, INF);
        }
    };
    for (size_t i = 0; i < n; ++i) {
        const Value Y = A[0][0] ^ A[i][0];
        add_weights(Y, R[i]);
    }
    for (size_t j = 0; j < m; ++j) {
        const Value Y = A[0][j];
        add_weights(Y, C[j]);
    }

    Weight ans = INF;
    // 親節点ID < 子節点ID なので、IDを昇順に見れば正しく重みを伝播できる
    for (Node node = 0; node < node_num; ++node) {
        for (const bool edge_label : { 0, 1 }) {
            const Node child_node = child[edge_label][node];
            if (child_node != null_node) {
                weight[child_node] += weight[node];
            } else {
                // 子節点が null ==> その子部分木の辺は全て重み0 ==> コスト確定
                ans = std::min(ans, weight[node]);
            }
        }
    }

    return ans != INF ? ans : -1;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    size_t t;
    std::cin >> t;

    for (size_t case_id = 0; case_id < t; ++case_id) {
        size_t n, m;
        std::cin >> n >> m;

        std::vector<Value> R(n), C(m);
        for (auto&& elm : R) std::cin >> elm;
        for (auto&& elm : C) std::cin >> elm;

        std::vector A(n, std::vector<Value>(m));
        for (auto&& row : A) for (auto&& elm : row) std::cin >> elm;

        std::cout << solve(n, m, R, C, A) << '\n';
    }
}
0