結果
問題 | No.619 CardShuffle |
ユーザー |
👑 |
提出日時 | 2021-05-19 20:46:38 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 501 ms / 3,000 ms |
コード長 | 6,131 bytes |
コンパイル時間 | 252 ms |
コンパイル使用メモリ | 5,376 KB |
実行使用メモリ | 18,408 KB |
最終ジャッジ日時 | 2024-10-10 02:30:06 |
合計ジャッジ時間 | 10,929 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 35 |
ソースコード
local mfl, mce = math.floor, math.ceillocal mmi, mma = math.min, math.maxlocal bls, brs = bit.lshift, bit.rshiftlocal mod = 1000000007local function bmul(x, y)local x1, y1 = mfl(x / 31623), mfl(y / 31623)local x0, y0 = x - x1 * 31623, y - y1 * 31623return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % modendlocal function badd(x, y)return (x + y) % modendlocal function bsub(x, y)return x < y and x - y + mod or x - yendlocal 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.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.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.new = function(n, func, emptyvalue)local obj = {}setmetatable(obj, {__index = SegTree})obj:create(n, func, emptyvalue)return objendlocal nn = io.read("*n", "*l")local n = 1 + mfl(nn / 2)local stmul = SegTree.new(n, bmul, 1)local s = io.read()for i = 1, n dolocal a = s:sub(4 * i - 3, 4 * i - 3)a = tonumber(a)stmul:setValue(i, a, true)endstmul:updateAll()local stsum = SegTree.new(n, badd, 0)local stflag = SegTree.new(n + 1, mma, 0)stflag:setValue(n + 1, 1)local curleft = 1for i = 2, n dolocal b = s:sub(i * 4 - 5, i * 4 - 5) == "+"if b thenlocal v = stmul:getRange(curleft, i - 1)stsum:setValue(curleft, v)stflag:setValue(curleft, 1)curleft = iendenddolocal v = stmul:getRange(curleft, n)stsum:setValue(curleft, v)stflag:setValue(curleft, 1)endlocal function setValue(pos, val)stmul:setValue(pos, val)local left = stflag:left_bound(1, 1, pos)local right = stflag:right_bound(1, pos + 1, n + 1) - 1stsum:setValue(left, stmul:getRange(left, right))endlocal function change(x)local curflag = stflag:getRange(x, x)local left = stflag:left_bound(1, 1, x - 1)local right = stflag:right_bound(1, x + 1, n + 1) - 1if curflag == 0 thenstsum:setValue(left, stmul:getRange(left, x - 1))stsum:setValue(x, stmul:getRange(x, right))stflag:setValue(x, 1)elsestsum:setValue(left, stmul:getRange(left, right))stsum:setValue(x, 0)stflag:setValue(x, 0)endendlocal q = io.read("*n", "*l")for iq = 1, q dolocal tp, x, y = io.read():match("(%g) (%d+) (%d+)")x = tonumber(x)y = tonumber(y)if tp == "!" thenif x % 2 == 1 thenx = mfl(x / 2) + 1y = mfl(y / 2) + 1local xv = stmul:getRange(x, x)local yv = stmul:getRange(y, y)setValue(x, yv)setValue(y, xv)elsex = mfl(x / 2)y = mfl(y / 2)x = x + 1y = y + 1if stflag:getRange(x, x) ~= stflag:getRange(y, y) thenchange(x)change(y)endendelsex = mfl(x / 2) + 1y = mfl(y / 2) + 1local xright = stflag:right_bound(1, x + 1, n + 1) - 1if y <= xright thenprint(stmul:getRange(x, y))elselocal v = stmul:getRange(x, xright)local yleft = stflag:left_bound(1, 1, y)v = badd(v, stmul:getRange(yleft, y))if xright + 1 <= yleft - 1 thenv = badd(v, stsum:getRange(xright + 1, yleft - 1))endprint(v)endendend