結果
| 問題 |
No.885 アマリクエリ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-17 12:54:11 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 970 ms / 2,000 ms |
| コード長 | 2,639 bytes |
| コンパイル時間 | 28 ms |
| コンパイル使用メモリ | 6,688 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-07 10:29:02 |
| 合計ジャッジ時間 | 4,437 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
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, n, func, emptyvalue)
self.func, self.emptyvalue = func, emptyvalue
local stagenum, mul = 1, 1
self.cnt, self.stage, self.size = {1}, {{}}, {}
while mul < n 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, n do self.stage[stagenum][i] = i end
for i = n + 1, mul do self.stage[stagenum][i] = n + 1 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.recalc = function(self, idx)
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(n, func, emptyvalue)
local obj = {}
setmetatable(obj, {__index = SegTree})
obj:create(n, func, emptyvalue)
return obj
end
local n = io.read("*n")
local sum = 0
local val = {}
for i = 1, n do
val[i] = io.read("*n")
sum = sum + val[i]
end
val[n + 1] = -1
local st = SegTree.new(n, function(x, y) return val[x] > val[y] and x or y end, n + 1)
local q = io.read("*n")
for i = 1, q do
local x = io.read("*n")
while true do
local idx = st.stage[1][1]
if val[idx] < x then
break
else
local v = val[idx]
sum = sum - v
v = v % x
sum = sum + v
val[idx] = v
st:recalc(idx)
end
end
print(sum)
end