結果

問題 No.655 E869120 and Good Triangles
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-02 17:14:40
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,584 ms / 2,500 ms
コード長 3,664 bytes
コンパイル時間 1,332 ms
コンパイル使用メモリ 106,660 KB
実行使用メモリ 325,664 KB
最終ジャッジ日時 2023-09-04 01:00:51
合計ジャッジ時間 30,334 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1,584 ms
324,360 KB
testcase_11 AC 1,515 ms
324,808 KB
testcase_12 AC 1,525 ms
324,840 KB
testcase_13 AC 1,528 ms
324,600 KB
testcase_14 AC 1,520 ms
324,292 KB
testcase_15 AC 1,541 ms
325,664 KB
testcase_16 AC 1,520 ms
324,888 KB
testcase_17 AC 1,547 ms
325,620 KB
testcase_18 AC 1,574 ms
324,016 KB
testcase_19 AC 1,516 ms
325,316 KB
testcase_20 AC 1,517 ms
324,984 KB
testcase_21 AC 1,502 ms
325,052 KB
testcase_22 AC 1,533 ms
324,088 KB
testcase_23 AC 1,569 ms
324,320 KB
testcase_24 AC 1,206 ms
324,080 KB
testcase_25 AC 1,119 ms
323,520 KB
testcase_26 AC 1,094 ms
323,544 KB
testcase_27 AC 1,100 ms
324,276 KB
testcase_28 AC 1,170 ms
323,744 KB
testcase_29 AC 1,048 ms
323,564 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 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)); }


/*
        00      
      10  01    
    20  11  02  
  30  21  12  03
*/

int N, K;
long P;
int[] X, Y;

int[][] D;

void bfs() {
  auto q = new int[N * (N + 1)];
  int qHead, qTail;
  D = new int[][](N, N);
  foreach (x; 0 .. N) foreach (y; 0 .. N) {
    if (x + y < N) {
      D[x][y] = 2 * N;
    }
  }
  foreach (k; 0 .. K) {
    D[X[k]][Y[k]] = 0;
    q[qTail++] = X[k];
    q[qTail++] = Y[k];
  }
  for (; qHead != qTail; ) {
    const x = q[qHead++];
    const y = q[qHead++];
    void update(int xx, int yy) {
      if (D[xx][yy] > D[x][y] + 1) {
        D[xx][yy] = D[x][y] + 1;
        q[qTail++] = xx;
        q[qTail++] = yy;
      }
    }
    if (x + y + 1 < N) {
      update(x + 1, y);
      update(x, y + 1);
    }
    if (x - 1 >= 0) {
      update(x - 1, y + 1);
      update(x - 1, y);
    }
    if (y - 1 >= 0) {
      update(x, y - 1);
      update(x + 1, y - 1);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      P = readLong();
      X = new int[K];
      Y = new int[K];
      foreach (k; 0 .. K) {
        X[k] = readInt() - 1;
        Y[k] = readInt() - 1;
        X[k] -= Y[k];
      }
      
      bfs();
      
      // D[x][0] + D[x][1] + ...
      auto sumX = new long[][](N, N + 1);
      foreach (x; 0 .. N) {
        foreach (y; 0 .. N - x) {
          sumX[x][y + 1] = sumX[x][y] + D[x][y];
        }
      }
      // D[0][y] + D[1][y] + ...
      auto sumY = new long[][](N, N + 1);
      foreach (y; 0 .. N) {
        foreach (x; 0 .. N - y) {
          sumY[y][x + 1] = sumY[y][x] + D[x][y];
        }
      }
      
      debug {
        foreach (z; 0 .. N) {
          write(' '.repeat(N - z));
          foreach (y; 0 .. z + 1) {
            write(D[z - y][y], " ");
          }
          writeln();
        }
        writeln("sumX = ", sumX);
        writeln("sumY = ", sumY);
      }
      
      long ans;
      foreach (z; 0 .. N) {
        long now;
        for (int y0 = 0, y1 = 0; y0 <= z; ++y0) {
          // D[z - y1][y0] + ... + D[z - y1][y1]
          for (; y1 <= z && (y1 < y0 || now + (sumX[z - y1][y1 + 1] - sumX[z - y1][y0]) < P); ++y1) {
            now += (sumX[z - y1][y1 + 1] - sumX[z - y1][y0]);
          }
          debug {
            writeln(z, " ", y0, " ", y1);
          }
          ans += z + 1 - y1;
          // D[z - (y1 - 1)][y0] + ... + D[z - y0][y0]
          now -= (sumY[y0][z - y0 + 1] - sumY[y0][z - (y1 - 1)]);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0