結果

問題 No.440 2次元チワワ問題
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-07 14:35:13
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 829 ms / 5,000 ms
コード長 3,807 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 110,576 KB
実行使用メモリ 70,892 KB
最終ジャッジ日時 2023-09-04 01:36:06
合計ジャッジ時間 9,419 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 44 ms
12,112 KB
testcase_08 AC 112 ms
11,316 KB
testcase_09 AC 241 ms
30,468 KB
testcase_10 AC 44 ms
11,124 KB
testcase_11 AC 34 ms
5,428 KB
testcase_12 AC 78 ms
28,420 KB
testcase_13 AC 265 ms
49,272 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 265 ms
53,164 KB
testcase_16 AC 15 ms
4,380 KB
testcase_17 AC 416 ms
70,316 KB
testcase_18 AC 410 ms
70,664 KB
testcase_19 AC 413 ms
70,592 KB
testcase_20 AC 410 ms
70,796 KB
testcase_21 AC 829 ms
69,884 KB
testcase_22 AC 825 ms
70,036 KB
testcase_23 AC 821 ms
70,892 KB
testcase_24 AC 824 ms
70,788 KB
testcase_25 AC 798 ms
70,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int H, W;
string[] S;
int Q;
int[] A, B, C, D;

void main() {
  try {
    for (; ; ) {
      H = readInt();
      W = readInt();
      S = new string[H];
      foreach (x; 0 .. H) {
        S[x] = readToken();
      }
      Q = readInt();
      A = new int[Q];
      B = new int[Q];
      C = new int[Q];
      D = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        C[q] = readInt();
        D[q] = readInt();
      }
      
      auto ans = new long[Q];
      foreach (dir; 0 .. 2) {
        auto hor = new long[][][](H, W + 1, 3 + 1);
        foreach (x; 0 .. H) {
          hor[x][0][0] = 1;
          foreach (y; 0 .. W) {
            hor[x][y + 1][] = hor[x][y][];
            foreach (k; 0 .. 3) {
              if (S[x][y] == "cww"[k]) {
                hor[x][y + 1][k + 1] += hor[x][y][k];
              }
            }
          }
        }
        auto ver = new long[][][](H + 1, W, 3 + 1);
        foreach (y; 0 .. W) {
          ver[0][y][0] = 1;
          foreach (x; 0 .. H) {
            ver[x + 1][y][] = ver[x][y][];
            foreach (k; 0 .. 3) {
              if (S[x][y] == "cww"[k]) {
                ver[x + 1][y][k + 1] += ver[x][y][k];
              }
            }
          }
        }
        debug {
          writeln("hor = ", hor);
          writeln("ver = ", ver);
        }
        foreach (q; 0 .. Q) {
          foreach (x; A[q] .. C[q]) {
            const long w = (D[q] - B[q]) - (hor[x][D[q]][1] - hor[x][B[q]][1]);
            long tmp = hor[x][D[q]][3];
            tmp -= hor[x][B[q]][3];
            tmp -= hor[x][B[q]][2] * w;
            tmp -= hor[x][B[q]][1] * w * (w - 1) / 2;
            debug {
              writeln("x = ", x, ": ", tmp);
            }
            ans[q] += tmp;
          }
          foreach (y; B[q] .. D[q]) {
            const long w = (C[q] - A[q]) - (ver[C[q]][y][1] - ver[A[q]][y][1]);
            long tmp = ver[C[q]][y][3];
            tmp -= ver[A[q]][y][3];
            tmp -= ver[A[q]][y][2] * w;
            tmp -= ver[A[q]][y][1] * w * (w - 1) / 2;
            debug {
              writeln("y = ", y, ": ", tmp);
            }
            ans[q] += tmp;
          }
        }
        
        S.reverse;
        foreach (x; 0 .. H) {
          S[x] = S[x].dup.to!(char[]).reverse.to!string;
        }
        foreach (q; 0 .. Q) {
          swap(A[q], C[q]);
          swap(B[q], D[q]);
          A[q] = H - A[q];
          C[q] = H - C[q];
          B[q] = W - B[q];
          D[q] = W - D[q];
        }
      }
      
      foreach (q; 0 .. Q) {
        writeln(ans[q]);
      }
    }
  } catch (EOFException e) {
  }
}
0