結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 obakyanobakyan
提出日時 2022-06-23 00:31:06
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,844 ms / 3,000 ms
コード長 2,801 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 134,448 KB
最終ジャッジ日時 2024-04-24 04:30:47
合計ジャッジ時間 20,440 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1,031 ms
84,656 KB
testcase_04 AC 979 ms
84,660 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 492 ms
41,728 KB
testcase_07 AC 885 ms
78,128 KB
testcase_08 AC 1,844 ms
134,448 KB
testcase_09 AC 1,122 ms
84,012 KB
testcase_10 AC 1,018 ms
76,212 KB
testcase_11 AC 837 ms
70,060 KB
testcase_12 AC 938 ms
75,056 KB
testcase_13 AC 773 ms
71,732 KB
testcase_14 AC 635 ms
48,304 KB
testcase_15 AC 1,337 ms
79,544 KB
testcase_16 AC 580 ms
45,616 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 942 ms
81,328 KB
testcase_19 AC 1,237 ms
79,416 KB
testcase_20 AC 854 ms
70,192 KB
testcase_21 AC 731 ms
48,944 KB
testcase_22 AC 732 ms
48,944 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 = io.read("*n")
local x = {}
for i = 1, n do
  x[i] = io.read("*n")
end
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local tmap = {}
for i = 1, n do
  tmap[x[i]] = true
  tmap[x[i] + a[i]] = true
  if 0 <= x[i] - a[i] then
    tmap[x[i] - a[i]] = true
  end
end
local tuniq = {}
for k, v in pairs(tmap) do
  table.insert(tuniq, k)
end
table.sort(tuniq)
local tn = #tuniq
for i = 1, tn do
  tmap[tuniq[i]] = i
end
local edge = {}
for i = 1, tn do
  edge[i] = {}
end
for i = 1, n do
  local xi, ai = x[i], a[i]
  local z = tmap[xi]
  local d = tmap[xi + ai]
  table.insert(edge[d], z)
  if 0 <= xi - ai then
    local d = tmap[xi - ai]
    table.insert(edge[d], z)
  end
end
local inf = 1000000007 * 2

local len = {}
local asked = {}
for i = 1, tn do
  len[i] = inf
  asked[i] = false
end
for i = 1, tn do
  len[i] = inf - tuniq[i]
end
asked[tn + 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(tn, mergefunc, tn + 1)

for i = 1, tn do
  local src = st.stage[1]
  -- print(tuniq[src], src, inf - len[src])
  if asked[src] then break end
  if inf <= len[src] then break end
  asked[src] = true
  st:update(src, true)
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if len[src] < len[dst] then
      len[dst] = len[src]
      st:update(dst)
    end
  end
end
for i = 1, n do
  local xi = tmap[x[i]]
  print(inf - len[xi] - x[i])
end
0