結果

問題 No.703 ゴミ拾い Easy
ユーザー 👑 obakyanobakyan
提出日時 2023-02-26 22:07:10
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 395 ms / 1,500 ms
コード長 3,640 bytes
コンパイル時間 367 ms
コンパイル使用メモリ 5,328 KB
実行使用メモリ 39,544 KB
最終ジャッジ日時 2023-10-12 11:54:42
合計ジャッジ時間 12,332 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
10,740 KB
testcase_01 AC 23 ms
10,736 KB
testcase_02 AC 23 ms
10,624 KB
testcase_03 AC 24 ms
10,808 KB
testcase_04 AC 22 ms
10,776 KB
testcase_05 AC 23 ms
10,716 KB
testcase_06 AC 24 ms
10,800 KB
testcase_07 AC 23 ms
10,932 KB
testcase_08 AC 24 ms
10,800 KB
testcase_09 AC 23 ms
10,740 KB
testcase_10 AC 23 ms
10,708 KB
testcase_11 AC 24 ms
10,788 KB
testcase_12 AC 24 ms
10,776 KB
testcase_13 AC 23 ms
10,988 KB
testcase_14 AC 32 ms
10,984 KB
testcase_15 AC 30 ms
11,108 KB
testcase_16 AC 31 ms
11,128 KB
testcase_17 AC 32 ms
10,992 KB
testcase_18 AC 29 ms
11,020 KB
testcase_19 AC 28 ms
10,996 KB
testcase_20 AC 28 ms
11,064 KB
testcase_21 AC 28 ms
11,068 KB
testcase_22 AC 33 ms
10,972 KB
testcase_23 AC 28 ms
10,912 KB
testcase_24 AC 361 ms
39,376 KB
testcase_25 AC 395 ms
39,424 KB
testcase_26 AC 318 ms
35,584 KB
testcase_27 AC 379 ms
39,368 KB
testcase_28 AC 391 ms
39,396 KB
testcase_29 AC 361 ms
39,336 KB
testcase_30 AC 371 ms
39,316 KB
testcase_31 AC 372 ms
39,544 KB
testcase_32 AC 346 ms
39,480 KB
testcase_33 AC 374 ms
39,416 KB
testcase_34 AC 219 ms
23,104 KB
testcase_35 AC 216 ms
23,168 KB
testcase_36 AC 219 ms
23,076 KB
testcase_37 AC 220 ms
23,252 KB
testcase_38 AC 220 ms
23,148 KB
testcase_39 AC 219 ms
23,156 KB
testcase_40 AC 216 ms
23,164 KB
testcase_41 AC 216 ms
23,260 KB
testcase_42 AC 217 ms
23,268 KB
testcase_43 AC 218 ms
23,176 KB
testcase_44 AC 22 ms
10,632 KB
testcase_45 AC 22 ms
10,696 KB
testcase_46 AC 244 ms
23,168 KB
testcase_47 AC 247 ms
23,180 KB
testcase_48 AC 31 ms
10,952 KB
testcase_49 AC 33 ms
11,112 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