結果

問題 No.1675 Strange Minimum Query
ユーザー 👑 obakyanobakyan
提出日時 2021-09-10 23:26:59
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 824 ms / 2,000 ms
コード長 4,470 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 5,352 KB
実行使用メモリ 55,164 KB
最終ジャッジ日時 2023-09-02 22:10:45
合計ジャッジ時間 18,421 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 277 ms
20,640 KB
testcase_04 AC 389 ms
30,204 KB
testcase_05 AC 67 ms
19,196 KB
testcase_06 AC 338 ms
21,648 KB
testcase_07 AC 367 ms
30,032 KB
testcase_08 AC 3 ms
4,372 KB
testcase_09 AC 9 ms
4,368 KB
testcase_10 AC 351 ms
37,144 KB
testcase_11 AC 145 ms
25,840 KB
testcase_12 AC 396 ms
39,260 KB
testcase_13 AC 346 ms
40,532 KB
testcase_14 AC 481 ms
52,352 KB
testcase_15 AC 554 ms
55,164 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 117 ms
12,348 KB
testcase_18 AC 167 ms
14,240 KB
testcase_19 AC 314 ms
32,936 KB
testcase_20 AC 511 ms
35,928 KB
testcase_21 AC 517 ms
38,808 KB
testcase_22 AC 633 ms
44,180 KB
testcase_23 AC 398 ms
23,200 KB
testcase_24 AC 445 ms
27,608 KB
testcase_25 AC 265 ms
17,572 KB
testcase_26 AC 177 ms
18,880 KB
testcase_27 AC 468 ms
38,276 KB
testcase_28 AC 265 ms
29,880 KB
testcase_29 AC 250 ms
30,828 KB
testcase_30 AC 178 ms
18,404 KB
testcase_31 AC 519 ms
28,424 KB
testcase_32 AC 818 ms
47,928 KB
testcase_33 AC 824 ms
48,348 KB
testcase_34 AC 814 ms
47,568 KB
testcase_35 AC 803 ms
45,696 KB
testcase_36 AC 785 ms
45,756 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