結果

問題 No.1995 CHIKA Road
ユーザー 👑 obakyanobakyan
提出日時 2022-07-02 01:03:26
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 617 ms / 2,000 ms
コード長 2,541 bytes
コンパイル時間 141 ms
コンパイル使用メモリ 5,300 KB
実行使用メモリ 41,516 KB
最終ジャッジ日時 2023-08-17 12:50:47
合計ジャッジ時間 8,790 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 33 ms
4,852 KB
testcase_07 AC 116 ms
10,876 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 61 ms
6,092 KB
testcase_11 AC 617 ms
41,516 KB
testcase_12 AC 314 ms
22,840 KB
testcase_13 AC 214 ms
18,684 KB
testcase_14 AC 199 ms
19,160 KB
testcase_15 AC 538 ms
39,176 KB
testcase_16 AC 55 ms
6,432 KB
testcase_17 AC 236 ms
19,436 KB
testcase_18 AC 402 ms
27,692 KB
testcase_19 AC 412 ms
27,584 KB
testcase_20 AC 209 ms
18,640 KB
testcase_21 AC 190 ms
15,000 KB
testcase_22 AC 376 ms
26,876 KB
testcase_23 AC 111 ms
10,948 KB
testcase_24 AC 332 ms
25,124 KB
testcase_25 AC 62 ms
7,448 KB
testcase_26 AC 141 ms
13,124 KB
testcase_27 AC 319 ms
24,812 KB
testcase_28 AC 191 ms
15,060 KB
testcase_29 AC 374 ms
27,256 KB
testcase_30 AC 61 ms
6,764 KB
testcase_31 AC 30 ms
4,668 KB
testcase_32 AC 81 ms
8,244 KB
testcase_33 AC 313 ms
24,716 KB
testcase_34 AC 35 ms
5,064 KB
testcase_35 AC 124 ms
11,608 KB
testcase_36 AC 144 ms
13,508 KB
testcase_37 AC 356 ms
26,188 KB
testcase_38 AC 246 ms
19,996 KB
testcase_39 AC 27 ms
4,516 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 n, m = io.read("*n", "*n")
local tm = {}
local a, b = {}, {}
for i = 1, m do
  a[i], b[i] = io.read("*n", "*n")
  tm[a[i]], tm[b[i]] = true, true
end
tm[1] = true
tm[n] = true
local tuniq = {}
for k, v in pairs(tm) do
  table.insert(tuniq, k)
end
table.sort(tuniq)
local tn = #tuniq
for i = 1, tn do
  tm[tuniq[i]] = i
end

local inf = 1000000007 * 10
local len = {}
local asked = {}
local edge = {}
for i = 1, tn do
  len[i] = inf
  asked[i] = false
  edge[i] = {}
end
len[1] = 0
asked[tn + 1] = true

for i = 1, tn - 1 do
  local x, y = tuniq[i], tuniq[i + 1]
  edge[i][i + 1] = (y - x) * 2
end
for i = 1, m do
  local ai, bi = a[i], b[i]
  local ap, bp = tm[ai], tm[bi]
  edge[ap][bp] = (bi - ai) * 2 - 1  
end

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(tn, mergefunc, tn + 1)

for i = 1, tn do
  local src = st.stage[1]
  if asked[src] then break end
  if inf <= len[src] then break end
  asked[src] = true
  st:update(src, true)
  for dst, cost in pairs(edge[src]) do
    if len[src] + cost < len[dst] then
      len[dst] = len[src] + cost
      st:update(dst)
    end
  end
end

print(len[tn])
0