結果

問題 No.440 2次元チワワ問題
ユーザー te-shte-sh
提出日時 2017-12-13 17:22:26
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 906 ms / 5,000 ms
コード長 3,281 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 110,080 KB
実行使用メモリ 25,856 KB
最終ジャッジ日時 2024-06-12 23:03:08
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 28 ms
7,168 KB
testcase_08 AC 78 ms
7,424 KB
testcase_09 AC 160 ms
13,824 KB
testcase_10 AC 22 ms
6,400 KB
testcase_11 AC 23 ms
5,376 KB
testcase_12 AC 39 ms
10,752 KB
testcase_13 AC 192 ms
21,248 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 174 ms
22,276 KB
testcase_16 AC 10 ms
5,376 KB
testcase_17 AC 292 ms
25,856 KB
testcase_18 AC 282 ms
25,728 KB
testcase_19 AC 298 ms
25,728 KB
testcase_20 AC 284 ms
25,728 KB
testcase_21 AC 890 ms
25,600 KB
testcase_22 AC 901 ms
25,600 KB
testcase_23 AC 891 ms
25,600 KB
testcase_24 AC 896 ms
25,600 KB
testcase_25 AC 906 ms
25,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split.to!(int[]), h = rd[0], w = rd[1];
  auto s = new string[](h);
  foreach (i; 0..h) s[i] = readln.chomp;

  auto uc = new int[][](h, w), uw = new int[][](h, w);
  foreach (i; 0..h)
    foreach (j; 0..w) {
      uc[i][j] = s[i][j] == 'c';
      uw[i][j] = s[i][j] == 'w';
    }

  auto ch = uc.toCs, wh = uw.toCs;
  auto cv = uc.transposed.map!(a => a.array).array.toCs;
  auto wv = uw.transposed.map!(a => a.array).array.toCs;

  auto ucwr = new int[][](h, w), ucwl = new int[][](h, w);
  foreach (i; 0..h)
    foreach (j; 0..w)
      if (s[i][j] == 'c') {
        ucwr[i][j] = wh[i][j..$];
        ucwl[i][j] = wh[i][0..j];
      }
  auto cwr = ucwr.toCs, cwl = ucwl.toCs;

  auto ucwb = new int[][](w, h), ucwu = new int[][](w, h);
  foreach (i; 0..w)
    foreach (j; 0..h)
      if (s[j][i] == 'c') {
        ucwb[i][j] = wv[i][j..$];
        ucwu[i][j] = wv[i][0..j];
      }
  auto cwb = ucwb.toCs, cwu = ucwu.toCs;

  auto ucwwr = new int[][](h, w), ucwwl = new int[][](h, w);
  foreach (i; 0..h)
    foreach (j; 0..w)
      if (s[i][j] == 'c') {
        ucwwr[i][j] = wh[i][j..$] * (wh[i][j..$] - 1) / 2;
        ucwwl[i][j] = wh[i][0..j] * (wh[i][0..j] - 1) / 2;
      }
  auto cwwr = ucwwr.toCs, cwwl = ucwwl.toCs;
  
  auto ucwwb = new int[][](w, h), ucwwu = new int[][](w, h);
  foreach (i; 0..w)
    foreach (j; 0..h)
      if (s[j][i] == 'c') {
        ucwwb[i][j] = wv[i][j..$] * (wv[i][j..$] - 1) / 2;
        ucwwu[i][j] = wv[i][0..j] * (wv[i][0..j] - 1) / 2;
      }
  auto cwwb = ucwwb.toCs, cwwu = ucwwu.toCs;

  auto q = readln.chomp.to!int;
  foreach (_; 0..q) {
    auto rd2 = readln.splitter;
    auto a = rd2.front.to!int-1; rd2.popFront();
    auto b = rd2.front.to!int-1; rd2.popFront();
    auto c = rd2.front.to!int;   rd2.popFront();
    auto d = rd2.front.to!int;

    auto r = 0L;

    foreach (i; a..c) {
      auto ww1 = wh[i][d..$] * (wh[i][d..$] - 1) / 2;
      auto cw1 = cwr[i][b..$] - cwr[i][d..$] - ch[i][b..d] * wh[i][d..$];
      auto cww1 = cwwr[i][b..$] - cwwr[i][d..$] - ch[i][b..d] * ww1 - cw1 * wh[i][d..$];

      auto ww2 = wh[i][0..b] * (wh[i][0..b] - 1) / 2;
      auto cw2 = cwl[i][0..d] - cwl[i][0..b] - ch[i][b..d] * wh[i][0..b];
      auto cww2 = cwwl[i][0..d] - cwwl[i][0..b] - ch[i][b..d] * ww2 - cw2 * wh[i][0..b];

      r += cww1 + cww2;
    }

    foreach (i; b..d) {
      auto ww1 = wv[i][c..$] * (wv[i][c..$] - 1) / 2;
      auto cw1 = cwb[i][a..$] - cwb[i][c..$] - cv[i][a..c] * wv[i][c..$];
      auto cww1 = cwwb[i][a..$] - cwwb[i][c..$] - cv[i][a..c] * ww1 - cw1 * wv[i][c..$];

      auto ww2 = wv[i][0..a] * (wv[i][0..a] - 1) / 2;
      auto cw2 = cwu[i][0..c] - cwu[i][0..a] - cv[i][a..c] * wv[i][0..a];
      auto cww2 = cwwu[i][0..c] - cwwu[i][0..a] - cv[i][a..c] * ww2 - cw2 * wv[i][0..a];

      r += cww1 + cww2;
    }

    writeln(r);
  }
}

auto toCs(T)(T[][] a)
{
  auto r = new Cs!T[](a.length);
  foreach (i, ai; a) r[i] = Cs!T(ai);
  return r;
}

struct Cs(T)
{
  T[] c;

  this(T[] a)
  {
    c = new T[](a.length+1);
    foreach (i, ai; a) c[i+1] = c[i] + ai;
  }

  pure auto opSlice(size_t l, size_t r)
  {
    return c[r] - c[l];
  }

  pure auto opDollar()
  {
    return c.length-1;
  }
}
0