結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-07-27 23:53:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,371 ms / 2,500 ms
コード長 3,313 bytes
コンパイル時間 3,747 ms
コンパイル使用メモリ 98,412 KB
実行使用メモリ 273,268 KB
最終ジャッジ日時 2023-10-13 18:18:32
合計ジャッジ時間 19,597 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 81 ms
4,348 KB
testcase_02 AC 57 ms
4,348 KB
testcase_03 AC 57 ms
4,352 KB
testcase_04 AC 60 ms
4,348 KB
testcase_05 AC 63 ms
4,348 KB
testcase_06 AC 98 ms
4,352 KB
testcase_07 AC 17 ms
4,372 KB
testcase_08 AC 18 ms
4,348 KB
testcase_09 AC 19 ms
4,352 KB
testcase_10 AC 19 ms
4,352 KB
testcase_11 AC 19 ms
4,352 KB
testcase_12 AC 18 ms
4,352 KB
testcase_13 AC 20 ms
4,372 KB
testcase_14 AC 17 ms
4,352 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 17 ms
4,348 KB
testcase_17 AC 18 ms
4,352 KB
testcase_18 AC 17 ms
4,352 KB
testcase_19 AC 20 ms
4,352 KB
testcase_20 AC 19 ms
4,348 KB
testcase_21 AC 19 ms
4,356 KB
testcase_22 AC 17 ms
4,348 KB
testcase_23 AC 19 ms
4,352 KB
testcase_24 AC 17 ms
4,352 KB
testcase_25 AC 19 ms
4,352 KB
testcase_26 AC 15 ms
4,348 KB
testcase_27 AC 15 ms
4,348 KB
testcase_28 AC 17 ms
4,352 KB
testcase_29 AC 16 ms
4,352 KB
testcase_30 AC 21 ms
4,352 KB
testcase_31 AC 20 ms
4,352 KB
testcase_32 AC 20 ms
4,352 KB
testcase_33 AC 22 ms
4,368 KB
testcase_34 AC 33 ms
5,712 KB
testcase_35 AC 13 ms
4,728 KB
testcase_36 AC 34 ms
6,516 KB
testcase_37 AC 31 ms
6,596 KB
testcase_38 AC 41 ms
9,676 KB
testcase_39 AC 132 ms
28,072 KB
testcase_40 AC 38 ms
9,104 KB
testcase_41 AC 915 ms
158,932 KB
testcase_42 AC 44 ms
10,108 KB
testcase_43 AC 891 ms
160,776 KB
testcase_44 AC 925 ms
158,148 KB
testcase_45 AC 918 ms
160,200 KB
testcase_46 AC 907 ms
156,776 KB
testcase_47 AC 906 ms
159,860 KB
testcase_48 AC 1,371 ms
273,268 KB
testcase_49 AC 880 ms
156,152 KB
testcase_50 AC 879 ms
155,568 KB
testcase_51 AC 615 ms
4,496 KB
testcase_52 AC 777 ms
160,064 KB
testcase_53 AC 733 ms
157,256 KB
testcase_54 AC 759 ms
160,512 KB
testcase_55 AC 36 ms
4,356 KB
testcase_56 AC 30 ms
4,352 KB
testcase_57 AC 30 ms
4,348 KB
testcase_58 AC 38 ms
4,976 KB
testcase_59 AC 46 ms
5,636 KB
testcase_60 AC 53 ms
6,480 KB
testcase_61 AC 35 ms
4,348 KB
testcase_62 AC 35 ms
4,348 KB
testcase_63 AC 22 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 区間列挙 + 座圧 + imos

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>

long long floor_pow2(long long n) {
    long long x = 1;
    while ((x << 1) <= n) x <<= 1;
    return x;
}

using Weight = long long;

constexpr int L = 30;

constexpr Weight inf = std::numeric_limits<int>::max();

long long solve(int n, int m, const std::vector<long long>& r, const std::vector<long long>& c, const std::vector<std::vector<long long>>& a) {
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
        if (a[0][0] ^ a[0][j] ^ a[i][0] ^ a[i][j]) {
            return -1;
        }
    }

    // (l, r, w): add w to x in [l, r)
    std::vector<std::tuple<long long, long long, Weight>> costs;

    // add f(X) = W * [X ^ Y > Z]
    auto add_cost = [&](long long Y, long long Z, Weight W) {
        const long long YZ = Y ^ Z;

        long long l = 0, r = 1LL << L;
        for (int bit = L - 1; bit >= 0; --bit) {
            long long mid = (l + r) >> 1;
            if ((YZ >> bit) & 1) {
                if (not ((Z >> bit) & 1)) {
                    costs.emplace_back(l, mid, W);
                }
                l = mid;
            } else {
                if (not ((Z >> bit) & 1)) {
                    costs.emplace_back(mid, r, W);
                }
                r = mid;
            }
        }
    };
    for (int i = 0; i < n; ++i) {
        long long Y = a[0][0] ^ a[i][0];
        if (r[i]) {
            add_cost(Y, 0, 1);
            add_cost(Y, r[i], 1);
            add_cost(Y, 2 * floor_pow2(r[i]) - 1, inf);
        } else {
            add_cost(Y, 0, inf);
        }
    }
    for (int j = 0; j < m; ++j) {
        long long Y = a[0][j];
        if (c[j]) {
            add_cost(Y, 0, 1);
            add_cost(Y, c[j], 1);
            add_cost(Y, 2 * floor_pow2(c[j]) - 1, inf);
        } else {
            add_cost(Y, 0, inf);
        }
    }
    std::vector<long long> xs{ 0, 1LL << L };
    for (auto [l, r, w] : costs) {
        xs.push_back(l);
        xs.push_back(r);
    }
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    const int k = xs.size();
    std::vector<Weight> imos(k);
    for (auto [l, r, w] : costs) {
        imos[std::lower_bound(xs.begin(), xs.end(), l) - xs.begin()] += w;
        imos[std::lower_bound(xs.begin(), xs.end(), r) - xs.begin()] -= w;
    }
    for (int i = 1; i < k; ++i) {
        imos[i] += imos[i - 1];
    }

    std::pair<Weight, long long> min_cost{ inf, 0 };
    for (int i = 0; i < k - 1; ++i) {
        min_cost = std::min(min_cost, std::make_pair(imos[i], xs[i]));
    }

    auto [cost, X] = min_cost;

    if (cost >= inf) {
        return -1;
    }

    return cost;
}

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

    int t;
    std::cin >> t;

    while (t-- > 0) {
        int n, m;
        std::cin >> n >> m;

        std::vector<long long> r(n), c(m);
        for (auto& e : r) std::cin >> e;
        for (auto& e : c) std::cin >> e;

        std::vector a(n, std::vector<long long>(m));
        for (auto& row : a) for (auto& e : row) {
            std::cin >> e;
        }

        std::cout << solve(n, m, r, c, a) << '\n';
    }

    return 0;
}
0