結果

問題 No.440 2次元チワワ問題
ユーザー 37zigen37zigen
提出日時 2016-10-29 00:35:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,840 ms / 5,000 ms
コード長 3,264 bytes
コンパイル時間 3,627 ms
コンパイル使用メモリ 78,180 KB
実行使用メモリ 63,644 KB
最終ジャッジ日時 2024-11-24 06:55:39
合計ジャッジ時間 25,168 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,064 KB
testcase_01 AC 117 ms
39,936 KB
testcase_02 AC 136 ms
41,512 KB
testcase_03 AC 138 ms
41,228 KB
testcase_04 AC 170 ms
42,024 KB
testcase_05 AC 143 ms
41,248 KB
testcase_06 AC 146 ms
41,856 KB
testcase_07 AC 395 ms
49,348 KB
testcase_08 AC 713 ms
49,924 KB
testcase_09 AC 914 ms
53,480 KB
testcase_10 AC 342 ms
48,072 KB
testcase_11 AC 506 ms
48,716 KB
testcase_12 AC 417 ms
51,008 KB
testcase_13 AC 913 ms
58,268 KB
testcase_14 AC 306 ms
47,364 KB
testcase_15 AC 942 ms
58,340 KB
testcase_16 AC 507 ms
48,536 KB
testcase_17 AC 1,366 ms
62,772 KB
testcase_18 AC 1,351 ms
63,552 KB
testcase_19 AC 1,366 ms
63,620 KB
testcase_20 AC 1,337 ms
63,432 KB
testcase_21 AC 1,840 ms
63,540 KB
testcase_22 AC 1,828 ms
63,644 KB
testcase_23 AC 1,635 ms
63,344 KB
testcase_24 AC 1,778 ms
63,620 KB
testcase_25 AC 1,027 ms
60,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Arrays;
import java.util.Scanner;


public class Q440 {
	public static void main(String[] args) {
		new Q440().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		char[][] map = new char[H][W];
		for (int i = 0; i < H; ++i) {
			map[i] = sc.next().toCharArray();
		}
		int Q = sc.nextInt();
		int[] a = new int[Q];
		int[] b = new int[Q];
		int[] c = new int[Q];
		int[] d = new int[Q];
		for (int i = 0; i < Q; ++i) {
			b[i] = sc.nextInt() - 1;
			a[i] = sc.nextInt() - 1;
			d[i] = sc.nextInt() - 1;
			c[i] = sc.nextInt() - 1;

		}
		long[] ans1 = solver(H, W,Q, map, a, b, c, d);
		
		char[][] aaa=new char[H][W];
		for(int i=0;i<H;++i){
			for(int j=0;j<W;++j){
				aaa[i][j]=map[i][W-j-1];
			}
		}
		char[][] map2=new char[H][W];
		for(int i=0;i<H;++i){
			for(int j=0;j<W;++j){
				map2[i][j]=aaa[H-i-1][j];
			}
		}
		
		int[] a2=new int[Q];
		int[] b2=new int[Q];
		int[] c2=new int[Q];
		int[] d2=new int[Q];
		for(int i=0;i<Q;++i){
			a2[i]=W-a[i]-1;
			c2[i]=W-c[i]-1;
			b2[i]=H-b[i]-1;
			d2[i]=H-d[i]-1;
		}
		long[] ans2=solver(H,W,Q,map2,c2,d2,a2,b2);
		for(int i=0;i<Q;++i){
			System.out.println(ans1[i]+ans2[i]);
		}
	}

	long[] solver(int H, int W, int Q,char[][] map, int[] a, int[] b, int[] c, int[] d) {
		int[][] WsV = new int[W][H];
		int[][] WsH = new int[H][W];
		long[][] CWsV = new long[W][H];
		long[][] CWsH = new long[H][W];
		long[][] CWWsV = new long[W][H];
		long[][] CWWsH = new long[H][W];
		for (int i = 0; i < W; ++i) {
			for (int j = 0; j < H; ++j) {
				WsV[i][j] = (j > 0 ? WsV[i][j - 1] : 0) + (map[j][i] == 'w' ? 1 : 0);
				if (j > 0)
					CWsV[i][j] = CWsV[i][j - 1] + (map[j][i] == 'w' ? (j - WsV[i][j - 1]) : 0);
				if (j > 1) {
					CWWsV[i][j] = CWWsV[i][j - 1];
					if (map[j][i] == 'w')
						CWWsV[i][j] += CWsV[i][j - 1];
				}
			}
		}

		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				WsH[i][j] = (j > 0 ? WsH[i][j - 1] : 0) + (map[i][j] == 'w' ? 1 : 0);// ok
				if (j > 0)
					CWsH[i][j] = CWsH[i][j - 1] + (map[i][j] == 'w' ? (j - WsH[i][j - 1]) : 0);// ok
				if (j > 1) {
					CWWsH[i][j] = CWWsH[i][j - 1];
					if (map[i][j] == 'w')
						CWWsH[i][j] += CWsH[i][j - 1];
				}
			}
		}

		long ans[] = new long[Q];
		for (int i = 0; i < Q; ++i) {
			for (int x = a[i]; x <= c[i]; ++x) {
				ans[i] += CWWsV[x][d[i]] - (b[i] > 0 ? CWWsV[x][b[i] - 1] : 0);
				ans[i] -= (b[i] - (b[i] > 0 ? WsV[x][b[i] - 1] : 0))
						* (WsV[x][d[i]] - (b[i] > 0 ? WsV[x][b[i] - 1] : 0))
						* (WsV[x][d[i]] - (b[i] > 0 ? WsV[x][b[i] - 1] : 0) - 1) / 2;// [c][ww]
				ans[i] -= (b[i] > 0 ? CWsV[x][b[i] - 1] : 0) * (WsV[x][d[i]] - (b[i] > 0 ? WsV[x][b[i] - 1] : 0));// [cw][w]
			}
			for (int y = b[i]; y <= d[i]; ++y) {
				ans[i] += CWWsH[y][c[i]] - (a[i] > 0 ? CWWsH[y][a[i] - 1] : 0);
				ans[i] -= (a[i] - (a[i] > 0 ? WsH[y][a[i] - 1] : 0))
						* (WsH[y][c[i]] - (a[i] > 0 ? WsH[y][a[i] - 1] : 0))
						* (WsH[y][c[i]] - (a[i] > 0 ? WsH[y][a[i] - 1] : 0) - 1) / 2;
				ans[i] -= (a[i] > 0 ? CWsH[y][a[i] - 1] : 0) * (WsH[y][c[i]] - (a[i] > 0 ? WsH[y][a[i] - 1] : 0));
			}
		}
		return ans;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0