結果

問題 No.855 ヘビの日光浴
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-09-03 19:32:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 177 ms / 3,153 ms
コード長 8,646 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 130,888 KB
実行使用メモリ 21,436 KB
最終ジャッジ日時 2024-06-22 02:27:23
合計ジャッジ時間 7,142 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 1 ms
6,944 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 1 ms
6,944 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 2 ms
6,944 KB
testcase_46 AC 2 ms
6,940 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 1 ms
6,944 KB
testcase_53 AC 1 ms
6,944 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 1 ms
6,940 KB
testcase_57 AC 1 ms
6,940 KB
testcase_58 AC 1 ms
6,940 KB
testcase_59 AC 1 ms
6,940 KB
testcase_60 AC 1 ms
6,940 KB
testcase_61 AC 1 ms
6,940 KB
testcase_62 AC 1 ms
6,944 KB
testcase_63 AC 1 ms
6,944 KB
testcase_64 AC 1 ms
6,940 KB
testcase_65 AC 1 ms
6,940 KB
testcase_66 AC 1 ms
6,940 KB
testcase_67 AC 1 ms
6,940 KB
testcase_68 AC 1 ms
6,944 KB
testcase_69 AC 1 ms
6,940 KB
testcase_70 AC 1 ms
6,944 KB
testcase_71 AC 1 ms
6,944 KB
testcase_72 AC 1 ms
6,944 KB
testcase_73 AC 57 ms
7,812 KB
testcase_74 AC 57 ms
7,792 KB
testcase_75 AC 33 ms
6,944 KB
testcase_76 AC 118 ms
12,944 KB
testcase_77 AC 87 ms
9,048 KB
testcase_78 AC 133 ms
13,604 KB
testcase_79 AC 7 ms
6,940 KB
testcase_80 AC 9 ms
6,944 KB
testcase_81 AC 154 ms
13,724 KB
testcase_82 AC 156 ms
13,880 KB
testcase_83 AC 167 ms
13,956 KB
testcase_84 AC 173 ms
20,420 KB
testcase_85 AC 166 ms
13,896 KB
testcase_86 AC 165 ms
13,988 KB
testcase_87 AC 167 ms
13,924 KB
testcase_88 AC 168 ms
21,436 KB
testcase_89 AC 166 ms
14,048 KB
testcase_90 AC 177 ms
14,056 KB
testcase_91 AC 177 ms
14,060 KB
testcase_92 AC 171 ms
13,884 KB
testcase_93 AC 1 ms
6,944 KB
testcase_94 AC 92 ms
13,964 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)); }


// point update, range product
//   T: monoid
class SegmentTree(T, alias op, T ide) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  int n;
  T[] ts;
  this(int n_) {
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = ide;
  }
  this(int n_, T[] ini) {
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = ide;
    ts[n .. n + n_] = ini[];
    ts[n + n_ .. n << 1] = ide;
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }

  // 0 <= a < n
  T get(int a) const {
    return ts[a + n];
  }
  void update(int a, in T val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, in T val) {
    update(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, in T val) {
    update(a, opFun(ts[a + n], val));
  }

  // prod of [a, b) (0 <= a <= b <= n)
  T rangeProd(int a, int b) const {
    T prodL = ide, prodR = ide;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }

  // min b s.t. pred(prod of [a, b)) (or n + 1 if no such b)
  //   0 <= a <= n
  //   assume pred(prod of [a, b)) is non-decreasing in b
  int binarySearchR(int a, bool delegate(T) pred) const {
    if (pred(ide)) return a;
    if (a == n) return n + 1;
    T prod = ide;
    for (a += n; ; a >>= 1) {
      if (a & 1) {
        if (pred(opFun(prod, ts[a]))) {
          for (; a < n; ) {
            a <<= 1;
            if (!pred(opFun(prod, ts[a]))) {
              prod = opFun(prod, ts[a++]);
            }
          }
          return a - n + 1;
        }
        prod = opFun(prod, ts[a++]);
        if (!(a & a - 1)) return n + 1;
      }
    }
  }

  // max a s.t. pred(prod of [a, b)) (or -1 if no such a)
  //   0 <= b <= n
  //   assume pred(prod of [a, b)) is non-increasing in a
  int binarySearchL(int b, bool delegate(T) pred) const {
    if (pred(ide)) return b;
    if (b == 0) return -1;
    T prod = ide;
    for (b += n; ; b >>= 1) {
      if ((b & 1) || b == 2) {
        if (pred(opFun(prod, ts[b - 1]))) {
          for (; b <= n; ) {
            b <<= 1;
            if (!pred(opFun(prod, ts[b - 1]))) {
              prod = opFun(prod, ts[--b]);
            }
          }
          return b - n - 1;
        }
        prod = opFun(prod, ts[--b]);
        if (!(b & b - 1)) return -1;
      }
    }
  }
}


long M, N;
int Q;
long[] X, Y, L;

