結果

問題 No.318 学学学学学
ユーザー 👑 obakyanobakyan
提出日時 2020-05-02 16:21:30
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 3,043 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 5,372 KB
実行使用メモリ 33,328 KB
最終ジャッジ日時 2023-08-28 21:24:51
合計ジャッジ時間 7,469 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
5,600 KB
testcase_01 AC 67 ms
9,128 KB
testcase_02 AC 81 ms
9,588 KB
testcase_03 AC 49 ms
6,616 KB
testcase_04 AC 72 ms
9,224 KB
testcase_05 AC 490 ms
33,328 KB
testcase_06 AC 316 ms
21,492 KB
testcase_07 AC 258 ms
20,388 KB
testcase_08 AC 205 ms
16,464 KB
testcase_09 AC 160 ms
14,448 KB
testcase_10 AC 133 ms
12,288 KB
testcase_11 AC 481 ms
33,296 KB
testcase_12 AC 302 ms
23,280 KB
testcase_13 AC 227 ms
21,584 KB
testcase_14 AC 171 ms
17,272 KB
testcase_15 AC 157 ms
14,708 KB
testcase_16 AC 58 ms
12,344 KB
testcase_17 AC 216 ms
21,248 KB
testcase_18 AC 207 ms
22,624 KB
testcase_19 AC 196 ms
19,440 KB
testcase_20 AC 57 ms
12,628 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[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, n do self.stage[stagenum][i] = i end
  for i = n + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    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 spos, epos = {}, {}
local uniqtbl = {}
for i = 1, n do
  local a = io.read("*n")
  if not spos[a] then
    spos[a] = i
    epos[a] = i + 1
    table.insert(uniqtbl, a)
  else
    epos[a] = i + 1
  end
end
table.sort(uniqtbl)
local m = #uniqtbl
local sevent = {}
local eevent = {}
for i = 1, n + 1 do
  sevent[i] = {}
  eevent[i] = {}
end
local state = {}
for i = 1, m do
  state[i] = false
  local aval = uniqtbl[i]
  local sp, ep = spos[aval], epos[aval]
  table.insert(sevent[sp], i)
  table.insert(eevent[ep], i)
end
state[m + 1] = false

local function mergefunc(a, b)
  if not state[a] then return b
  elseif not state[b] then return a
  else
    return a < b and b or a
  end
end
local st = SegTree.new(m, mergefunc, m + 1)
for i = 1, n do
  for j = 1, #sevent[i] do
    state[sevent[i][j]] = true
    st:update(sevent[i][j])
  end
  for j = 1, #eevent[i] do
    state[eevent[i][j]] = false
    st:update(eevent[i][j])
  end
  local v = st.stage[1][1]
  local aval = uniqtbl[v]
  io.write(aval)
  io.write(i == n and "\n" or " ")
end
0