結果

問題 No.440 2次元チワワ問題
ユーザー te-shte-sh
提出日時 2017-12-13 17:18:18
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,270 ms / 5,000 ms
コード長 3,291 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 111,652 KB
実行使用メモリ 48,652 KB
最終ジャッジ日時 2024-06-12 23:02:49
合計ジャッジ時間 10,994 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 30 ms
10,752 KB
testcase_08 AC 88 ms
10,804 KB
testcase_09 AC 177 ms
26,624 KB
testcase_10 AC 28 ms
12,804 KB
testcase_11 AC 25 ms
6,944 KB
testcase_12 AC 51 ms
26,632 KB
testcase_13 AC 242 ms
40,328 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 249 ms
41,800 KB
testcase_16 AC 10 ms
6,944 KB
testcase_17 AC 414 ms
48,176 KB
testcase_18 AC 452 ms
48,060 KB
testcase_19 AC 429 ms
48,052 KB
testcase_20 AC 470 ms
47,560 KB
testcase_21 AC 1,054 ms
48,260 KB
testcase_22 AC 1,242 ms
47,852 KB
testcase_23 AC 1,167 ms
48,652 KB
testcase_24 AC 1,270 ms
48,332 KB
testcase_25 AC 1,052 ms
48,184 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 long[][](h, w), uw = new long[][](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 long[][](h, w), ucwl = new long[][](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 long[][](w, h), ucwu = new long[][](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 long[][](h, w), ucwwl = new long[][](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 long[][](w, h), ucwwu = new long[][](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