結果

問題 No.960 マンハッタン距離3
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-25 18:38:55
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 7,751 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 117,624 KB
実行使用メモリ 19,564 KB
最終ジャッジ日時 2023-09-04 04:31:44
合計ジャッジ時間 14,031 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 1 ms
4,376 KB
testcase_64 AC 1 ms
4,380 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 1 ms
4,380 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 1 ms
4,376 KB
testcase_69 AC 1 ms
4,380 KB
testcase_70 AC 2 ms
4,380 KB
testcase_71 AC 1 ms
4,376 KB
testcase_72 AC 2 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 1 ms
4,376 KB
testcase_75 AC 1 ms
4,376 KB
testcase_76 AC 2 ms
4,376 KB
testcase_77 AC 2 ms
4,380 KB
testcase_78 AC 2 ms
4,380 KB
testcase_79 AC 2 ms
4,376 KB
testcase_80 AC 1 ms
4,380 KB
testcase_81 AC 1 ms
4,376 KB
testcase_82 AC 1 ms
4,376 KB
testcase_83 AC 1 ms
4,380 KB
testcase_84 AC 1 ms
4,376 KB
testcase_85 AC 2 ms
4,380 KB
testcase_86 AC 1 ms
4,380 KB
testcase_87 AC 1 ms
4,376 KB
testcase_88 AC 2 ms
4,380 KB
testcase_89 AC 1 ms
4,376 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 2 ms
4,376 KB
testcase_92 AC 1 ms
4,380 KB
testcase_93 AC 2 ms
4,380 KB
testcase_94 AC 1 ms
4,376 KB
testcase_95 AC 2 ms
4,380 KB
testcase_96 AC 2 ms
4,376 KB
testcase_97 AC 2 ms
4,376 KB
testcase_98 AC 1 ms
4,380 KB
testcase_99 AC 1 ms
4,380 KB
testcase_100 AC 1 ms
4,376 KB
testcase_101 AC 2 ms
4,376 KB
testcase_102 AC 2 ms
4,376 KB
testcase_103 AC 1 ms
4,380 KB
testcase_104 AC 1 ms
4,380 KB
testcase_105 AC 2 ms
4,376 KB
testcase_106 AC 1 ms
4,380 KB
testcase_107 AC 1 ms
4,376 KB
testcase_108 AC 2 ms
4,376 KB
testcase_109 AC 1 ms
4,376 KB
testcase_110 AC 1 ms
4,380 KB
testcase_111 AC 1 ms
4,380 KB
testcase_112 AC 1 ms
4,376 KB
testcase_113 AC 1 ms
4,376 KB
testcase_114 AC 1 ms
4,380 KB
testcase_115 AC 2 ms
4,376 KB
testcase_116 AC 1 ms
4,380 KB
testcase_117 AC 2 ms
4,380 KB
testcase_118 AC 2 ms
4,376 KB
testcase_119 AC 1 ms
4,376 KB
testcase_120 AC 1 ms
4,380 KB
testcase_121 AC 2 ms
4,380 KB
testcase_122 AC 1 ms
4,380 KB
testcase_123 AC 2 ms
4,380 KB
testcase_124 AC 1 ms
4,376 KB
testcase_125 AC 1 ms
4,376 KB
testcase_126 AC 2 ms
4,380 KB
testcase_127 AC 1 ms
4,376 KB
testcase_128 AC 28 ms
6,236 KB
testcase_129 AC 77 ms
8,852 KB
testcase_130 AC 52 ms
7,256 KB
testcase_131 AC 93 ms
16,340 KB
testcase_132 AC 93 ms
11,172 KB
testcase_133 AC 3 ms
4,380 KB
testcase_134 AC 25 ms
6,200 KB
testcase_135 AC 31 ms
5,988 KB
testcase_136 AC 31 ms
5,724 KB
testcase_137 AC 47 ms
6,820 KB
testcase_138 AC 67 ms
10,880 KB
testcase_139 AC 79 ms
11,004 KB
testcase_140 AC 100 ms
10,356 KB
testcase_141 AC 104 ms
18,700 KB
testcase_142 AC 43 ms
7,072 KB
testcase_143 AC 60 ms
7,536 KB
testcase_144 AC 88 ms
17,116 KB
testcase_145 AC 7 ms
4,380 KB
testcase_146 AC 22 ms
6,004 KB
testcase_147 AC 71 ms
11,916 KB
testcase_148 AC 38 ms
6,252 KB
testcase_149 AC 59 ms
7,584 KB
testcase_150 AC 85 ms
11,092 KB
testcase_151 AC 6 ms
4,380 KB
testcase_152 AC 67 ms
8,048 KB
testcase_153 AC 102 ms
18,720 KB
testcase_154 AC 5 ms
4,380 KB
testcase_155 AC 78 ms
14,452 KB
testcase_156 AC 27 ms
5,244 KB
testcase_157 AC 78 ms
16,236 KB
testcase_158 AC 101 ms
18,636 KB
testcase_159 AC 15 ms
4,672 KB
testcase_160 AC 6 ms
4,380 KB
testcase_161 AC 91 ms
18,404 KB
testcase_162 AC 89 ms
17,308 KB
testcase_163 AC 50 ms
7,852 KB
testcase_164 AC 100 ms
10,876 KB
testcase_165 AC 80 ms
14,852 KB
testcase_166 AC 15 ms
4,668 KB
testcase_167 AC 50 ms
8,092 KB
testcase_168 AC 36 ms
6,216 KB
testcase_169 AC 40 ms
7,328 KB
testcase_170 AC 32 ms
6,512 KB
testcase_171 AC 56 ms
7,024 KB
testcase_172 AC 39 ms
6,488 KB
testcase_173 AC 10 ms
4,384 KB
testcase_174 AC 87 ms
17,152 KB
testcase_175 AC 107 ms
18,724 KB
testcase_176 AC 73 ms
13,864 KB
testcase_177 AC 10 ms
4,528 KB
testcase_178 AC 76 ms
10,448 KB
testcase_179 AC 81 ms
12,712 KB
testcase_180 AC 101 ms
11,116 KB
testcase_181 AC 22 ms
6,712 KB
testcase_182 AC 45 ms
6,780 KB
testcase_183 AC 63 ms
7,776 KB
testcase_184 AC 48 ms
6,776 KB
testcase_185 AC 73 ms
8,568 KB
testcase_186 AC 92 ms
11,136 KB
testcase_187 AC 69 ms
8,104 KB
testcase_188 AC 1 ms
4,376 KB
testcase_189 AC 2 ms
4,380 KB
testcase_190 AC 1 ms
4,376 KB
testcase_191 AC 2 ms
4,380 KB
testcase_192 AC 1 ms
4,380 KB
testcase_193 AC 1 ms
4,380 KB
testcase_194 AC 2 ms
4,376 KB
testcase_195 AC 2 ms
4,380 KB
testcase_196 AC 66 ms
11,460 KB
testcase_197 AC 19 ms
5,984 KB
testcase_198 AC 49 ms
8,080 KB
testcase_199 AC 73 ms
12,232 KB
testcase_200 AC 81 ms
14,952 KB
testcase_201 AC 44 ms
7,304 KB
testcase_202 AC 77 ms
10,360 KB
testcase_203 AC 42 ms
7,988 KB
testcase_204 AC 101 ms
18,524 KB
testcase_205 AC 47 ms
7,588 KB
testcase_206 AC 90 ms
18,392 KB
testcase_207 AC 69 ms
13,904 KB
testcase_208 AC 73 ms
13,992 KB
testcase_209 AC 23 ms
5,688 KB
testcase_210 AC 11 ms
4,472 KB
testcase_211 AC 33 ms
7,248 KB
testcase_212 AC 21 ms
6,448 KB
testcase_213 AC 47 ms
7,316 KB
testcase_214 AC 110 ms
19,564 KB
testcase_215 AC 85 ms
15,792 KB
testcase_216 AC 98 ms
18,108 KB
testcase_217 AC 54 ms
8,580 KB
testcase_218 AC 94 ms
17,832 KB
testcase_219 AC 49 ms
8,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, 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)); }




