結果
問題 | No.755 Zero-Sum Rectangle |
ユーザー |
![]() |
提出日時 | 2018-12-03 23:30:03 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 397 ms / 2,000 ms |
コード長 | 2,965 bytes |
コンパイル時間 | 2,111 ms |
コンパイル使用メモリ | 166,400 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-13 01:59:12 |
合計ジャッジ時間 | 6,889 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 12 |
ソースコード
import std.stdio, std.conv, std.string, std.bigint;import std.math, std.random, std.datetime;import std.array, std.range, std.algorithm, std.container;string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }void main(){int n = read.to!int;int m = read.to!int;long[131][131] ass;foreach(i; 1 .. m + 1) foreach(j; 1 .. m + 1) ass[i][j] = read.to!long;int[2][] xs;foreach(j; 0 .. n) xs ~= [read.to!int, read.to!int];// 左端と上端は番兵0// そこを四隅とする和がゼロの矩形が何個あるかint[131][131] ltCount;int[131][131] rtCount;int[131][131] lbCount;int[131][131] rbCount;// 左端からそこまでの(行内での)和long[131][131] rowsum;foreach(i; 0 .. m + 1){rowsum[i][0] = ass[i][0];foreach(j; 1 .. m + 1) rowsum[i][j] = rowsum[i][j - 1] + ass[i][j];}debug "rowsum".writeln, rowsum.dump(m);// 左上隅からそこまでの(矩形の)和long[131][131] rectsum;foreach(j; 0 .. m + 1){rectsum[0][j] = rowsum[0][j];foreach(i; 1 .. m + 1) rectsum[i][j] = rectsum[i - 1][j] + rowsum[i][j];}debug "rectsum".writeln, rectsum.dump(m);// ゼロ矩形の四隅を検出する// それは、rectsum[i1 - 1][j1 - 1] - rectsum[i1 - 1][j2] == rectsum[i2][j1 - 1] - rectsum[i2][j2]// であるような (i1, j1), (i2, j2), (i1, j1), (i2, j2)foreach(j1; 1 .. m + 1) foreach(j2; j1 .. m + 1){int[][long] dic;foreach(i2; 0 .. m + 1){long d = rectsum[i2][j1 - 1] - rectsum[i2][j2];if(d in dic) foreach(i; dic[d]){ltCount[i + 1][j1] += 1, rtCount[i + 1][j2] += 1;lbCount[i2][j1] += 1, rbCount[i2][j2] += 1;debug writeln("(", i + 1, ", ", j1, ")-(", i2, ", ", j2, "), ", d);}dic[d] ~= i2;}}debug "ltCount".writeln, ltCount.dump(m);debug "rtCount".writeln, rtCount.dump(m);debug "lbCount".writeln, lbCount.dump(m);debug "rbCount".writeln, rbCount.dump(m);// そこを上辺、下辺とするゼロ矩形が何個あるかint[131][131] topCount;int[131][131] botCount;foreach(i; 1 .. m + 1){topCount[i][1] = ltCount[i][1];botCount[i][1] = lbCount[i][1];foreach(j; 2 .. m + 1){topCount[i][j] = topCount[i][j - 1] - rtCount[i][j - 1] + ltCount[i][j];botCount[i][j] = botCount[i][j - 1] - rbCount[i][j - 1] + lbCount[i][j];}}debug "topCount".writeln, topCount.dump(m);debug "botCount".writeln, botCount.dump(m);// そこを含むゼロ矩形が何個あるかint[131][131] answer;foreach(j; 1 .. m + 1){answer[1][j] = topCount[1][j];foreach(i; 2 .. m + 1){answer[i][j] = answer[i - 1][j] - botCount[i - 1][j] + topCount[i][j];}}debug "answer".writeln, answer.dump(m);// 出力foreach(x; xs) answer[x[0]][x[1]].writeln;}void dump(int[131][131] A, int m){foreach(r; A[0 .. m + 1]) r[0 .. m + 1].writeln;}void dump(long[131][131] A, int m){foreach(r; A[0 .. m + 1]) r[0 .. m + 1].writeln;}