結果

問題 No.655 E869120 and Good Triangles
ユーザー te-shte-sh
提出日時 2018-04-18 16:08:13
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,118 ms / 2,500 ms
コード長 2,734 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 96,552 KB
実行使用メモリ 301,536 KB
最終ジャッジ日時 2023-09-03 19:48:22
合計ジャッジ時間 41,753 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2,053 ms
301,356 KB
testcase_11 AC 2,060 ms
301,408 KB
testcase_12 AC 2,067 ms
301,480 KB
testcase_13 AC 2,072 ms
301,332 KB
testcase_14 AC 2,074 ms
301,420 KB
testcase_15 AC 2,101 ms
301,424 KB
testcase_16 AC 2,105 ms
301,432 KB
testcase_17 AC 2,085 ms
301,408 KB
testcase_18 AC 2,098 ms
301,536 KB
testcase_19 AC 2,087 ms
301,412 KB
testcase_20 AC 2,118 ms
301,344 KB
testcase_21 AC 2,084 ms
301,476 KB
testcase_22 AC 2,066 ms
301,420 KB
testcase_23 AC 2,090 ms
301,424 KB
testcase_24 AC 1,598 ms
301,404 KB
testcase_25 AC 1,514 ms
301,416 KB
testcase_26 AC 1,485 ms
301,416 KB
testcase_27 AC 1,514 ms
301,360 KB
testcase_28 AC 1,482 ms
301,288 KB
testcase_29 AC 1,509 ms
301,364 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

alias point = Point!int;

void main()
{
  int n, k; long p; readV(n, k, p);
  point[] b; readS(k, b);
  foreach (ref bi; b) { --bi.x; --bi.y; }

  if (n == 1) {
    writeln(0);
    return;
  }

  auto a = kaidan!long(n);
  foreach (i; 0..n) a[i][] = -1;

  auto q = DList!point(b), dp = [point(-1, -1), point(-1, 0), point(0, -1), point(0, 1), point(1, 0), point(1, 1)];
  foreach (ref bi; b) a[bi.x][bi.y] = 0;
  while (!q.empty) {
    auto pp = q.front; q.removeFront;
    foreach (dpi; dp) {
      auto np = pp + dpi;
      if (np.x < 0 || np.x >= n || np.y < 0 || np.y > np.x || a[np.x][np.y] >= 0) continue;
      a[np.x][np.y] = a[pp.x][pp.y] + 1;
      q.insertBack(np);
    }
  }

  auto as = kaidan!long(n+1);
  foreach_reverse (i; 0..n)
    foreach (j; 0..i+1) {
      as[i][j] = as[i+1][j] + as[i+1][j+1] + a[i][j];
      if (i < n-1) as[i][j] -= as[i+2][j+1];
    }

  auto bs = kaidan2!long(n+1);
  foreach_reverse (i; 0..n)
    foreach (j; 0..i+1)
      bs[i][j+1] = bs[i][j]+bs[i+1][j+1]-bs[i+1][j]+a[i][j];

  auto calc(int i, int j, int e)
  {
    if (e == 0) return a[i][j];
    else return as[i][j]-as[i+e+1][j+e+1]-bs[i+e+1][j+e+1]+bs[i+e+1][j];
  }

  auto e = kaidan!int(n);
  foreach (j; 0..n) e[n-1][j] = as[n-1][j] >= p ? 0 : 1;

  foreach_reverse (i; 0..n-1)
    foreach (j; 0..i+1) {
      auto r = min(e[i+1][j], e[i+1][j+1]);
      while (r >= 0 && calc(i, j, r) >= p) --r;
      e[i][j] = r+1;
    }

  auto ans = 0L;
  foreach (i; 0..n)
    foreach (j; 0..i+1)
      ans += n-(e[i][j]+i);

  writeln(ans);
}

auto kaidan(T)(int n)
{
  auto a = new T[][](n);
  foreach (i; 0..n) a[i] = new T[](i+1);
  return a;
}

auto kaidan2(T)(int n)
{
  auto a = new T[][](n);
  foreach (i; 0..n) a[i] = new T[](i+2);
  return a;
}

struct Point(T)
{
  T x, y;
  pure auto opBinary(string op)(Point!T r) if (op == "+" || op == "-") { return mixin("Point!T(x"~op~"r.x,y"~op~"r.y)"); }
  pure auto opBinary(string op)(T a) if (op == "*" || op == "/") { return mixin("Point!T(x"~op~"a,y"~op~"a)"); }
  auto opOpAssign(string op)(Point!T r) if (op == "+" || op == "-") { mixin("x"~op~"=r.x; y"~op~"=r.y;"); return this; }
  auto opOpAssign(string op)(T a) if (op == "*" || op == "/") { mixin("x"~op~"=a; y"~op~"=a;"); return this; }
  pure auto opBinary(string op: "*")(Point!T r) { return x*r.x + y*r.y; }
  pure auto hypot2() const { return x^^2 + y^^2; }
}
0