結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 obakyanobakyan
提出日時 2019-11-06 22:53:55
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,053 ms / 3,000 ms
コード長 3,218 bytes
コンパイル時間 116 ms
コンパイル使用メモリ 5,156 KB
実行使用メモリ 42,676 KB
最終ジャッジ日時 2023-10-13 02:45:11
合計ジャッジ時間 15,418 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 160 ms
4,352 KB
testcase_04 AC 1,053 ms
42,664 KB
testcase_05 AC 947 ms
42,676 KB
testcase_06 AC 138 ms
6,452 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,476 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 339 ms
23,060 KB
testcase_13 AC 335 ms
23,016 KB
testcase_14 AC 353 ms
23,036 KB
testcase_15 AC 362 ms
22,792 KB
testcase_16 AC 310 ms
19,920 KB
testcase_17 AC 361 ms
22,604 KB
testcase_18 AC 417 ms
23,324 KB
testcase_19 AC 478 ms
23,932 KB
testcase_20 AC 518 ms
27,196 KB
testcase_21 AC 573 ms
37,292 KB
testcase_22 AC 602 ms
37,276 KB
testcase_23 AC 652 ms
38,632 KB
testcase_24 AC 704 ms
39,952 KB
testcase_25 AC 808 ms
42,200 KB
testcase_26 AC 881 ms
42,516 KB
testcase_27 AC 4 ms
4,352 KB
testcase_28 AC 4 ms
4,352 KB
testcase_29 AC 4 ms
4,356 KB
testcase_30 AC 361 ms
16,072 KB
testcase_31 AC 333 ms
16,052 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,348 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 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