void main() {
  try {
    for (; ; ) {
      const W = readLong();
      const H = readLong();
      const N = readInt();
      auto X = new long[N];
      auto Y = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong();
        Y[i] = readLong();
      }
      
      auto A = new long[N];
      auto B = new long[N];
      foreach (i; 0 .. N) {
        A[i] = X[i] + Y[i];
        B[i] = -X[i] + Y[i];
      }
      const as = A.dup.sort.uniq.array;
      const bs = B.dup.sort.uniq.array;
      debug {
        writeln("A = ", A);
        writeln("B = ", B);
        writeln("as = ", as);
        writeln("bs = ", bs);
      }
      
      bool check(long ca, long cb, long sz) {
        const cx = (ca - cb) / 2;
        const cy = (ca + cb) / 2;
        if (!(1 <= cx && cx <= W && 1 <= cy && cy <= H)) {
          return false;
        }
        foreach (i; 0 .. N) {
          if (sz != max(abs(A[i] - ca), abs(B[i] - cb))) {
            return false;
          }
        }
        debug {
          writeln("check ", ca, " ", cb, " ", sz, " = true");
        }
        return true;
      }
      
      long ans;
      if (A.map!(a => (a & 1)).array.sort.uniq.array.length == 1 &&
          B.map!(a => (a & 1)).array.sort.uniq.array.length == 1) {
        if (as.length == 1 && bs.length == 1) {
          ans = H * W;
        } if (as.length == 1) {
          /*
                 ##
                 ##
              o
               o
                o
            ##
            ##
          */
          const i0 = X.minIndex;
          const i1 = X.maxIndex;
          ans += (X[i0] - 1 + 1) * (Y[i1] - 1 + 1);
          ans += (W - X[i1] + 1) * (H - Y[i0] + 1);
          if (N == 2) {
            ans += (X[i1] - X[i0] - 1);
          }
        } else if (bs.length == 1) {
          /*
            ##
            ##
                o
               o
              o
                 ##
                 ##
          */
          const i0 = X.minIndex;
          const i1 = X.maxIndex;
          ans += (X[i0] - 1 + 1) * (H - Y[i1] + 1);
          ans += (W - X[i1] + 1) * (Y[i0] - 1 + 1);
          if (N == 2) {
            ans += (X[i1] - X[i0] - 1);
          }
        } else {
          // no as[0], as[$ - 1]
          if (bs.length == 2) {
            const sz = (bs[$ - 1] - bs[0]) / 2;
            const cb = bs[0] + sz;
            // x = 1 or y = 1
            long aL = max(1 + (cb + 1), (1 - cb) + 1);
            // x = W or y = H
            long aU = min(W + (cb + W), (H - cb) + H);
            foreach (i; 0 .. N) {
              chmax(aL, A[i] - sz);
              chmin(aU, A[i] + sz);
            }
            chmax(aL, as[$ - 1] - sz + 2);
            chmin(aU, as[0] + sz - 2);
            ans += max((aU - aL) / 2 + 1, 0);
          }
          // no bs[0], bs[$ - 1]
          if (as.length == 2) {
            const sz = (as[$ - 1] - as[0]) / 2;
            const ca = as[0] + sz;
            // x = W or y = 1
            long bL = max(-W + (ca - W), -(ca - 1) + 1);
            // x = 1 or y = H
            long bU = min(-1 + (ca - 1), -(ca - H) + H);
            foreach (i; 0 .. N) {
              chmax(bL, B[i] - sz);
              chmin(bU, B[i] + sz);
            }
            chmax(bL, bs[$ - 1] - sz + 2);
            chmin(bU, bs[0] + sz - 2);
            ans += max((bU - bL) / 2 + 1, 0);
          }
          // [as[0], *] x [bs[0], *]
          {
            long sz;
            long yL = (as[0] + bs[0]) / 2;
            foreach (i; 0 .. N) {
              if (A[i] == as[0] || B[i] == bs[0]) {
                chmax(yL, Y[i]);
              } else {
                chmax(sz, max(A[i] - as[0], B[i] - bs[0]) / 2);
              }
            }
            if (sz) {
              if (check(as[0] + sz, bs[0] + sz, sz)) ans += 1;
            } else {
              ans += max(H - yL + 1, 0);
            }
          }
          // [as[0], *] x [*, bs[$ - 1]]
          {
            long sz;
            long xL = (as[0] - bs[$ - 1]) / 2;
            foreach (i; 0 .. N) {
              if (A[i] == as[0] || B[i] == bs[$ - 1]) {
                chmax(xL, X[i]);
              } else {
                chmax(sz, max(A[i] - as[0], bs[$ - 1] - B[i]) / 2);
              }
            }
            if (sz) {
              if (check(as[0] + sz, bs[$ - 1] - sz, sz)) ans += 1;
            } else {
              ans += max(W - xL + 1, 0);
            }
          }
          // [as[0], *] x [bs[0], bs[$ - 1]]
          {
            const sz = (bs[$ - 1] - bs[0]) / 2;
            if (check(as[0] + sz, bs[0] + sz, sz)) ans -= 1;
          }
          // [*, as[$ - 1]] x [bs[0], *]
          {
            long sz;
            long xU = (as[$ - 1] - bs[0]) / 2;
            foreach (i; 0 .. N) {
              if (A[i] == as[$ - 1] || B[i] == bs[0]) {
                chmin(xU, X[i]);
              } else {
                chmax(sz, max(as[$ - 1] - A[i], B[i] - bs[0]) / 2);
              }
            }
            if (sz) {
              if (check(as[$ - 1] - sz, bs[0] + sz, sz)) ans += 1;
            } else {
              ans += max(xU - 1 + 1, 0);
            }
          }
          // [*, as[$ - 1]] x [*, bs[$ - 1]]
          {
            long sz;
            long yU = (as[$ - 1] + bs[$ - 1]) / 2;
            foreach (i; 0 .. N) {
              if (A[i] == as[$ - 1] || B[i] == bs[$ - 1]) {
                chmin(yU, Y[i]);
              } else {
                chmax(sz, max(as[$ - 1] - A[i], bs[$ - 1] - B[i]) / 2);
              }
            }
            if (sz) {
              if (check(as[$ - 1] - sz, bs[$ - 1] - sz, sz)) ans += 1;
            } else {
              ans += max(yU - 1 + 1, 0);
            }
          }
          // [*, as[$ - 1]] x [bs[0], bs[$ - 1]]
          {
            const sz = (bs[$ - 1] - bs[0]) / 2;
            if (check(as[$ - 1] - sz, bs[0] + sz, sz)) ans -= 1;
          }
          // [as[0], as[$ - 1]] x [bs[0], *]
          {
            const sz = (as[$ - 1] - as[0]) / 2;
            if (check(as[0] + sz, bs[0] + sz, sz)) ans -= 1;
          }
          // [as[0], as[$ - 1]] x [*, bs[$ - 1]]
          {
            const sz = (as[$ - 1] - as[0]) / 2;
            if (check(as[0] + sz, bs[$ - 1] - sz, sz)) ans -= 1;
          }
          // [as[0], as[$ - 1]] x [bs[0], bs[$ - 1]]
          if (as[$ - 1] - as[0] == bs[$ - 1] - bs[0]) {
            const sz = (as[$ - 1] - as[0]) / 2;
            if (check(as[0] + sz, bs[0] + sz, sz)) ans += 1;
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0