結果

問題 No.1000 Point Add and Array Add
ユーザー 👑 obakyanobakyan
提出日時 2020-02-28 22:04:58
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 3,228 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 7,072 KB
実行使用メモリ 56,452 KB
最終ジャッジ日時 2024-04-21 18:32:39
合計ジャッジ時間 9,267 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 7 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 660 ms
44,288 KB
testcase_17 AC 563 ms
38,688 KB
testcase_18 AC 991 ms
56,396 KB
testcase_19 AC 1,054 ms
56,452 KB
testcase_20 AC 464 ms
45,084 KB
testcase_21 AC 1,084 ms
52,312 KB
testcase_22 AC 841 ms
51,708 KB
testcase_23 AC 1,122 ms
52,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce, mmi = math.floor, math.ceil, math.min
local mma = math.max
local LazySegTree = {}

LazySegTree.create = function(self, n, emptyvalue)
  self.emptyvalue = emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.size = {1}, {}
  self.lazy = {{emptyvalue}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum] = mul
    self.lazy[stagenum] = {}
    for i = 1, mul do self.lazy[stagenum][i] = emptyvalue end
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
end
LazySegTree.resolve = function(self)
  for stage = 1, self.stagenum - 1 do
    for pos = 1, self.cnt[stage] do
      local v = self.lazy[stage][pos]
      if v ~= self.emptyvalue then
        self.lazy[stage + 1][pos * 2 - 1] = self.lazy[stage + 1][pos * 2 - 1] + v
        self.lazy[stage + 1][pos * 2] = self.lazy[stage + 1][pos * 2] + v
        self.lazy[stage][pos] = self.emptyvalue
      end
    end
  end
  return self.lazy[self.stagenum]
end
LazySegTree.getValue = function(self, idx)
  for stage = 1, self.stagenum - 1 do
    local pos = mce(idx / (self.size[stage]))
    local v = self.lazy[stage][pos]
    if v ~= self.emptyvalue then
      self.lazy[stage + 1][pos * 2 - 1] = self.lazy[stage + 1][pos * 2 - 1] + v
      self.lazy[stage + 1][pos * 2] = self.lazy[stage + 1][pos * 2] + v
      self.lazy[stage][pos] = self.emptyvalue
    end
  end
  return self.lazy[self.stagenum][idx]
end
LazySegTree.addRange = function(self, left, right, value)
  if left == right then self.lazy[self.stagenum][left] = self.lazy[self.stagenum][left] + value return end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      self.lazy[stage][mce(l / sz)] = self.lazy[stage][mce(l / sz)] + value
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
end
LazySegTree.new = function(n, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = LazySegTree})
  obj:create(n, emptyvalue)
  return obj
end

local n, q = io.read("*n", "*n", "*l")
local a = {}
local b = {}
do
  local tmp = io.read()
  for z in tmp:gmatch("%d+") do
    table.insert(a, tonumber(z))
  end
end
for i = 1, n do
  b[i] = 0LL
end
local qs = {}
for i = 1, q do
  qs[i] = io.read()
end
local lst = LazySegTree.new(n, 0)
for i = q, 1, -1 do
  local qc, qx, qy = qs[i]:match("(%w) (%d+) (%d+)")
  qx, qy = tonumber(qx), tonumber(qy)
  if qc == "B" then
    lst:addRange(qx, qy, 1)
  else
    local z = lst:getValue(qx)
    b[qx] = b[qx] + 1LL * z * qy
  end
end
local zz = lst:resolve()
for i = 1, n do
  b[i] = b[i] + 1LL * zz[i] * a[i]
  local str = tostring(b[i]):gsub("LL", "")
  io.write(str)
  io.write(i == n and "\n" or " ")
end
0