結果
問題 | No.307 最近色塗る問題多くない? |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-04-08 23:33:21 |
言語 | D (dmd 2.106.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,112 bytes |
コンパイル時間 | 8,527 ms |
コンパイル使用メモリ | 333,112 KB |
実行使用メモリ | 14,016 KB |
最終ジャッジ日時 | 2024-06-13 04:57:49 |
合計ジャッジ時間 | 14,953 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,892 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 5 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,948 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 1,136 ms
11,816 KB |
testcase_08 | TLE | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
ソースコード
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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; } real readReal() { return readToken.to!real; } 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)); } int H, W; int[][] A; int Q; int[] R, C, X; void main() { try { for (; ; ) { H = readInt(); W = readInt(); A = new int[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { A[x][y] = readInt(); } Q = readInt(); R = new int[Q]; C = new int[Q]; X = new int[Q]; foreach (q; 0 .. Q) { R[q] = readInt() - 1; C[q] = readInt() - 1; X[q] = readInt(); } auto uf = new int[H * W]; auto colors = new int[H * W]; auto graph = new RedBlackTree!int[H * W]; int root(int u) { return (uf[u] < 0) ? u : (uf[u] = root(uf[u])); } void connect(int u, int v) { debug { writeln("connect ", u, " ", v); } u = root(u); v = root(v); if (u != v) { if (uf[u] > uf[v]) { swap(u, v); } uf[u] += uf[v]; uf[v] = u; foreach (w; graph[v]) { graph[u].insert(w); } graph[v].clear; } } uf[] = -1; foreach (x; 0 .. H) foreach (y; 0 .. W) { const u = x * W + y; colors[u] = A[x][y]; graph[u] = new RedBlackTree!int(); } foreach (x; 0 .. H) foreach (y; 0 .. W) { if (x > 0) { const u = (x - 1) * W + y, v = x * W + y; graph[u].insert(v); graph[v].insert(u); } if (y > 0) { const u = x * W + (y - 1), v = x * W + y; graph[u].insert(v); graph[v].insert(u); } } foreach (x; 0 .. H) foreach (y; 0 .. W) { if (x > 0 && A[x - 1][y] == A[x][y]) { const u = (x - 1) * W + y, v = x * W + y; connect(u, v); } if (y > 0 && A[x][y - 1] == A[x][y]) { const u = x * W + (y - 1), v = x * W + y; connect(u, v); } } debug { writeln("root = "); foreach (x; 0 .. H) { foreach (y; 0 .. W) { write(" ", root(x * W + y)); } writeln(); } writeln("colors = "); foreach (x; 0 .. H) { foreach (y; 0 .. W) { write(" ", colors[root(x * W + y)]); } writeln(); } writeln("graph = ", graph); } foreach (q; 0 .. Q) { const u = root(R[q] * W + C[q]); if (colors[u] != X[q]) { colors[u] = X[q]; int[] vs; foreach (v; graph[u]) { vs ~= root(v); } vs = vs.sort.uniq.array; foreach (v; vs) { connect(u, v); } } } auto ans = new int[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { ans[x][y] = colors[root(x * W + y)]; } foreach (x; 0 .. H) { writeln(ans[x].to!string.replaceAll(regex(`[\[\],]`), "")); } } } catch (EOFException e) { } }