結果

問題 No.440 2次元チワワ問題
ユーザー antaanta
提出日時 2016-10-28 23:34:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 5,000 ms
コード長 2,484 bytes
コンパイル時間 2,885 ms
コンパイル使用メモリ 182,864 KB
実行使用メモリ 9,068 KB
最終ジャッジ日時 2023-08-15 21:42:22
合計ジャッジ時間 4,258 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 17 ms
4,376 KB
testcase_09 AC 38 ms
6,136 KB
testcase_10 AC 7 ms
4,412 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 9 ms
4,856 KB
testcase_13 AC 40 ms
6,944 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 37 ms
7,096 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 62 ms
8,964 KB
testcase_18 AC 59 ms
8,792 KB
testcase_19 AC 59 ms
8,932 KB
testcase_20 AC 59 ms
8,792 KB
testcase_21 AC 110 ms
8,928 KB
testcase_22 AC 110 ms
8,876 KB
testcase_23 AC 109 ms
9,068 KB
testcase_24 AC 112 ms
8,876 KB
testcase_25 AC 110 ms
8,932 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; }

struct Query {
	int yL; int xL; int yR; int xR;
};

int main() {
	int H; int W;
	while(~scanf("%d%d", &H, &W)) {
		vector<vector<char>> S(H, vector<char>(W));
		rep(i, H) {
			char buf[501];
			scanf("%s", buf);
			rep(j, W)
				S[i][j] = buf[j];
		}
		auto calc = [](int n) { return (ll)n * (n - 1) / 2; };
		int Q;
		scanf("%d", &Q);
		vector<Query> queries(Q);
		rep(i, Q) {
			int yL; int xL; int yR; int xR;
			scanf("%d%d%d%d", &yL, &xL, &yR, &xR), -- yL, -- xL;
			queries[i] = Query{ yL,xL,yR,xR };
		}
		vector<ll> ans(Q, 0);
		rep(rot, 2) {
			vector<vi> sum(H, vi(W + 1));
			rep(i, H) rep(j, W)
				sum[i][j + 1] = sum[i][j] + (S[i][j] == 'w');

			vector<vector<ll>> sumL(H, vector<ll>(W + 1)), sumLR(H, vector<ll>(W + 1));
			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][j + 1] = sumLR[i][j] + (S[i][j] == 'w' ? (ll)wL * cR + (ll)cL * wR : 0);
			}
			
			rep(qi, Q) {
				Query q = queries[qi];
				ll total = 0;
				reu(i, q.yL, q.yR) {
					int n = q.xR - q.xL;
					int w = sum[i][q.xR] - sum[i][q.xL], c = n - w;
					int wL = sum[i][q.xL], cL = q.xL - wL;
					int wR = sum[i][W] - sum[i][q.xR], cR = (W - q.xR) - wR;
					ll ww = calc(w), cc = calc(c);
					ll cw = (sumL[i][q.xR] - sumL[i][q.xL]) - (ll)w * cL;
					ll wc = calc(n) - ww - cc - cw;

					total += sumLR[i][q.xR] - sumLR[i][q.xL];
					total -= (ll)cL * w * wR + (ll)wL * w * cR;
					total -= cw * wR + wL * wc;
					total -= cL * ww + ww * cR;
				}
				ans[qi] += total;
			}

			vector<vector<char>> nS(W, vector<char>(H));
			rep(i, H) rep(j, W)
				nS[j][i] = S[i][j];
			for(auto &q : queries) {
				swap(q.yL, q.xL);
				swap(q.yR, q.xR);
			}
			swap(H, W);
			S.swap(nS);
		}

		for(int i = 0; i < Q; ++ i)
			printf("%lld\n", ans[i]);
	}
	return 0;
}
0