結果

問題 No.619 CardShuffle
ユーザー 👑 obakyanobakyan
提出日時 2021-05-19 20:46:38
言語 Lua
(LuaJit 2.1.1696795921)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 5 ms
5,248 KB
testcase_02 AC 4 ms
5,248 KB
testcase_03 AC 5 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 3 ms
5,248 KB
testcase_06 AC 4 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 5 ms
5,248 KB
testcase_09 AC 3 ms
5,248 KB
testcase_10 AC 4 ms
5,248 KB
testcase_11 AC 3 ms
5,248 KB
testcase_12 AC 3 ms
5,248 KB
testcase_13 AC 3 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 3 ms
5,248 KB
testcase_16 AC 451 ms
18,080 KB
testcase_17 AC 353 ms
17,952 KB
testcase_18 AC 439 ms
18,100 KB
testcase_19 AC 345 ms
17,956 KB
testcase_20 AC 437 ms
17,920 KB
testcase_21 AC 477 ms
18,252 KB
testcase_22 AC 359 ms
18,012 KB
testcase_23 AC 480 ms
18,124 KB
testcase_24 AC 358 ms
17,920 KB
testcase_25 AC 501 ms
17,904 KB
testcase_26 AC 443 ms
18,408 KB
testcase_27 AC 346 ms
18,052 KB
testcase_28 AC 426 ms
18,212 KB
testcase_29 AC 354 ms
18,048 KB
testcase_30 AC 333 ms
18,012 KB
testcase_31 AC 2 ms
5,248 KB
testcase_32 AC 448 ms
17,920 KB
testcase_33 AC 460 ms
18,116 KB
testcase_34 AC 247 ms
14,740 KB
testcase_35 AC 101 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local mod = 1000000007

local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt 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.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  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
  for i = 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 stagenum = self.stagenum
  local ret = self.emptyvalue
  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.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.update = function(self, idx)
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    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.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    self:update(idx)
  end
end
SegTree.left_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    local tmp = self.func(ret, self.stage[stage][mfl(r / sz)])
    if tmp < val then
      ret, retpos = tmp, r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end
SegTree.right_bound = function(self, val, left, right)
  local ret, retpos = self.emptyvalue, left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
    if tmp < val then
      ret, retpos = tmp, l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local 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 do
  local a = s:sub(4 * i - 3, 4 * i - 3)
  a = tonumber(a)
  stmul:setValue(i, a, true)
end
stmul:updateAll()
local stsum = SegTree.new(n, badd, 0)
local stflag = SegTree.new(n + 1, mma, 0)
stflag:setValue(n + 1, 1)
local curleft = 1
for i = 2, n do
  local b = s:sub(i * 4 - 5, i * 4 - 5) == "+"
  if b then
    local v = stmul:getRange(curleft, i - 1)
    stsum:setValue(curleft, v)
    stflag:setValue(curleft, 1)
    curleft = i
  end
end
do
  local v = stmul:getRange(curleft, n)
  stsum:setValue(curleft, v)
  stflag:setValue(curleft, 1)
end

local 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) - 1
  stsum:setValue(left, stmul:getRange(left, right))
end

local 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) - 1
  if curflag == 0 then
    stsum:setValue(left, stmul:getRange(left, x - 1))
    stsum:setValue(x, stmul:getRange(x, right))
    stflag:setValue(x, 1)
  else
    stsum:setValue(left, stmul:getRange(left, right))
    stsum:setValue(x, 0)
    stflag:setValue(x, 0)
  end
end

local q = io.read("*n", "*l")
for iq = 1, q do
  local tp, x, y = io.read():match("(%g) (%d+) (%d+)")
  x = tonumber(x)
  y = tonumber(y)
  if tp == "!" then
    if x % 2 == 1 then
      x = mfl(x / 2) + 1
      y = mfl(y / 2) + 1
      local xv = stmul:getRange(x, x)
      local yv = stmul:getRange(y, y)
      setValue(x, yv)
      setValue(y, xv)
    else
      x = mfl(x / 2)
      y = mfl(y / 2)
      x = x + 1
      y = y + 1
      if stflag:getRange(x, x) ~= stflag:getRange(y, y) then
        change(x)
        change(y)
      end
    end
  else
    x = mfl(x / 2) + 1
    y = mfl(y / 2) + 1
    local xright = stflag:right_bound(1, x + 1, n + 1) - 1
    if y <= xright then
      print(stmul:getRange(x, y))
    else
      local 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 then
        v = badd(v, stsum:getRange(xright + 1, yleft - 1))
      end
      print(v)
    end
  end
end
0