結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 obakyanobakyan
提出日時 2019-11-06 22:53:55
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 886 ms / 3,000 ms
コード長 3,218 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 47,960 KB
最終ジャッジ日時 2024-09-15 00:27:06
合計ジャッジ時間 12,276 ms
ジャッジサーバーID
(参考情報)
judge3 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 89 ms
5,376 KB
testcase_04 AC 886 ms
47,708 KB
testcase_05 AC 847 ms
47,580 KB
testcase_06 AC 81 ms
6,784 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 366 ms
25,424 KB
testcase_13 AC 335 ms
25,448 KB
testcase_14 AC 351 ms
25,540 KB
testcase_15 AC 362 ms
25,328 KB
testcase_16 AC 279 ms
22,124 KB
testcase_17 AC 373 ms
25,396 KB
testcase_18 AC 398 ms
25,724 KB
testcase_19 AC 443 ms
27,156 KB
testcase_20 AC 458 ms
30,072 KB
testcase_21 AC 714 ms
39,232 KB
testcase_22 AC 586 ms
40,196 KB
testcase_23 AC 640 ms
41,064 KB
testcase_24 AC 663 ms
41,956 KB
testcase_25 AC 722 ms
44,648 KB
testcase_26 AC 800 ms
47,960 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 5 ms
5,376 KB
testcase_30 AC 313 ms
18,916 KB
testcase_31 AC 284 ms
16,964 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.stage[i + 1][j * 2 - 1] + self.stage[i + 1][j * 2]
    end
  end
end
SegTree.create = function(self, n)
  self.func = func
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = 0 end
  self:updateAll()
end
SegTree.add = function(self, idx, value)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + value
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] + self.stage[i + 1][rem]
    idx = dst
  end
end
SegTree.lower_bound = function(self, val)
  local ret, retpos = 0, 0
  local t1, t2, t3 = {1}, {1}, {self.size[1]}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if sz <= r + 1 - l then
      local tmp = ret + self.stage[stage][mce(l / sz)]
      if tmp < val then
        ret, retpos = tmp, l + sz - 1
        if l + sz <= r then table.insert(t1, stage + 1) table.insert(t2, l + sz) table.insert(t3, r) end
      else
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
      end
    else
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return retpos + 1
end
SegTree.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n)
  return obj
end

local function lltonumber(str)
  local ret = 0LL
  for i = 1, #str do
    ret = ret * 10LL
    ret = ret + tonumber(str:sub(i, i))
  end
  return ret
end

local function llprint(llnumber)
  local str = tostring(llnumber):gsub("LL", "")
  print(str)
end
local q, k = io.read("*n", "*n", "*l")
local tasks = {}
local nums = {}
local numuniq = {}
for i = 1, q do
  local str = io.read()
  if str:sub(1, 1) == "1" then
    table.insert(tasks, true)
    local numstr = str:sub(3, #str)
    local tmp = ""
    for i = 1, 19 - #numstr do
      tmp = tmp .. "0"
    end
    numstr = tmp .. numstr
    table.insert(tasks, numstr)
    if not numuniq[numstr] then
      table.insert(nums, numstr)
      numuniq[numstr] = true
    end
  else
    table.insert(tasks, false)
  end
end
table.sort(nums)
local nummap = {}
for i = 1, #nums do
  nummap[nums[i]] = i
end
local st = SegTree.new(#nums)
local done = 0
local curcnt = 0
while done < #tasks do
  done = done + 1
  if tasks[done] then
    done = done + 1
    local num = tasks[done]
    local pos = nummap[num]
    st:add(pos, 1)
    curcnt = curcnt + 1
  else
    if curcnt < k then
      print(-1)
    else
      local pos = st:lower_bound(k)
      local llnum = lltonumber(nums[pos])
      llprint(llnum)
      st:add(pos, -1)
      curcnt = curcnt - 1
    end
  end
end
0