結果

問題 No.900 aδδitivee
ユーザー 👑 obakyanobakyan
提出日時 2021-07-25 10:17:10
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 648 ms / 2,000 ms
コード長 6,823 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 63,624 KB
最終ジャッジ日時 2024-07-20 17:52:04
合計ジャッジ時間 15,264 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 508 ms
60,416 KB
testcase_08 AC 485 ms
60,288 KB
testcase_09 AC 493 ms
60,288 KB
testcase_10 AC 467 ms
60,288 KB
testcase_11 AC 482 ms
60,288 KB
testcase_12 AC 477 ms
60,288 KB
testcase_13 AC 484 ms
60,288 KB
testcase_14 AC 471 ms
60,288 KB
testcase_15 AC 496 ms
60,288 KB
testcase_16 AC 489 ms
60,288 KB
testcase_17 AC 498 ms
60,288 KB
testcase_18 AC 498 ms
60,288 KB
testcase_19 AC 499 ms
60,288 KB
testcase_20 AC 578 ms
60,288 KB
testcase_21 AC 648 ms
60,288 KB
testcase_22 AC 643 ms
63,588 KB
testcase_23 AC 519 ms
63,624 KB
testcase_24 AC 511 ms
63,484 KB
testcase_25 AC 517 ms
63,492 KB
testcase_26 AC 509 ms
63,596 KB
testcase_27 AC 519 ms
63,612 KB
testcase_28 AC 517 ms
63,604 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.initAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.flag[i][j] = self.merge(self.flag[i + 1][j * 2 - 1], self.flag[i + 1][j * 2])
      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, initval, initflag)
  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.flag = {{0}}
  self.lazy = {{lzempty}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
    self.flag[stagenum] = {}
    self.lazy[stagenum] = {}
    for i = 1, mul do
      self.stage[stagenum][i] = empty
      self.flag[stagenum][i] = 0
      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] = initval[i]
    self.flag[stagenum][i] = initflag[i]
  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], self.flag[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

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

local n = io.read("*n")
local edge = {}
local edgemap = {}
local asked = {}
local cpos = {}
local len = {}
local stpos = {}
for i = 1, n do
  edge[i] = {}
  edgemap[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
  stpos[i] = 0
end
len[n + 1] = 1000000007
for i = 1, n - 1 do
  local x, y, w = io.read("*n", "*n", "*n")
  x, y = x + 1, y + 1
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  edgemap[x][y] = w
  edgemap[y][x] = w
end

local stleft = {}
local stright = {}
for i = 1, n do
  stleft[i] = 0
  stright[i] = 0
end

local etpos = {}
local etflag = {}
local etw = {}

local tasks = {1}
local sti = 1
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  if stleft[src] == 0 then
    stleft[src] = sti
  end
  stright[src] = sti
  etpos[sti] = src
  sti = sti + 1
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
for i = 1, 2 * n - 2 do
  local p1 = etpos[i]
  local p2 = etpos[i + 1]
  if len[p1] < len[p2] then
    etflag[i] = 1
    etw[i] = edgemap[p1][p2]
  else
    etflag[i] = -1
    etw[i] = -edgemap[p1][p2]
  end
end

local function merge(a, b) return a + b end
local function lzmerge(a, b) return a + b end
local function lzdevide(a_lz, numer, denom) return a_lz end
local function lzapply(a_val, b_flag, c_lz)
  return a_val + b_flag * c_lz
end

lzst = LazyRangeSeg.new()
lzst:create(2 * n - 2,
  merge, lzmerge, lzdevide, lzapply,
  0, 0, etw, etflag)

local q = io.read("*n")
for iq = 1, q do
  local tp = io.read("*n")
  if tp == 1 then
    local a, x = io.read("*n", "*n")
    a = a + 1
    lzst:setRange(stleft[a], stright[a] - 1, x)
  else
    local a = io.read("*n")
    a = a + 1
    if a == 1 then
      print(0)
    else
      local v = lzst:getRange(1, stleft[a] - 1)
      v = tostring(1LL * v):gsub("LL", "")
      print(v)
    end
  end
end
0