結果

問題 No.1441 MErGe
ユーザー 👑 obakyanobakyan
提出日時 2021-07-24 19:04:21
言語 Lua
(LuaJit 2.1.1696795921)
結果
RE  
実行時間 -
コード長 6,039 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 5,504 KB
実行使用メモリ 33,660 KB
最終ジャッジ日時 2024-07-20 01:27:31
合計ジャッジ時間 20,778 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 RE -
testcase_05 AC 36 ms
5,376 KB
testcase_06 AC 35 ms
5,376 KB
testcase_07 RE -
testcase_08 AC 464 ms
12,520 KB
testcase_09 AC 429 ms
12,520 KB
testcase_10 RE -
testcase_11 AC 725 ms
9,184 KB
testcase_12 AC 779 ms
14,452 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 TLE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 TLE -
testcase_29 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift

local LazyRangeSeg = {}

LazyRangeSeg.initAll = 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.merge(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
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
  for i = 1, n do self.stage[stagenum][i] = 1 end
  self:initAll()
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.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

-- lower_bound: experimental
-- accepted at "joisc2010_hideseek"
LazyRangeSeg.lower_bound = function(self, val)
  local ret, retpos = self.empty, 0
  local stagenum = self.stagenum
  local stage, l, r = 1, 1, bls(1, stagenum - 1)
  while true do
    local sz = bls(1, stagenum - stage)
    self:resolve(l + sz - 1)
    local tmp = self.merge(ret, self.stage[stage][mce(l / sz)])
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if l + sz <= r then stage, l = stage + 1, l + sz
      else break
      end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break
      end
    end
  end
  return retpos + 1
end

LazyRangeSeg.new = function()
  local obj = {}
  setmetatable(obj, {__index = LazyRangeSeg})
  return obj
end

local n, q = io.read("*n", "*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local asum = {a[1]}
for i = 2, n do
  asum[i] = asum[i - 1] + a[i]
end

local function merge(a, b) return a + b end
local function lzmerge(a_lz, b_lz) return a_lz or b_lz end
local function lzdevide(a_lz, numer, denom) return a_lz end
local function lzapply(a, b_lz) return b_lz and 0 or a end

lzst = LazyRangeSeg.new()
lzst:create(n,
  merge, lzmerge, lzdevide, lzapply,
  0, false)

for iq = 1, q do
  local tp, l, r = io.read("*n", "*n", "*n")
  local lb1 = lzst:lower_bound(l)
  local lb2 = lzst:lower_bound(r + 1) - 1
  if tp == 1 then
    if lb1 < lb2 then
      lzst:setRange(lb1 + 1, lb2, true)
    end
  else
    local v = n < lb2 and asum[n] or asum[lb2]
    if 1 < lb1 then v = v - asum[lb1 - 1] end
    v = tostring(1LL * v):gsub("LL", "")
    print(v)
  end
end
0