結果

問題 No.230 Splarraay スプラレェーイ
ユーザー 👑 obakyanobakyan
提出日時 2020-09-20 10:23:07
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 393 ms / 5,000 ms
コード長 3,853 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-06-24 10:39:23
合計ジャッジ時間 3,938 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 7 ms
6,944 KB
testcase_06 AC 22 ms
6,940 KB
testcase_07 AC 7 ms
6,944 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 173 ms
7,168 KB
testcase_10 AC 141 ms
6,944 KB
testcase_11 AC 110 ms
6,940 KB
testcase_12 AC 170 ms
7,168 KB
testcase_13 AC 30 ms
6,940 KB
testcase_14 AC 88 ms
11,264 KB
testcase_15 AC 316 ms
11,392 KB
testcase_16 AC 305 ms
11,392 KB
testcase_17 AC 393 ms
11,392 KB
testcase_18 AC 237 ms
11,392 KB
testcase_19 AC 264 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

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.create = function(self, n)
  self.n = n
  local stagenum, mul = 1, 1
  self.A = {{0}}
  self.B = {{0}}
  self.lazy = {{0}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.A[stagenum] = {}
    self.B[stagenum] = {}
    self.lazy[stagenum] = {}
    for i = 1, mul do
      self.A[stagenum][i] = 0
      self.B[stagenum][i] = 0
      self.lazy[stagenum][i] = 0
    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 incval = self.lazy[i][curidx]
      if 0 < incval then
        self:resolveRange(i + 1, curidx * 2 - 1, incval, true)
        self:resolveRange(i + 1, curidx * 2, incval, true)
        self.lazy[i + 1][curidx * 2 - 1] = incval
        self.lazy[i + 1][curidx * 2] = incval
        self.lazy[i][curidx] = 0
      end
    elseif p == right then
      break
    else
      assert(false)
    end
  end
end
LazyRangeSeg.resolveRange = function(self, stagepos, idx, value, shallow)
  local len = bls(1, self.stagenum - stagepos)
  if value == 1 then
    self.A[stagepos][idx] = len
    self.B[stagepos][idx] = 0
  else
    self.A[stagepos][idx] = 0
    self.B[stagepos][idx] = len
  end
  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.A[i][dst] = self.A[i + 1][idx] + self.A[i + 1][rem]
    self.B[i][dst] = self.B[i + 1][idx] + self.B[i + 1][rem]
    idx = dst
  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 reta, retb = 0, 0
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    reta = reta + self.A[stage][1 + brs(left - 1, stagenum - stage)]
    retb = retb + self.B[stage][1 + brs(left - 1, stagenum - stage)]
    left = left + sz
  end
  return reta, retb
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)
    self:resolveRange(stage, idx, value)
    self.lazy[stage][idx] = value
    left = left + sz
  end
end
LazyRangeSeg.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = LazyRangeSeg})
  obj:create(n)
  return obj
end

local n, q = io.read("*n", "*n")
local lzst = LazyRangeSeg.new(n)
local suma, sumb = 0, 0
for iq = 1, q do
  local x, l, r = io.read("*n", "*n", "*n")
  l, r = l + 1, r + 1
  if x == 0 then
    local pa, pb = lzst:getRange(l, r)
    if pa < pb then
      sumb = sumb + pb
    elseif pb < pa then
      suma = suma + pa
    end
  elseif x == 1 then
    lzst:setRange(l, r, 1)
  else
    lzst:setRange(l, r, 2)
  end
end
local ra, rb = lzst:getRange(1, n)
suma = suma + ra
sumb = sumb + rb
print(suma .. " " ..sumb)
0