結果

問題 No.1099 Range Square Sum
ユーザー 👑 obakyanobakyan
提出日時 2020-11-30 23:23:29
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 4,329 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 67,072 KB
最終ジャッジ日時 2024-09-13 02:40:32
合計ジャッジ時間 8,613 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 13 ms
6,944 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 11 ms
6,944 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 11 ms
6,940 KB
testcase_18 AC 13 ms
6,940 KB
testcase_19 AC 11 ms
6,944 KB
testcase_20 AC 11 ms
6,944 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,552 ms
66,944 KB
testcase_27 AC 1,698 ms
66,816 KB
testcase_28 AC 1,562 ms
66,944 KB
testcase_29 AC 1,644 ms
66,944 KB
testcase_30 AC 1,568 ms
67,072 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.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.lin[i][j] = self.lin[i + 1][j * 2 - 1] + self.lin[i + 1][j * 2]
      self.sq[i][j] = self.sq[i + 1][j * 2 - 1] + self.sq[i + 1][j * 2]
    end
  end
end
LazyRangeSeg.create = function(self, n)
  local stagenum, mul = 1, 1
  self.lin = {{0}}
  self.sq = {{0}}
  self.lazy = {{0}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.lin[stagenum] = {}
    self.sq[stagenum] = {}
    self.lazy[stagenum] = {}
    for i = 1, mul do
      self.lin[stagenum][i] = 0
      self.sq[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
  for i = 1, n do
    local a = io.read("*n")
    self.lin[stagenum][i] = a
    self.sq[stagenum][i] = 1LL * a * a
  end
  self:updateAll()
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 ~= 0 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.lazy[i + 1][curidx * 2 - 1] + lzval
        self.lazy[i + 1][curidx * 2] = self.lazy[i + 1][curidx * 2] + lzval
        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 sz = bls(1, self.stagenum - stagepos)
  local linadd = sz * value
  local sqadd1 = 1LL * sz * value * value
  local sqadd2 = 2LL * self.lin[stagepos][idx] * value
  self.lin[stagepos][idx] = self.lin[stagepos][idx] + linadd
  self.sq[stagepos][idx] = self.sq[stagepos][idx] + sqadd1 + sqadd2
  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.lin[i][dst] = self.lin[i + 1][idx] + self.lin[i + 1][rem]
    self.sq[i][dst] = self.sq[i + 1][idx] + self.sq[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 ret = 0LL
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    ret = ret + self.sq[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)
    self:resolveRange(stage, idx, value)
    self.lazy[stage][idx] = self.lazy[stage][idx] + value
    left = left + sz
  end
end

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

local n = io.read("*n")
local lzst = LazyRangeSeg.new()
lzst:create(n)
local q = io.read("*n")
for iq = 1, q do
  local tp = io.read("*n")
  if tp == 1 then
    local l, r, x = io.read("*n", "*n", "*n")
    lzst:setRange(l, r, x)
  else
    local l, r = io.read("*n", "*n")
    local val = lzst:getRange(l, r)
    val = tostring(val):gsub("LL", "")
    print(val)
  end
end
0