結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 obakyanobakyan
提出日時 2019-11-06 23:07:46
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 848 ms / 3,000 ms
コード長 2,930 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 47,712 KB
最終ジャッジ日時 2024-09-15 00:27:42
合計ジャッジ時間 11,303 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 88 ms
6,944 KB
testcase_04 AC 848 ms
47,712 KB
testcase_05 AC 774 ms
47,708 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 294 ms
24,036 KB
testcase_13 AC 293 ms
24,300 KB
testcase_14 AC 314 ms
23,876 KB
testcase_15 AC 293 ms
24,312 KB
testcase_16 AC 271 ms
21,872 KB
testcase_17 AC 338 ms
25,396 KB
testcase_18 AC 362 ms
25,716 KB
testcase_19 AC 403 ms
25,572 KB
testcase_20 AC 448 ms
30,708 KB
testcase_21 AC 534 ms
38,720 KB
testcase_22 AC 565 ms
40,192 KB
testcase_23 AC 609 ms
40,548 KB
testcase_24 AC 624 ms
40,548 KB
testcase_25 AC 660 ms
41,312 KB
testcase_26 AC 708 ms
44,772 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 267 ms
16,228 KB
testcase_31 AC 255 ms
15,844 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 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 stage, l, r = 1, 1, self.size[1]
  while true do
    local sz = self.size[stage]
    local tmp = ret + self.stage[stage][mce(l / sz)]
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if l + sz <= r then stage, l = stage + 1, l + sz
      else break
      end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break
      end
    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