結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-10-13 22:51:48
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,457 ms / 2,500 ms
コード長 3,794 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 114,896 KB
実行使用メモリ 63,328 KB
最終ジャッジ日時 2023-10-13 22:52:13
合計ジャッジ時間 19,609 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 88 ms
4,348 KB
testcase_02 AC 72 ms
4,352 KB
testcase_03 AC 72 ms
4,352 KB
testcase_04 AC 71 ms
4,352 KB
testcase_05 AC 73 ms
4,348 KB
testcase_06 AC 97 ms
4,348 KB
testcase_07 AC 40 ms
4,348 KB
testcase_08 AC 40 ms
4,352 KB
testcase_09 AC 42 ms
4,348 KB
testcase_10 AC 41 ms
4,352 KB
testcase_11 AC 43 ms
4,352 KB
testcase_12 AC 41 ms
4,348 KB
testcase_13 AC 41 ms
4,348 KB
testcase_14 AC 42 ms
4,352 KB
testcase_15 AC 41 ms
4,348 KB
testcase_16 AC 39 ms
4,348 KB
testcase_17 AC 42 ms
4,348 KB
testcase_18 AC 41 ms
4,476 KB
testcase_19 AC 41 ms
4,348 KB
testcase_20 AC 41 ms
4,352 KB
testcase_21 AC 41 ms
4,352 KB
testcase_22 AC 41 ms
4,348 KB
testcase_23 AC 41 ms
4,352 KB
testcase_24 AC 41 ms
4,352 KB
testcase_25 AC 41 ms
4,352 KB
testcase_26 AC 40 ms
4,352 KB
testcase_27 AC 42 ms
4,348 KB
testcase_28 AC 41 ms
4,348 KB
testcase_29 AC 41 ms
4,348 KB
testcase_30 AC 50 ms
4,396 KB
testcase_31 AC 47 ms
4,396 KB
testcase_32 AC 45 ms
4,352 KB
testcase_33 AC 52 ms
5,832 KB
testcase_34 AC 58 ms
6,200 KB
testcase_35 AC 22 ms
4,356 KB
testcase_36 AC 59 ms
6,112 KB
testcase_37 AC 47 ms
6,624 KB
testcase_38 AC 58 ms
5,896 KB
testcase_39 AC 176 ms
11,112 KB
testcase_40 AC 54 ms
5,892 KB
testcase_41 AC 1,041 ms
49,700 KB
testcase_42 AC 61 ms
5,896 KB
testcase_43 AC 1,050 ms
61,480 KB
testcase_44 AC 1,040 ms
48,956 KB
testcase_45 AC 1,050 ms
63,088 KB
testcase_46 AC 1,009 ms
48,460 KB
testcase_47 AC 1,057 ms
61,588 KB
testcase_48 AC 169 ms
11,168 KB
testcase_49 AC 1,055 ms
49,644 KB
testcase_50 AC 1,074 ms
49,400 KB
testcase_51 AC 2,457 ms
4,348 KB
testcase_52 AC 657 ms
61,896 KB
testcase_53 AC 602 ms
48,620 KB
testcase_54 AC 633 ms
63,328 KB
testcase_55 AC 61 ms
4,348 KB
testcase_56 AC 55 ms
4,352 KB
testcase_57 AC 58 ms
4,352 KB
testcase_58 AC 63 ms
4,656 KB
testcase_59 AC 75 ms
6,124 KB
testcase_60 AC 86 ms
6,084 KB
testcase_61 AC 53 ms
5,912 KB
testcase_62 AC 53 ms
4,348 KB
testcase_63 AC 54 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum E = 30;
enum INF = 10L^^9;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const M = readInt;
        const N = readInt;
        auto B = new int[M]; foreach (x; 0 .. M) B[x] = readInt;
        auto C = new int[N]; foreach (y; 0 .. N) C[y] = readInt;
        auto A = new int[][](M, N);
        foreach (x; 0 .. M) foreach (y; 0 .. N) A[x][y] = readInt;
        
        bool ok = true;
        foreach (x; 0 .. M) foreach (y; 0 .. N) {
          ok = ok && !(A[0][0] ^ A[0][y] ^ A[x][0] ^ A[x][y]);
        }
        long ans = INF;
        if (ok) {
          auto bs = new int[M];
          auto cs = new int[N];
          foreach (y; 0 .. N) cs[y] = A[0][y] ^ bs[0];
          foreach (x; 1 .. M) bs[x] = A[x][0] ^ cs[0];
          debug {
            writeln("bs = ", bs, ", cs = ", cs);
          }
          alias Entry = Tuple!(int, "key", int, "val");
          Entry[] es;
          void add(int l, int r, int val) {
            es ~= Entry(l, +val);
            es ~= Entry(r, -val);
          }
          add(0, 1 << E, 0);
          void check(int D, int d) {
            debug {
              writeln("check ", D, " ", d);
            }
            const e0 = D ? (bsr(D) + 1) : 0;
            int l = (d >> e0) << e0;
            int r = l + (1 << e0);
            add(0, l, INF);
            add(r, 1 << E, INF);
            foreach_reverse (e; 0 .. e0) {
              const mid = (l + r) >> 1;
              if (D >> e & 1) {
                if (d >> e & 1) {
                  add(mid, r, 1);
                  r = mid;
                } else {
                  add(l, mid, 1);
                  l = mid;
                }
              } else {
                if (d >> e & 1) {
                  add(l, mid, 2);
                  l = mid;
                } else {
                  add(mid, r, 2);
                  r = mid;
                }
              }
            }
            add(l, r, 1);
            add(d, d + 1, -1);
          }
          foreach (x; 0 .. M) check(B[x], bs[x]);
          foreach (y; 0 .. N) check(C[y], cs[y]);
          es.sort;
          const esLen = cast(int)(es.length);
          long now;
          foreach (j; 0 .. esLen - 1) {
            now += es[j].val;
            if (es[j].key < es[j + 1].key) {
              debug {
                writefln("[%s, %s): %s", es[j].key, es[j + 1].key, now);
              }
              chmin(ans, now);
            }
          }
        }
        writeln((ans < INF) ? ans : -1);
      }
    }
  } catch (EOFException e) {
  }
}
0