結果
問題 | No.1332 Range Nearest Query |
ユーザー |
👑 |
提出日時 | 2022-05-11 23:31:38 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 846 ms / 2,500 ms |
コード長 | 4,928 bytes |
コンパイル時間 | 163 ms |
コンパイル使用メモリ | 5,376 KB |
実行使用メモリ | 72,968 KB |
最終ジャッジ日時 | 2024-07-19 09:30:22 |
合計ジャッジ時間 | 28,294 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 48 |
ソースコード
local mfl, mce = math.floor, math.ceillocal mmi, mma = math.min, math.maxlocal bls, brs = bit.lshift, bit.rshiftlocal SegTree = {}SegTree.updateAll = function(self)for i = self.stagenum - 1, 1, -1 dolocal cnt = bls(1, i - 1)for j = 1, cnt doself.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])endendendSegTree.create = function(self, n, func, emptyvalue)self.func, self.emptyvalue = func, emptyvaluelocal stagenum, mul = 1, 1self.stage = {{}}while mul < n domul, stagenum = mul * 2, stagenum + 1self.stage[stagenum] = {}endself.stagenum = stagenumself.left_stage = {}for i = 1, n dolocal sp, sz = 1, bls(1, stagenum - 1)while(i - 1) % sz ~= 0 dosp, sz = sp + 1, brs(sz, 1)endself.left_stage[i] = spendself.sz_stage = {}local tmp, sp = 1, stagenumfor i = 1, n doif tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 endself.sz_stage[i] = spendfor i = 1, mul do self.stage[stagenum][i] = emptyvalue endself:updateAll()endSegTree.getRange = function(self, left, right)if left == right then return self.stage[self.stagenum][left] endlocal stagenum = self.stagenumlocal ret = self.emptyvaluewhile left <= right dolocal stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])local sz = bls(1, stagenum - stage)ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])left = left + szendreturn retendSegTree.update = function(self, idx)for i = self.stagenum - 1, 1, -1 dolocal dst = brs(idx + 1, 1)local rem = dst * 4 - 1 - idxself.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])idx = dstendendSegTree.setValue = function(self, idx, value, silent)self.stage[self.stagenum][idx] = valueif not silent thenself:update(idx)endendSegTree.right_bound = function(self, val, left, right)local ret, retpos = self.emptyvalue, left - 1local l, r = left, rightlocal stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])local stagenum = self.stagenumwhile true dolocal sz = bls(1, stagenum - stage)local tmp = self.func(ret, self.stage[stage][mce(l / sz)])if tmp < val thenret, retpos = tmp, l + sz - 1if retpos == right then break endif l + sz <= r thenl = l + szstage = mma(self.left_stage[l], self.sz_stage[r - l + 1])else break endelseif sz ~= 1 then stage, r = stage + 1, l + sz - 2else break endendendreturn retpos + 1endSegTree.left_bound = function(self, val, left, right)local ret, retpos = self.emptyvalue, right + 1local stage, l, r = 1, left, rightlocal stagenum = self.stagenumwhile true dolocal sz = bls(1, stagenum - stage)while r % sz ~= 0 or r + 1 - l < sz dostage = stage + 1sz = bls(1, stagenum - stage)endlocal tmp = self.func(ret, self.stage[stage][mfl(r / sz)])if tmp < val thenret, retpos = tmp, r - sz + 1if l + sz <= r then stage, l, r = 1, l, r - szelse break endelseif sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, relse break endendendreturn retpos - 1endSegTree.new = function(n, func, emptyvalue)local obj = {}setmetatable(obj, {__index = SegTree})obj:create(n, func, emptyvalue)return objendlocal function comp(a, b)return a < bendlocal function lower_bound(ary, x)local num = #aryif num == 0 then return 1 endif not comp(ary[1], x) then return 1 endif comp(ary[num], x) then return num + 1 endlocal min, max = 1, numwhile 1 < max - min dolocal mid = mfl((min + max) / 2)if comp(ary[mid], x) thenmin = midelsemax = midendendreturn maxendlocal n = io.read("*n")local x = {}local xuniq = {}local tasks = {}for i = 1, n dox[i] = io.read("*n")xuniq[i] = x[i]tasks[i] = {}endtable.sort(xuniq)local xmap = {}for i = 1, n doxmap[xuniq[i]] = iendlocal q = io.read("*n")local l, r, xq = {}, {}, {}local ans = {}local inf = 1000000007for i = 1, q dol[i], r[i], xq[i] = io.read("*n", "*n", "*n")ans[i] = inftable.insert(tasks[r[i]], i)endlocal st = SegTree.new(n, mma, 0)for i = 1, n dolocal xi = x[i]local xpos = xmap[xi]st:setValue(xpos, i)for j = 1, #tasks[i] dolocal iq = tasks[i][j]local li = l[iq]local xqi = xq[iq]local xqpos = lower_bound(xuniq, xqi)local ret = infif 1 < xqpos thenlocal left = st:left_bound(li, 1, xqpos - 1)if 1 <= left thenans[iq] = mmi(ans[iq], xqi - xuniq[left])endendif xqpos <= n thenlocal right = st:right_bound(li, xqpos, n)if right <= n thenans[iq] = mmi(ans[iq], xuniq[right] - xqi)endendendendprint(table.concat(ans, "\n"))