結果

問題 No.440 2次元チワワ問題
ユーザー antaanta
提出日時 2016-10-29 00:26:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 5,000 ms
コード長 2,619 bytes
コンパイル時間 1,384 ms
コンパイル使用メモリ 171,784 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2023-08-15 21:58:30
合計ジャッジ時間 2,994 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,016 KB
testcase_01 AC 2 ms
11,768 KB
testcase_02 AC 2 ms
11,780 KB
testcase_03 AC 3 ms
11,776 KB
testcase_04 AC 3 ms
11,788 KB
testcase_05 AC 2 ms
11,944 KB
testcase_06 AC 2 ms
11,772 KB
testcase_07 AC 7 ms
12,048 KB
testcase_08 AC 14 ms
11,012 KB
testcase_09 AC 21 ms
12,588 KB
testcase_10 AC 7 ms
12,056 KB
testcase_11 AC 8 ms
12,348 KB
testcase_12 AC 8 ms
12,208 KB
testcase_13 AC 23 ms
13,220 KB
testcase_14 AC 4 ms
11,828 KB
testcase_15 AC 19 ms
13,376 KB
testcase_16 AC 6 ms
11,924 KB
testcase_17 AC 30 ms
13,812 KB
testcase_18 AC 27 ms
13,812 KB
testcase_19 AC 30 ms
13,800 KB
testcase_20 AC 28 ms
13,924 KB
testcase_21 AC 67 ms
14,004 KB
testcase_22 AC 67 ms
13,880 KB
testcase_23 AC 68 ms
13,796 KB
testcase_24 AC 67 ms
13,888 KB
testcase_25 AC 68 ms
14,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

class SolveForHorizontalPatterns {
public:
	void init(const vector<vector<char>> &S) {
		int H = (int)S.size(), W = (int)S[0].size();
		myW = W;

		rep(i, H) sum[0][i] = 0;
		rep(i, H) rep(j, W)
			sum[j + 1][i] = sum[j][i] + (S[i][j] == 'w');

		rep(i, H) sumL[0][i] = 0;
		rep(i, H + 1) sumLR[i][0] = 0;
		rep(j, W + 1) sumLR[0][j] = 0;
		rep(i, H) rep(j, W) {
			int wL = sum[j][i], cL = j - wL;
			int wR = sum[W][i] - sum[j + 1][i], cR = (W - (j + 1)) - wR;
			sumL[j + 1][i] = sumL[j][i] + (S[i][j] == 'w' ? cL : 0);
			sumLR[i + 1][j + 1] =
				sumLR[i][j + 1] + sumLR[i + 1][j] - sumLR[i][j] +
				(S[i][j] == 'w' ? (ll)wL * cR + (ll)cL * wR : 0);
		}
	}

	ll query(int yL, int xL, int yR, int xR) const {
		int W = myW;
		ll total = 0;
		total += sumLR[yR][xR];
		total -= sumLR[yR][xL];
		total -= sumLR[yL][xR];
		total += sumLR[yL][xL];
		reu(i, yL, yR) {
			int n = xR - xL;
			int w = sum[xR][i] - sum[xL][i], c = n - w;
			int wL = sum[xL][i], cL = xL - wL;
			int wR = sum[W][i] - sum[xR][i], cR = (W - xR) - wR;

			ll ww = calc(w), cc = calc(c);
			ll cw = (sumL[xR][i] - sumL[xL][i]) - (ll)w * cL;
			ll wc = calc(n) - ww - cc - cw;

			total -= (ll)cL * w * wR + (ll)wL * w * cR;
			total -= cw * wR + wL * wc;
			total -= cL * ww + ww * cR;
		}
		return total;
	}

private:
	static ll calc(int n) {
		return (ll)n * (n - 1) / 2;
	}

	int myW;
	int sum[501][500];
	ll sumL[501][500];
	ll sumLR[501][501];
};

int main() {
	int H; int W;
	while(~scanf("%d%d", &H, &W)) {
		vector<vector<char>> S(H, vector<char>(W)), transS(W, vector<char>(H));
		rep(i, H) {
			char buf[501];
			scanf("%s", buf);
			rep(j, W)
				transS[j][i] = S[i][j] = buf[j];
		}
		SolveForHorizontalPatterns solverH, solverV;
		solverH.init(S);
		solverV.init(transS);
		int Q;
		scanf("%d", &Q);
		rep(i, Q) {
			int yL; int xL; int yR; int xR;
			scanf("%d%d%d%d", &yL, &xL, &yR, &xR), -- yL, -- xL;
			ll ans = 0;
			ans += solverH.query(yL, xL, yR, xR);
			ans += solverV.query(xL, yL, xR, yR);
			printf("%lld\n", ans);
		}
	}
	return 0;
}
0