結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 obakyanobakyan
提出日時 2019-11-06 23:07:46
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,057 ms / 3,000 ms
コード長 2,930 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 5,328 KB
実行使用メモリ 42,808 KB
最終ジャッジ日時 2023-10-13 02:45:57
合計ジャッジ時間 12,934 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 156 ms
4,348 KB
testcase_04 AC 1,057 ms
42,792 KB
testcase_05 AC 967 ms
42,808 KB
testcase_06 AC 138 ms
6,360 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 324 ms
21,400 KB
testcase_13 AC 335 ms
21,484 KB
testcase_14 AC 337 ms
21,760 KB
testcase_15 AC 331 ms
21,496 KB
testcase_16 AC 304 ms
19,972 KB
testcase_17 AC 349 ms
21,952 KB
testcase_18 AC 408 ms
23,372 KB
testcase_19 AC 453 ms
23,960 KB
testcase_20 AC 497 ms
27,312 KB
testcase_21 AC 574 ms
37,336 KB
testcase_22 AC 589 ms
37,264 KB
testcase_23 AC 687 ms
37,232 KB
testcase_24 AC 691 ms
37,872 KB
testcase_25 AC 773 ms
40,420 KB
testcase_26 AC 788 ms
41,688 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 4 ms
4,352 KB
testcase_29 AC 4 ms
4,352 KB
testcase_30 AC 358 ms
16,140 KB
testcase_31 AC 321 ms
16,156 KB
testcase_32 AC 2 ms
4,356 KB
testcase_33 AC 2 ms
4,480 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,348 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