結果

問題 No.440 2次元チワワ問題
ユーザー btkbtk
提出日時 2016-04-28 11:34:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 804 ms / 5,000 ms
コード長 3,136 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 174,436 KB
実行使用メモリ 59,052 KB
最終ジャッジ日時 2024-04-19 07:35:04
合計ジャッジ時間 9,527 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 12 ms
12,672 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 6 ms
7,424 KB
testcase_07 AC 35 ms
19,072 KB
testcase_08 AC 181 ms
43,136 KB
testcase_09 AC 279 ms
57,472 KB
testcase_10 AC 82 ms
48,896 KB
testcase_11 AC 25 ms
7,936 KB
testcase_12 AC 59 ms
32,128 KB
testcase_13 AC 203 ms
46,208 KB
testcase_14 AC 7 ms
5,376 KB
testcase_15 AC 196 ms
52,352 KB
testcase_16 AC 19 ms
5,632 KB
testcase_17 AC 278 ms
58,916 KB
testcase_18 AC 304 ms
59,008 KB
testcase_19 AC 291 ms
58,780 KB
testcase_20 AC 302 ms
58,880 KB
testcase_21 AC 786 ms
58,972 KB
testcase_22 AC 804 ms
58,880 KB
testcase_23 AC 758 ms
58,880 KB
testcase_24 AC 782 ms
58,880 KB
testcase_25 AC 776 ms
59,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
* Problem link
* http://yukicoder.me/problems/1095
*/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

struct INIT { INIT() { cin.tie(0); ios_base::sync_with_stdio(false); } }init;
LL   c[4][500 + 2][500 + 2];
LL  cw[4][500 + 2][500 + 2];
LL cww[4][500 + 2][500 + 2];
LL   w[4][500 + 2][500 + 2];
LL  ww[4][500 + 2][500 + 2];
LL wwc[4][500 + 2][500 + 2];
LL  wc[4][500 + 2][500 + 2];

enum DIR{UP,DOWN,LEFT,RIGHT};
int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	/*input part*/
	int R, C;
	cin >> R >> C;

	vector<string> S(R);
	for (auto& it : S)
		cin >> it;

	int Q;
	cin >> Q;

	vector<int> rbg(Q), cbg(Q), red(Q), ced(Q);
	for (int i = 0; i < Q; i++)
		cin >> rbg[i] >> cbg[i] >> red[i] >> ced[i];


	/*DP part*/
	auto update = [&](int dir,int isC, int ni, int nj, int pi, int  pj) {
		int isW = 1 - isC;
		  c[dir][ni][nj] =   c[dir][pi][pj] + isC;
		 cw[dir][ni][nj] =  cw[dir][pi][pj] + isW *  c[dir][pi][pj];
		cww[dir][ni][nj] = cww[dir][pi][pj] + isW * cw[dir][pi][pj];
		  w[dir][ni][nj] =   w[dir][pi][pj] + isW;
		 ww[dir][ni][nj] =  ww[dir][pi][pj] + isW *  w[dir][pi][pj];
		wwc[dir][ni][nj] = wwc[dir][pi][pj] + isC * ww[dir][pi][pj];
		 wc[dir][ni][nj] =  wc[dir][pi][pj] + isC *  w[dir][pi][pj];
	};
	for (int i = 1; i <= R; i++)
		for (int j = 1; j <= C; j++){
			update(RIGHT, S[i - 1][j - 1] == 'c', i, j, i, j - 1);
			update(DOWN , S[i - 1][j - 1] == 'c', i, j, i - 1, j);
			update(LEFT , S[i - 1][C - j] == 'c', i, C - j + 1, i, C - j + 2);
			update(UP   , S[R - i][j - 1] == 'c', R - i + 1, j, R - i + 2, j);
		}

	/* 1---s|t---u|v---C*/
	auto sub1=[&](int row,int s,int t,int u,int v,int d,int rd) {
		LL _c = c[d][row][u] - c[d][row][s];
		LL _cw = wc[rd][row][t] - _c * w[rd][row][v] - wc[rd][row][v];
		LL ans = 0;
		ans += c[d][row][s] * ww[rd][row][t];
		ans += cw[d][row][s] * w[rd][row][t];
		ans += _c * ww[rd][row][v];
		ans += _cw * w[rd][row][v];
		ans += cww[d][row][s];
		ans += wwc[rd][row][v];
		return ans;
	};
	auto sub2 = [&](int column, int s, int t, int u, int v, int d, int rd) {
		LL _c = c[d][u][column] - c[d][s][column];
		LL _cw = wc[rd][t][column] - _c * w[rd][v][column] - wc[rd][v][column];
		LL ans = 0;
		ans += c[d][s][column] * ww[rd][t][column];
		ans += cw[d][s][column] * w[rd][t][column];
		ans += _c * ww[rd][v][column];
		ans += _cw * w[rd][v][column];
		ans += cww[d][s][column];
		ans += wwc[rd][v][column];
		return ans;
	};

	/*output part*/
	for (int q = 0; q < Q; q++){
		LL res = 0;
		for (int i = rbg[q]; i <= red[q]; i++){
			/*right*/
			res += cww[RIGHT][i][C] - sub1(i, cbg[q] - 1, cbg[q], ced[q], ced[q] + 1, RIGHT, LEFT);
			/*left*/
			res += cww[LEFT][i][1]  - sub1(i, ced[q] + 1, ced[q], cbg[q], cbg[q] - 1, LEFT, RIGHT);
		}
		for (int i = cbg[q]; i <= ced[q]; i++) {
			/*down*/
			res += cww[DOWN][R][i] - sub2(i, rbg[q] - 1, rbg[q], red[q], red[q] + 1, DOWN, UP);
			/*up*/
			res += cww[UP][1][i] - sub2(i, red[q] + 1, red[q], rbg[q], rbg[q] - 1, UP, DOWN);
		}
		cout << res << endl;
	}
	return 0;
}
0