結果

問題 No.318 学学学学学
ユーザー 👑 obakyanobakyan
提出日時 2020-05-02 16:21:30
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 3,043 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 40,768 KB
最終ジャッジ日時 2024-06-09 16:19:40
合計ジャッジ時間 5,456 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
6,816 KB
testcase_01 AC 43 ms
10,868 KB
testcase_02 AC 56 ms
11,676 KB
testcase_03 AC 34 ms
8,028 KB
testcase_04 AC 47 ms
11,256 KB
testcase_05 AC 306 ms
40,768 KB
testcase_06 AC 199 ms
28,160 KB
testcase_07 AC 159 ms
27,000 KB
testcase_08 AC 115 ms
23,160 KB
testcase_09 AC 87 ms
20,960 KB
testcase_10 AC 65 ms
18,944 KB
testcase_11 AC 316 ms
40,684 KB
testcase_12 AC 194 ms
30,604 KB
testcase_13 AC 143 ms
28,560 KB
testcase_14 AC 104 ms
24,336 KB
testcase_15 AC 84 ms
21,596 KB
testcase_16 AC 56 ms
18,944 KB
testcase_17 AC 155 ms
28,476 KB
testcase_18 AC 149 ms
29,440 KB
testcase_19 AC 155 ms
25,972 KB
testcase_20 AC 56 ms
19,200 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,944 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