結果

問題 No.440 2次元チワワ問題
ユーザー tnakao0123tnakao0123
提出日時 2016-11-03 19:32:49
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 379 ms / 5,000 ms
コード長 1,860 bytes
コンパイル時間 915 ms
コンパイル使用メモリ 87,384 KB
実行使用メモリ 492,964 KB
最終ジャッジ日時 2024-11-25 02:27:34
合計ジャッジ時間 6,845 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 440.cc: No.440 2次元チワワ問題 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 500;
const int MAX_N = 10000;

/* typedef */

typedef long long ll;

/* global variables */

string brds[MAX_N];
int y0s[MAX_N], x0s[MAX_N], y1s[MAX_N], x1s[MAX_N];
ll ans[MAX_N];
int dp[MAX_H][MAX_H][MAX_H];

/* subroutines */

/* main */

int main() {
  int h, w;
  cin >> h >> w;

  for (int y = 0; y < h; y++) cin >> brds[y];

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> y0s[i] >> x0s[i] >> y1s[i] >> x1s[i];
    y0s[i]--, x0s[i]--, y1s[i]--, x1s[i]--;
  }

  for (int y = 0; y < h; y++)
    for (int x0 = 0; x0 < w; x0++) {
      int cn = 0, wn = 0;
      int ww = 0, cw = 0;
      int cww = 0, wwc = 0;
      for (int x1 = x0; x1 < w; x1++) {
	if (brds[y][x1] == 'c') wwc += ww, cn++;
	else cww += cw, ww += wn, cw += cn, wn++;
	dp[y][x0][x1] = cww + wwc;
      }
    }

  for (int i = 0; i < n; i++)
    for (int y = y0s[i]; y <= y1s[i]; y++)
      ans[i] += dp[y][x0s[i]][x1s[i]];

  for (int x = 0; x < w; x++)
    for (int y0 = 0; y0 < h; y0++) {
      int cn = 0, wn = 0;
      int ww = 0, cw = 0;
      int cww = 0, wwc = 0;
      for (int y1 = y0; y1 < h; y1++) {
	if (brds[y1][x] == 'c') wwc += ww, cn++;
	else cww += cw, ww += wn, cw += cn, wn++;
	dp[x][y0][y1] = cww + wwc;
      }
    }

  for (int i = 0; i < n; i++)
    for (int x = x0s[i]; x <= x1s[i]; x++)
      ans[i] += dp[x][y0s[i]][y1s[i]];

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