結果

問題 No.703 ゴミ拾い Easy
ユーザー 👑 obakyanobakyan
提出日時 2023-02-26 22:07:10
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 490 ms / 1,500 ms
コード長 3,640 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 40,832 KB
最終ジャッジ日時 2024-09-14 10:49:04
合計ジャッジ時間 12,691 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
11,264 KB
testcase_01 AC 19 ms
11,264 KB
testcase_02 AC 19 ms
11,264 KB
testcase_03 AC 19 ms
11,136 KB
testcase_04 AC 20 ms
11,136 KB
testcase_05 AC 19 ms
11,136 KB
testcase_06 AC 19 ms
11,264 KB
testcase_07 AC 19 ms
11,136 KB
testcase_08 AC 19 ms
11,136 KB
testcase_09 AC 19 ms
11,264 KB
testcase_10 AC 19 ms
11,136 KB
testcase_11 AC 19 ms
11,392 KB
testcase_12 AC 19 ms
11,264 KB
testcase_13 AC 19 ms
11,136 KB
testcase_14 AC 29 ms
11,392 KB
testcase_15 AC 27 ms
11,264 KB
testcase_16 AC 27 ms
11,264 KB
testcase_17 AC 28 ms
11,392 KB
testcase_18 AC 26 ms
11,264 KB
testcase_19 AC 24 ms
11,264 KB
testcase_20 AC 25 ms
11,264 KB
testcase_21 AC 24 ms
11,264 KB
testcase_22 AC 30 ms
11,520 KB
testcase_23 AC 25 ms
11,264 KB
testcase_24 AC 417 ms
39,808 KB
testcase_25 AC 416 ms
40,704 KB
testcase_26 AC 375 ms
34,432 KB
testcase_27 AC 413 ms
40,576 KB
testcase_28 AC 423 ms
40,576 KB
testcase_29 AC 490 ms
34,560 KB
testcase_30 AC 401 ms
40,832 KB
testcase_31 AC 454 ms
34,560 KB
testcase_32 AC 408 ms
34,560 KB
testcase_33 AC 413 ms
38,016 KB
testcase_34 AC 242 ms
23,424 KB
testcase_35 AC 237 ms
23,424 KB
testcase_36 AC 243 ms
23,424 KB
testcase_37 AC 245 ms
23,424 KB
testcase_38 AC 249 ms
23,424 KB
testcase_39 AC 243 ms
23,424 KB
testcase_40 AC 238 ms
23,552 KB
testcase_41 AC 238 ms
23,424 KB
testcase_42 AC 239 ms
23,424 KB
testcase_43 AC 244 ms
23,424 KB
testcase_44 AC 18 ms
11,264 KB
testcase_45 AC 17 ms
11,264 KB
testcase_46 AC 264 ms
23,424 KB
testcase_47 AC 264 ms
23,424 KB
testcase_48 AC 27 ms
11,264 KB
testcase_49 AC 31 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local LiChaoTree = {}

LiChaoTree.create = function(self, n, x)
  self.xinv = {}
  for i = 1, n do
    self.xinv[x[i]] = i
  end
  self.x = x
  local stagenum, mul = 1, 1
  self.a, self.b = {{}}, {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.a[stagenum] = {}
    self.b[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, stagenum do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.a[i][j] = false
      self.b[i][j] = false
    end
  end
end

LiChaoTree.lessthan = function(self, x, la, lb, ra, rb)
  if not la then return false end
  if not ra then return true end
  if not x then
    if la ~= ra then
      return la < ra
    else
      return lb < rb
    end
  end
  do return 1LL * la * x + lb < 1LL * ra * x + rb end
  -- return la * x + lb < ra * x + rb
end

LiChaoTree.addLine = function(self, new_a, new_b)
  local x, a, b = self.x, self.a, self.b
  local stagenum = self.stagenum
  local idx = 1
  local xlidx = 1
  for i = 1, stagenum do
    local sz = bls(1, stagenum - i)
    local xridx = xlidx + sz - 1
    local fl = self:lessthan(x[xlidx], new_a, new_b, a[i][idx], b[i][idx])
    local fr = self:lessthan(x[xridx], new_a, new_b, a[i][idx], b[i][idx])
    if fl and fr then
      a[i][idx] = new_a
      b[i][idx] = new_b
      break
    elseif not fl and not fr then
      break
    else
      local xmidx = xlidx + brs(sz, 1)
      local fm = self:lessthan(x[xmidx], new_a, new_b, a[i][idx], b[i][idx])
      if fl then
        if fm then
          new_a, a[i][idx] = a[i][idx], new_a
          new_b, b[i][idx] = b[i][idx], new_b
          xlidx = xmidx
          idx = idx * 2
        else
          idx = idx * 2 - 1
        end
      else
        if fm then
          new_a, a[i][idx] = a[i][idx], new_a
          new_b, b[i][idx] = b[i][idx], new_b
          idx = idx * 2 - 1
        else
          xlidx = xmidx
          idx = idx * 2
        end
      end
    end
  end
end

LiChaoTree.getMinAB = function(self, x)
  local idx = self.xinv[x]
  if not idx then print("LiChaoTree:invalid x " .. x) assert(false) return end
  local canda, candb = self.a[1][1], self.b[1][1]
  for i = self.stagenum, 2, -1 do
    local za, zb = self.a[i][idx], self.b[i][idx]
    if self:lessthan(x, za, zb, canda, candb) then
      canda, candb = za, zb
    end
    idx = brs(idx + 1, 1)
  end
  return canda, candb
end
LiChaoTree.new = function(n, x)
  local obj = {}
  setmetatable(obj, {__index = LiChaoTree})
  obj:create(n, x)
  return obj
end

--
local n = io.read("*n")
local xlim = 100000
local a, x, y = {}, {}, {}
for i = 1, n do a[i] = io.read("*n") end
for i = 1, n do x[i] = io.read("*n") end
for i = 1, n do y[i] = io.read("*n") end
local xaxis = {}
for i = 0, xlim do
  table.insert(xaxis, i)
end
local cht = LiChaoTree.new(xlim + 1, xaxis)

cht:addLine(-2LL * x[1], 1LL * (x[1] * x[1] + y[1] * y[1]))

for i = 1, n do
  local p, q = cht:getMinAB(a[i])
  local nxt = 1LL * a[i] * a[i] + p * a[i] + q
  if i == n then
    local ans = tostring(nxt):gsub("LL", "")
    print(ans)
  else
    p = -2LL * x[i + 1]
    q = 1LL * (x[i + 1] * x[i + 1] + y[i + 1] * y[i + 1]) + nxt
    cht:addLine(p, q)
  end
end
0