結果
| 問題 |
No.1675 Strange Minimum Query
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2021-09-10 23:26:59 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
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