結果
問題 | No.1234 典型RMQ |
ユーザー | 👑 obakyan |
提出日時 | 2020-11-28 11:01:14 |
言語 | Lua (LuaJit 2.1.1696795921) |
結果 |
AC
|
実行時間 | 473 ms / 2,000 ms |
コード長 | 5,377 bytes |
コンパイル時間 | 161 ms |
コンパイル使用メモリ | 5,248 KB |
実行使用メモリ | 9,216 KB |
最終ジャッジ日時 | 2024-11-09 02:38:18 |
合計ジャッジ時間 | 10,214 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 430 ms
9,088 KB |
testcase_07 | AC | 298 ms
5,248 KB |
testcase_08 | AC | 473 ms
9,088 KB |
testcase_09 | AC | 382 ms
6,016 KB |
testcase_10 | AC | 453 ms
9,088 KB |
testcase_11 | AC | 422 ms
9,088 KB |
testcase_12 | AC | 374 ms
6,144 KB |
testcase_13 | AC | 303 ms
5,248 KB |
testcase_14 | AC | 386 ms
6,016 KB |
testcase_15 | AC | 369 ms
6,016 KB |
testcase_16 | AC | 444 ms
9,088 KB |
testcase_17 | AC | 383 ms
6,016 KB |
testcase_18 | AC | 267 ms
5,248 KB |
testcase_19 | AC | 448 ms
9,088 KB |
testcase_20 | AC | 283 ms
9,088 KB |
testcase_21 | AC | 419 ms
9,216 KB |
testcase_22 | AC | 357 ms
9,088 KB |
testcase_23 | AC | 359 ms
9,088 KB |
testcase_24 | AC | 377 ms
9,088 KB |
testcase_25 | AC | 369 ms
9,216 KB |
testcase_26 | AC | 351 ms
9,088 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 1 ms
5,248 KB |
testcase_29 | AC | 2 ms
5,248 KB |
ソースコード
local mfl, mce = math.floor, math.ceil local mmi, mma = math.min, math.max local bls, brs = bit.lshift, bit.rshift local LazyRangeSeg = {} LazyRangeSeg.create = function(self, n, merge, lzmerge, lzdevide, lzapply, empty, lzempty) self.merge, self.lzmerge = merge, lzmerge self.lzdevide, self.lzapply = lzdevide, lzapply self.empty, self.lzempty = empty, lzempty local stagenum, mul = 1, 1 self.stage = {{empty}} self.lazy = {{lzempty}} while mul < n do mul, stagenum = mul * 2, stagenum + 1 self.stage[stagenum] = {} self.lazy[stagenum] = {} for i = 1, mul do self.stage[stagenum][i] = empty self.lazy[stagenum][i] = lzempty end end 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 self.stagenum = stagenum end LazyRangeSeg.resolve = function(self, right) local stagenum = self.stagenum local offset = 0 for i = 1, stagenum - 1 do local p = offset + bls(1, stagenum - i) if p < right then offset = p p = p + bls(1, stagenum - i) end if right < p then local curidx = brs(p, stagenum - i) local lzval = self.lazy[i][curidx] if lzval ~= self.lzempty then local sz = bls(1, stagenum - i - 1) lzval = self.lzdevide(lzval, sz, sz * 2) self:resolveRange(i + 1, curidx * 2 - 1, lzval, true) self:resolveRange(i + 1, curidx * 2, lzval, true) self.lazy[i + 1][curidx * 2 - 1] = self.lzmerge(self.lazy[i + 1][curidx * 2 - 1], lzval) self.lazy[i + 1][curidx * 2] = self.lzmerge(self.lazy[i + 1][curidx * 2], lzval) self.lazy[i][curidx] = self.lzempty end elseif p == right then break else assert(false) end end end LazyRangeSeg.resolveRange = function(self, stagepos, idx, value, shallow) self.stage[stagepos][idx] = self.lzapply(self.stage[stagepos][idx], value) if shallow then return end for i = stagepos - 1, 1, -1 do local dst = brs(idx + 1, 1) local rem = dst * 4 - 1 - idx self.stage[i][dst] = self.merge(self.stage[i + 1][idx], self.stage[i + 1][rem]) idx = dst end end LazyRangeSeg.setValue = function(self, idx, value) self.stage[self.stagenum][idx] = value end LazyRangeSeg.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] = mmi(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2]) end end end LazyRangeSeg.resolveAll = function(self) local stagenum = self.stagenum for i = 1, stagenum - 1 do local cnt = bls(1, i - 1) for j = 1, cnt do local lzval = self.lazy[i][j] if lzval ~= self.lzempty then local sz = bls(1, stagenum - i - 1) lzval = self.lzdevide(lzval, sz, sz * 2) self:resolveRange(i + 1, j * 2 - 1, lzval, true) self:resolveRange(i + 1, j * 2, lzval, true) self.lazy[i + 1][j * 2 - 1] = self.lzmerge(self.lazy[i + 1][j * 2 - 1], lzval) self.lazy[i + 1][j * 2] = self.lzmerge(self.lazy[i + 1][j * 2], lzval) self.lazy[i][j] = self.lzempty end end end end LazyRangeSeg.getRange = function(self, left, right) if 1 < left then self:resolve(left - 1) end self:resolve(right) local stagenum = self.stagenum local ret = self.empty 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.merge(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)]) left = left + sz end return ret end LazyRangeSeg.setRange = function(self, left, right, value) if 1 < left then self:resolve(left - 1) end self:resolve(right) local stagenum = self.stagenum while left <= right do local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1]) local sz = bls(1, stagenum - stage) local len = right - left + 1 local idx = 1 + brs(left - 1, stagenum - stage) local v = self.lzdevide(value, sz, len) value = self.lzdevide(value, len - sz, len) self:resolveRange(stage, idx, v) self.lazy[stage][idx] = self.lzmerge(self.lazy[stage][idx], v) left = left + sz end end LazyRangeSeg.new = function() local obj = {} setmetatable(obj, {__index = LazyRangeSeg}) return obj end -- merge(a, b) -> c -- lzmerge(a_lz, b_lz) -> c_lz -- lzdevide(a_lz, numer, denom) -> b_lz -- lzapply(a, b_lz) -> c -- lzst = LazyRangeSeg.new() -- lzst:create(n, -- merge, lzmerge, lzdevide, lzapply, -- empty, lzempty) local function merge(a, b) return a < b and a or b end local function lzmerge(a, b) return a + b end local function lzdevide(a, _num, _den) return a end local function lzapply(a, blz) return a + blz end local n = io.read("*n") local lzst = LazyRangeSeg.new() lzst:create(n, merge, lzmerge, lzdevide, lzapply, 1000000007 * 20, 0) for i = 1, n do local a = io.read("*n") lzst:setValue(i, a) end lzst:updateAll() local q = io.read("*n") for iq = 1, q do local tp, l, r, c = io.read("*n", "*n", "*n", "*n") if tp == 1 then lzst:setRange(l, r, c) else print(lzst:getRange(l, r)) end end