結果

問題 No.2546 Many Arithmetic Sequences
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-11-24 21:55:24
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 407 ms / 2,000 ms
コード長 4,858 bytes
コンパイル時間 2,830 ms
コンパイル使用メモリ 127,616 KB
実行使用メモリ 24,196 KB
最終ジャッジ日時 2023-11-25 20:45:01
合計ジャッジ時間 11,354 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 148 ms
6,676 KB
testcase_04 AC 287 ms
16,188 KB
testcase_05 AC 233 ms
14,140 KB
testcase_06 AC 50 ms
6,676 KB
testcase_07 AC 111 ms
6,676 KB
testcase_08 AC 38 ms
6,676 KB
testcase_09 AC 106 ms
6,752 KB
testcase_10 AC 199 ms
9,184 KB
testcase_11 AC 211 ms
11,304 KB
testcase_12 AC 94 ms
6,676 KB
testcase_13 AC 198 ms
14,824 KB
testcase_14 AC 207 ms
22,360 KB
testcase_15 AC 62 ms
8,576 KB
testcase_16 AC 193 ms
16,524 KB
testcase_17 AC 264 ms
14,892 KB
testcase_18 AC 149 ms
12,636 KB
testcase_19 AC 173 ms
14,268 KB
testcase_20 AC 107 ms
14,140 KB
testcase_21 AC 129 ms
6,676 KB
testcase_22 AC 210 ms
14,580 KB
testcase_23 AC 389 ms
23,304 KB
testcase_24 AC 407 ms
23,300 KB
testcase_25 AC 395 ms
23,300 KB
testcase_26 AC 392 ms
23,300 KB
testcase_27 AC 393 ms
23,300 KB
testcase_28 AC 333 ms
24,196 KB
testcase_29 AC 332 ms
24,196 KB
testcase_30 AC 334 ms
24,196 KB
testcase_31 AC 332 ms
24,196 KB
testcase_32 AC 335 ms
24,196 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 330 ms
12,116 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 316 ms
12,244 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 1 ms
6,676 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; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

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)); }


// class Func = TX -> TY
//   for any f, g: Func, x -> sgn(g(x) - f(x)) must be monotone
class LiChaoTree(Func) {
  import std.algorithm : swap;
  alias TX = Func.TX;
  alias TY = Func.TY;
  const(TX) L, R;
  class Tree {
    Tree l, r;
    Func f;
    this(Func f) {
      this.f = f;
    }
  }
  Tree root;

  // [L, R)
  this(TX L, TX R) {
    assert(L < R, "LiChaoTree: L < R must hold");
    this.L = L;
    this.R = R;
  }
  // Add g to the whole [L, R)
  void add(Func g) {
    root = add(root, L, R, g);
  }
  // Add g to [a, b)
  void add(TX a, TX b, Func g) {
    root = add(root, L, R, a, b, g);
  }
  // Get max at a
  TY opCall(TX a) {
    TY mx = TY.min;
    TX l = L, r = R;
    for (Tree u = root; u; ) {
      if (u.f) {
        const fX = u.f(a);
        if (mx < fX) mx = fX;
      }
      const mid = l + (r - l) / 2;
      if (a < mid) {
        u = u.l; r = mid;
      } else {
        u = u.r; l = mid;
      }
    }
    return mx;
  }

 private:
  Tree add(Tree u0, TX l, TX r, Func g) {
    if (!u0) return new Tree(g);
    TY gL = g(l), gR = g(r);
    for (Tree u = u0; ; ) {
      TY fL = u.f ? u.f(l) : TY.min, fR = u.f ? u.f(r) : TY.min;
      if (fL >= gL && fR >= gR) return u0;
      if (fL <= gL && fR <= gR) { u.f = g; return u0; }
      if (r - l == 1) { if (fL <= gL) { u.f = g; } return u0; }
      const mid = l + (r - l) / 2;
      TY fMid = u.f(mid), gMid = g(mid);
      if (fMid < gMid) {
        swap(u.f, g);
        if (!g) return u0;
        if (gL < fL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gL = fL; gR = fMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = fMid; gR = fR;
        }
      } else {
        if (fL < gL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gR = gMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = gMid;
        }
      }
    }
  }
  Tree add(Tree u, TX l, TX r, TX a, TX b, Func g) {
    if (b <= l || r <= a) return u;
    if (a <= l && r <= b) return add(u, l, r, g);
    if (u && u.f && u.f(l) >= g(l) && u.f(r) >= g(r)) return u;
    if (!u) u = new Tree(null);
    const mid = l + (r - l) / 2;
    u.l = add(u.l, l, mid, a, b, g);
    u.r = add(u.r, mid, r, a, b, g);
    return u;
  }
}

// y = p x + q
class Line {
  alias TX = long;
  alias TY = long;
  long p, q;
  this(long p, long q) {
    this.p = p;
    this.q = q;
  }
  TY opCall(TX x) {
    return p * x + q;
  }
}


void main() {
  try {
    for (; ; ) {
      const N = readInt;
      const M = readInt;
      auto A = new long[N];
      auto D = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong;
        D[i] = readLong;
      }
      
      auto lct = new LiChaoTree!Line(0, M + 1);
      alias Entry = Tuple!(long, "c", int, "i");
      BinaryHeap!(Array!Entry) que;
      foreach (i; 0 .. N) {
        if (D[i] > 0) {
          lct.add(new Line(D[i], 2 * A[i] - D[i]));
        } else {
          que.insert(Entry(A[i], i));
        }
      }
      
      long ans = long.min;
      long now;
      foreach (m; 0 .. M + 1) {
        const res = (M - m) ? lct(M - m) : 0;
        debug {
          if (M <= 10) {
            writeln(m, ": ", now, " ", res);
          }
        }
        if (res > long.min) {
          chmax(ans, now + res * (M - m) / 2);
        }
        if (m == M) break;
        if (que.empty) break;
        const e = que.front;
        que.removeFront;
        now += e.c;
        que.insert(Entry(e.c + D[e.i], e.i));
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0