結果

問題 No.440 2次元チワワ問題
ユーザー te-shte-sh
提出日時 2017-12-13 17:18:18
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,270 ms / 5,000 ms
コード長 3,291 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 97,604 KB
実行使用メモリ 49,680 KB
最終ジャッジ日時 2023-09-03 17:31:36
合計ジャッジ時間 12,218 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 32 ms
11,088 KB
testcase_08 AC 93 ms
11,964 KB
testcase_09 AC 246 ms
27,120 KB
testcase_10 AC 30 ms
12,972 KB
testcase_11 AC 30 ms
6,688 KB
testcase_12 AC 61 ms
27,412 KB
testcase_13 AC 324 ms
39,472 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 355 ms
42,012 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 596 ms
49,428 KB
testcase_18 AC 614 ms
48,620 KB
testcase_19 AC 591 ms
49,456 KB
testcase_20 AC 600 ms
48,864 KB
testcase_21 AC 1,153 ms
47,920 KB
testcase_22 AC 1,183 ms
49,192 KB
testcase_23 AC 1,180 ms
47,912 KB
testcase_24 AC 1,270 ms
49,680 KB
testcase_25 AC 1,207 ms
48,212 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