結果

問題 No.2179 Planet Traveler
ユーザー 👑 obakyanobakyan
提出日時 2023-02-18 11:30:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 2,890 bytes
コンパイル時間 51 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 05:06:41
合計ジャッジ時間 17,828 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 16 ms
4,380 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 1,483 ms
4,380 KB
testcase_18 AC 1,719 ms
4,380 KB
testcase_19 AC 1,457 ms
4,380 KB
testcase_20 AC 204 ms
4,376 KB
testcase_21 AC 1,390 ms
4,376 KB
testcase_22 AC 1,048 ms
4,384 KB
testcase_23 AC 798 ms
4,380 KB
testcase_24 AC 1,392 ms
4,380 KB
testcase_25 AC 968 ms
4,380 KB
testcase_26 AC 1,491 ms
4,376 KB
testcase_27 AC 235 ms
4,376 KB
testcase_28 AC 719 ms
4,380 KB
testcase_29 AC 1,034 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local msq = math.sqrt
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 = io.read("*n")
local x, y, t = {}, {}, {}
local inf = 1000000007 * 4
local len = {}
local asked = {}
for i = 1, n do
  x[i], y[i], t[i] = io.read("*n", "*n", "*n")
  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 function sqrtceil(v)
  local z = mma(0, mfl(msq(v)) - 2)
  while true do
    if v <= z * z then return z end
    z = z + 1
  end
end

local function sqrtfloor(v)
  local l, r = 0LL, 1280000001LL
  while 1LL < r - l do
    local m = (l + r) / 2LL
    if m * m <= v then
      l = m
    else
      r = m
    end
  end
  return l
end

local function getlen1(a, b)
  local v1 = x[a] * x[a] + y[a] * y[a]
  local v2 = x[b] * x[b] + y[b] * y[b]
  local z = sqrtfloor(4LL * v1 * v2)
  z = tostring(z):gsub("LL", "")
  z = tonumber(z)
  -- print(v1, v2, z)
  return v1 + v2 - z
end
-- print("a", getlen1(1, 2))
-- os.exit()

local function getlen2(a, b)
  local v = (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b])
  return v
end

local st = SegTree.new(n, mergefunc, n + 1)

for i = 1, n 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 = 1, n do
    local cost = t[src] ~= t[dst] and getlen1(src, dst) or getlen2(src, dst)
    cost = mma(cost, len[src])
    if cost < len[dst] then
      len[dst] = cost
      st:update(dst)
    end
  end
end
print(len[n])

0