結果

問題 No.755 Zero-Sum Rectangle
ユーザー iicafiaxusiicafiaxus
提出日時 2018-12-03 23:30:03
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 2,965 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 167,596 KB
実行使用メモリ 7,456 KB
最終ジャッジ日時 2023-09-03 21:20:26
合計ジャッジ時間 6,587 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 381 ms
5,204 KB
testcase_02 AC 374 ms
5,148 KB
testcase_03 AC 387 ms
5,264 KB
testcase_04 AC 389 ms
5,136 KB
testcase_05 AC 405 ms
5,944 KB
testcase_06 AC 415 ms
6,072 KB
testcase_07 AC 418 ms
7,456 KB
testcase_08 AC 419 ms
5,916 KB
testcase_09 AC 420 ms
5,948 KB
testcase_10 AC 420 ms
6,080 KB
testcase_11 AC 2 ms
4,376 KB
evil_1 AC 374 ms
5,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
}

0