結果

問題 No.2505 matriX cOnstRuction
ユーザー suisensuisen
提出日時 2023-02-14 21:16:00
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
J_ERR  
(最初)
実行時間 -
コード長 3,299 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 320,232 KB
最終ジャッジ日時 2024-09-15 18:45:28
合計ジャッジ時間 31,593 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 596 ms
197,716 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
import sys
from typing import List, Tuple

def floor_pow2(n: int):
    x = 1
    while (x << 1) <= n:
        x <<= 1
    return x

class Node:
    def __init__(self) -> None:
        self.lch = None
        self.rch = None
        self.weight = 0
    
    def left_child_or_create(self) -> 'Node':
        if self.lch is None:
            self.lch = Node()
        return self.lch

    def right_child_or_create(self) -> 'Node':
        if self.rch is None:
            self.rch = Node()
        return self.rch

L = 30

inf = 1 << 30

def solve(n: int, m: int, R: List[int], C: List[int], A: List[List[int]]):
    for i, j in product(range(n), range(m)):
        if A[0][0] ^ A[0][j] ^ A[i][0] ^ A[i][j]:
            print(-1)
            return
        
    root = Node()

    # f(X) += W * [X ^ Y > Z]
    def add_weight(Y: int, Z: int, W: int):
        YZ = Y ^ Z
        cur = root
        for bit in reversed(range(L)):
            if (YZ >> bit) & 1:
                if not ((Z >> bit) & 1):
                    cur.left_child_or_create().weight += W
                cur = cur.right_child_or_create()
            else:
                if not ((Z >> bit) & 1):
                    cur.right_child_or_create().weight += W
                cur = cur.left_child_or_create()

    for i in range(n):
        Y = A[0][0] ^ A[i][0]
        if R[i]:
            add_weight(Y, 0, 1)
            add_weight(Y, R[i], 1)
            add_weight(Y, 2 * floor_pow2(R[i]) - 1, inf)
        else:
            add_weight(Y, 0, inf)

    for j in range(m):
        Y = A[0][j]
        if C[j]:
            add_weight(Y, 0, 1)
            add_weight(Y, C[j], 1)
            add_weight(Y, 2 * floor_pow2(C[j]) - 1, inf)
        else:
            add_weight(Y, 0, inf)

    min_weight = (inf, -1)
    
    st : List[Tuple[Node, int, int]] = [(root, 0, 1 << L)]
    while st:
        node, l, r = st.pop()

        weight = node.weight
        if r - l == 1:
            min_weight = min(min_weight, (weight, l))
            continue

        mid = (l + r) >> 1

        if node.lch is None:
            min_weight = min(min_weight, (weight, mid - 1))
        else:
            node.lch.weight += weight
            st.append((node.lch, l, mid))

        if node.rch is None:
            min_weight = min(min_weight, (weight, r - 1))
        else:
            node.rch.weight += weight
            st.append((node.rch, mid, r))
    
    cost, X = min_weight

    if cost >= inf:
        print(-1)
        return

    print(cost)
    for j in range(m):
        t = X ^ A[0][j]
        if t == 0:
            continue
        elif t <= C[j]:
            print('c', j + 1, t)
        else:
            print('c', j + 1, t ^ C[j])
            print('c', j + 1, C[j] + (j == 0))
    for i in range(n):
        s = X ^ A[0][0] ^ A[i][0]
        if s == 0:
            continue
        elif s <= R[i]:
            print('r', i + 1, s)
        else:
            print('r', i + 1, s ^ R[i])
            print('r', i + 1, R[i])

input = sys.stdin.readline

T = int(input())
for _ in range(T):
    n, m = map(int, input().split())
    R = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A = [list(map(int, input().split())) for _ in range(n)]

    solve(n, m, R, C, A)
0