結果

問題 No.2095 High Rise
ユーザー 👑 obakyanobakyan
提出日時 2023-01-25 15:38:26
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 2,956 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 88,608 KB
最終ジャッジ日時 2023-09-09 02:55:32
合計ジャッジ時間 14,047 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 13 ms
4,376 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 206 ms
13,652 KB
testcase_18 AC 138 ms
12,040 KB
testcase_19 AC 32 ms
4,916 KB
testcase_20 AC 216 ms
13,736 KB
testcase_21 AC 434 ms
23,284 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 1,993 ms
88,608 KB
testcase_26 AC 1,997 ms
86,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {} -- use 1D Tree (stage[], not stage[][])
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx, force)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    if not force and self.stage[dst] ~= self.stage[idx] then break end
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local h, w = io.read("*n", "*n", "*l")
local a = {}
for i = 1, h do
  a[i] = {}
  local s = io.read()
  for b in s:gmatch("(%d+)") do
    table.insert(a[i], tonumber(b))
  end
end
local n = h * w * 2
local len = {}
local asked = {}
local inf = 1000000007 * 1000
for i = 1, n do
  len[i] = inf
  asked[i] = false
end
len[1] = 0
asked[n + 1] = true

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  else
    return len[x] < len[y] and x or y
  end
end
local st = SegTree.new(n, mergefunc, n + 1)

local tasks = {1}
local tasksize = 1

local function walk(src, dst, cost)
  if len[src] + cost < len[dst] then
    len[dst] = len[src] + cost
    st:update(dst)
  end
end
local function walk0(src, dst)
  if len[src] < len[dst] then
    len[dst] = len[src]
    tasksize = tasksize + 1
    tasks[tasksize] = dst
  end
end

while true do
  local src = false
  if 0 < tasksize then
    src = tasks[tasksize]
    tasksize = tasksize - 1
  else
    src = st.stage[1]
  end
  if asked[src] then break end
  if inf <= len[src] then break end
  asked[src] = true
  st:update(src, true)
  local alt = h * w < src
  local row = mce(src / w)
  local col = src - (row - 1) * w
  if not alt and row == h then break end
  if h < row then row = row - h end
  if alt then
    walk0(src, src - h * w)
    if row < h then
      walk(src, src + w, a[row + 1][col])
    end
  else
    walk(src, src + h * w, a[row][col])
    if col < w then
      walk0(src, src + 1)
    else
      walk0(src, src + 1 - w)
    end
  end
end
local ret = inf
for i = 1, w do
  ret = mmi(ret, len[(h - 1) * w + i])
end
print(ret)
-- print(os.clock())
0