void main() {
  try {
    for (; ; ) {
      N = readLong();
      M = readLong();
      Q = readInt();
      X = new long[Q];
      Y = new long[Q];
      L = new long[Q];
      foreach (q; 0 .. Q) {
        X[q] = readLong();
        Y[q] = readLong();
        L[q] = readLong();
      }
      
      auto xs = X.dup ~ [0, M + 1];
      auto ys = Y.dup ~ [0, N + 1];
      xs = xs.sort.uniq.array;
      ys = ys.sort.uniq.array;
      debug {
        writeln("xs = ", xs);
        writeln("ys = ", ys);
      }
      const xsLen = cast(int)(xs.length);
      const ysLen = cast(int)(ys.length);
      
      alias SegTree = SegmentTree!(long, max, 0);
      auto segL = new SegTree(xsLen);
      auto segR = new SegTree(xsLen);
      auto segU = new SegTree(ysLen);
      auto segD = new SegTree(ysLen);
      foreach (q; 0 .. Q) {
        const e = xs.lowerBound(X[q]);
        const f = ys.lowerBound(Y[q]);
        if (f == 0) {
          debug {
            writeln("L ", e, " ", L[q]);
          }
          const l = segL.get(e) + L[q];
          const fU = segU.binarySearchR(0, k => (k >= xs[e])) - 1;
          const fD = segD.binarySearchR(0, k => (k >= M + 1 - xs[e])) - 1;
          if (fU < fD && fU < ysLen - 1 && ys[fU] <= l) {
            debug {
              writeln("  hit with U ", fU);
            }
            segL.update(e, 0);
            segU.update(fU, 0);
          } else if (fD < fU && fD < ysLen - 1 && ys[fD] <= l) {
            debug {
              writeln("  hit with D ", fD);
            }
            segL.update(e, 0);
            segD.update(fD, 0);
          } else if (l + segR.get(e) >= N + 1) {
            debug {
              writeln("  frontal");
            }
            segL.update(e, 0);
            segR.update(e, 0);
          } else {
            segL.update(e, l);
          }
        } else if (f == ysLen - 1) {
          debug {
            writeln("R ", e, " ", L[q]);
          }
          const l = segR.get(e) + L[q];
          const fU = segU.binarySearchL(ysLen, k => (k >= xs[e]));
          const fD = segD.binarySearchL(ysLen, k => (k >= M + 1 - xs[e]));
          if (fU > fD && fU > 0 && ys[fU] >= N + 1 - l) {
            debug {
              writeln("  hit with U ", fU);
            }
            segR.update(e, 0);
            segU.update(fU, 0);
          } else if (fD > fU && fD > 0 && ys[fD] >= N + 1 - l) {
            debug {
              writeln("  hit with D ", fD);
            }
            segR.update(e, 0);
            segD.update(fD, 0);
          } else if (l + segL.get(e) >= N + 1) {
            debug {
              writeln("  frontal");
            }
            segR.update(e, 0);
            segL.update(e, 0);
          } else {
            segR.update(e, l);
          }
        } else if (e == 0) {
          debug {
            writeln("U ", f, " ", L[q]);
          }
          const l = segU.get(f) + L[q];
          const eL = segL.binarySearchR(0, k => (k >= ys[f])) - 1;
          const eR = segR.binarySearchR(0, k => (k >= N + 1 - ys[f])) - 1;
          if (eL < eR && eL < xsLen - 1 && xs[eL] <= l) {
            debug {
              writeln("  hit with L ", eL);
            }
            segU.update(f, 0);
            segL.update(eL, 0);
          } else if (eR < eL && eR < xsLen - 1 && xs[eR] <= l) {
            debug {
              writeln("  hit with R ", eR);
            }
            segU.update(f, 0);
            segR.update(eR, 0);
          } else if (l + segD.get(f) >= M + 1) {
            debug {
              writeln("  frontal");
            }
            segU.update(f, 0);
            segD.update(f, 0);
          } else {
            segU.update(f, l);
          }
        } else if (e == xsLen - 1) {
          debug {
            writeln("D ", f, " ", L[q]);
          }
          const l = segD.get(f) + L[q];
          const eL = segL.binarySearchL(xsLen, k => (k >= ys[f]));
          const eR = segR.binarySearchL(xsLen, k => (k >= N + 1 - ys[f]));
          if (eL > eR && eL > 0 && xs[eL] >= M + 1 - l) {
            debug {
              writeln("  hit with L ", eL);
            }
            segD.update(f, 0);
            segL.update(eL, 0);
          } else if (eR > eL && eR > 0 && xs[eR] >= M + 1 - l) {
            debug {
              writeln("  hit with R ", eR);
            }
            segD.update(f, 0);
            segR.update(eR, 0);
          } else if (l + segU.get(f) >= M + 1) {
            debug {
              writeln("  frontal");
            }
            segD.update(f, 0);
            segU.update(f, 0);
          } else {
            segD.update(f, l);
          }
        } else {
          assert(false);
        }
      }
      
      long ans;
      foreach (e; 1 .. xsLen - 1) {
        ans += segL.get(e);
        ans += segR.get(e);
      }
      foreach (f; 1 .. ysLen - 1) {
        ans += segU.get(f);
        ans += segD.get(f);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0