結果

問題 No.1675 Strange Minimum Query
ユーザー 👑 obakyanobakyan
提出日時 2021-09-10 23:26:59
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 4,470 bytes
コンパイル時間 367 ms
コンパイル使用メモリ 6,940 KB
実行使用メモリ 70,316 KB
最終ジャッジ日時 2024-06-12 04:24:17
合計ジャッジ時間 17,425 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 307 ms
22,784 KB
testcase_04 AC 387 ms
35,968 KB
testcase_05 AC 89 ms
26,752 KB
testcase_06 AC 346 ms
24,064 KB
testcase_07 AC 369 ms
34,688 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 307 ms
48,280 KB
testcase_11 AC 98 ms
34,816 KB
testcase_12 AC 310 ms
51,500 KB
testcase_13 AC 298 ms
53,504 KB
testcase_14 AC 442 ms
67,528 KB
testcase_15 AC 483 ms
70,316 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 106 ms
16,000 KB
testcase_18 AC 153 ms
18,304 KB
testcase_19 AC 257 ms
44,672 KB
testcase_20 AC 463 ms
45,184 KB
testcase_21 AC 457 ms
50,688 KB
testcase_22 AC 581 ms
56,960 KB
testcase_23 AC 403 ms
27,776 KB
testcase_24 AC 405 ms
35,584 KB
testcase_25 AC 249 ms
21,504 KB
testcase_26 AC 154 ms
24,576 KB
testcase_27 AC 394 ms
50,944 KB
testcase_28 AC 236 ms
39,424 KB
testcase_29 AC 176 ms
41,600 KB
testcase_30 AC 160 ms
24,576 KB
testcase_31 AC 482 ms
33,664 KB
testcase_32 AC 809 ms
60,544 KB
testcase_33 AC 800 ms
60,544 KB
testcase_34 AC 811 ms
60,416 KB
testcase_35 AC 734 ms
58,368 KB
testcase_36 AC 725 ms
58,496 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, 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.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    self:update(idx)
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local Heapq = {}
Heapq.create = function(self, lt)
  self.lt = lt
  self.cnt = 0
  self.t = {}
end

Heapq.push = function(self, v)
  local hqlt = self.lt
  local hqt = self.t
  local c = self.cnt + 1
  self.cnt = c
  hqt[c] = v
  while 1 < c do
    local p = brs(c, 1)
    if hqlt(hqt[c], hqt[p]) then
      hqt[c], hqt[p] = hqt[p], hqt[c]
      c = p
    else
      break
    end
  end
end

Heapq.empty = function(self) return self.cnt == 0 end
Heapq.top = function(self)
  if self:empty() then return nil end
  return self.t[1]
end

Heapq.pop = function(self)
  local hqlt = self.lt
  local hqt = self.t
  local ret = hqt[1]
  local c = self.cnt
  hqt[1] = hqt[c]
  c = c - 1
  self.cnt = c
  local p = 1
  while true do
    local d1, d2 = p * 2, p * 2 + 1
    if c < d1 then break
    elseif c < d2 then
      if hqlt(hqt[d1], hqt[p]) then
        hqt[d1], hqt[p] = hqt[p], hqt[d1]
      end
      break
    else
      if hqlt(hqt[d1], hqt[d2]) then
        if hqlt(hqt[d1], hqt[p]) then
          hqt[d1], hqt[p] = hqt[p], hqt[d1]
          p = d1
        else break
        end
      else
        if hqlt(hqt[d2], hqt[p]) then
          hqt[d2], hqt[p] = hqt[p], hqt[d2]
          p = d2
        else break
        end
      end
    end
  end
  return ret
end

Heapq.new = function(lt)
  local obj = {}
  setmetatable(obj, {__index = Heapq})
  obj:create(lt)
  return obj
end


local n, q = io.read("*n", "*n")
local add, rm = {}, {}
for i = 1, n do
  add[i], rm[i] = {}, {}
end
local lq, rq, bq = {}, {}, {}
for iq = 1, q do
  local l, r, b = io.read("*n", "*n", "*n")
  lq[iq], rq[iq], bq[iq] = l, r, b
  table.insert(add[l], b)
  if r < n then
    table.insert(rm[r + 1], b)
  end
end
local hqadd = Heapq.new(function(x, y) return x > y end)
local hqrm = Heapq.new(function(x, y) return x > y end)
local st = SegTree.new(n, mmi, 1000000007)
hqadd:push(1)
for i = 1, n do
  for j = 1, #add[i] do
    hqadd:push(add[i][j])
  end
  for j = 1, #rm[i] do
    hqrm:push(rm[i][j])
  end
  while not hqrm:empty() and hqrm:top() == hqadd:top() do
    hqrm:pop()
    hqadd:pop()
  end
  st:setValue(i, hqadd:top(), true)
end
st:updateAll()
local valid = true
for iq = 1, q do
  local l, r, b = lq[iq], rq[iq], bq[iq]
  if st:getRange(l, r) ~= b then valid = false break end
end
if valid then
  for i = 1, n do
    io.write(st:getRange(i, i))
    io.write(i == n and "\n" or " ")
  end
else
  print(-1)
end
0