結果

問題 No.877 Range ReLU Query
ユーザー 👑 obakyanobakyan
提出日時 2019-09-07 00:49:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,728 ms / 2,000 ms
コード長 2,891 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 139,648 KB
最終ジャッジ日時 2024-04-25 22:08:25
合計ジャッジ時間 16,368 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 6 ms
6,940 KB
testcase_02 AC 6 ms
6,940 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 5 ms
6,940 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 8 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 6 ms
6,944 KB
testcase_11 AC 1,471 ms
126,464 KB
testcase_12 AC 1,289 ms
117,120 KB
testcase_13 AC 956 ms
85,248 KB
testcase_14 AC 974 ms
84,992 KB
testcase_15 AC 1,719 ms
139,648 KB
testcase_16 AC 1,728 ms
136,320 KB
testcase_17 AC 1,628 ms
133,120 KB
testcase_18 AC 1,728 ms
138,368 KB
testcase_19 AC 1,290 ms
138,624 KB
testcase_20 AC 1,552 ms
138,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, ary, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < #ary do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, #ary do self.stage[stagenum][i] = ary[i] end
  for i = #ary + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  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
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(ary, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(ary, func, emptyvalue)
  return obj
end

local n, q = io.read("*n", "*n")
local a = {}
for i = 1, n do a[i] = {io.read("*n"), i} end
for i = 1, q do
  local _u, l, r, x = io.read("*n", "*n", "*n", "*n")
  a[n + i] = {x, n + i, l, r}
end
table.sort(a, function(x, y) return x[1] > y[1] end)
local stary = {}
for i = 1, n do stary[i] = {0, 0} end
local st = SegTree.new(stary, function(x, y) return {x[1] + y[1], x[2] + y[2]} end, {0, 0})
local z = {}
for i = 1, n + q do
  if a[i][2] <= n then
    st:setValue(a[i][2], {a[i][1], 1})
  else
    local tmp = st:getRange(a[i][3], a[i][4])
    a[i][1] = tmp[1] - a[i][1] * tmp[2]
    table.insert(z, a[i])
  end
end
table.sort(z, function(x, y) return x[2] < y[2] end)
for i = 1, q do
  print(z[i][1])
end
0