結果

問題 No.440 2次元チワワ問題
ユーザー antaanta
提出日時 2016-10-29 00:22:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 3,167 bytes
コンパイル時間 1,783 ms
コンパイル使用メモリ 174,436 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-05-03 08:27:13
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 8 ms
7,552 KB
testcase_08 AC 20 ms
8,192 KB
testcase_09 AC 41 ms
11,008 KB
testcase_10 AC 9 ms
8,576 KB
testcase_11 AC 12 ms
6,144 KB
testcase_12 AC 12 ms
9,088 KB
testcase_13 AC 43 ms
11,904 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 41 ms
12,416 KB
testcase_16 AC 7 ms
5,376 KB
testcase_17 AC 71 ms
14,080 KB
testcase_18 AC 63 ms
14,080 KB
testcase_19 AC 74 ms
13,952 KB
testcase_20 AC 64 ms
14,080 KB
testcase_21 AC 92 ms
13,952 KB
testcase_22 AC 89 ms
13,952 KB
testcase_23 AC 92 ms
13,952 KB
testcase_24 AC 88 ms
14,080 KB
testcase_25 AC 95 ms
14,080 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; }

template<typename T>
struct RectangeSum {
	vector<vector<T> > sum;
	template<typename U>
	void init(int h, int w, U &a) {
		sum.assign(h + 1, vector<T>(w + 1));
		rer(y, 0, h) rer(x, 0, w) sum[y][x] = (y == 0 || x == 0) ? 0 :
			sum[y - 1][x] + sum[y][x - 1] - sum[y - 1][x - 1] + a[y - 1][x - 1];
	}
	//[l, r), [t, b)
	inline T get(int t, int l, int b, int r) const {
		return sum[b][r] - sum[b][l] - sum[t][r] + sum[t][l];
	}
};

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

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

		//sumL.assign(H, vector<ll>(W + 1));
		//sumLR.assign(H, vector<ll>(W + 1));
		rep(i, H) sumL[i][0] = 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[i][j], cL = j - wL;
			int wR = sum[i][W] - sum[i][j + 1], cR = (W - (j + 1)) - wR;
			sumL[i][j + 1] = sumL[i][j] + (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[i][xR] - sum[i][xL], c = n - w;
			int wL = sum[i][xL], cL = xL - wL;
			int wR = sum[i][W] - sum[i][xR], cR = (W - xR) - wR;

			ll ww = calc(w), cc = calc(c);
			ll cw = (sumL[i][xR] - sumL[i][xL]) - (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[500][501];
	ll sumL[500][501];
	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];
		}
		static 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