結果

問題 No.1300 Sum of Inversions
ユーザー 👑 obakyanobakyan
提出日時 2020-11-30 09:45:38
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,141 ms / 2,000 ms
コード長 4,109 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 37,412 KB
最終ジャッジ日時 2023-10-11 02:46:25
合計ジャッジ時間 27,443 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 870 ms
37,304 KB
testcase_04 AC 821 ms
37,404 KB
testcase_05 AC 656 ms
19,884 KB
testcase_06 AC 969 ms
37,412 KB
testcase_07 AC 942 ms
37,360 KB
testcase_08 AC 1,079 ms
37,180 KB
testcase_09 AC 1,058 ms
37,372 KB
testcase_10 AC 517 ms
19,912 KB
testcase_11 AC 522 ms
19,868 KB
testcase_12 AC 838 ms
37,276 KB
testcase_13 AC 818 ms
37,208 KB
testcase_14 AC 1,141 ms
37,348 KB
testcase_15 AC 1,037 ms
37,244 KB
testcase_16 AC 861 ms
37,352 KB
testcase_17 AC 462 ms
19,856 KB
testcase_18 AC 568 ms
19,928 KB
testcase_19 AC 690 ms
37,272 KB
testcase_20 AC 760 ms
37,276 KB
testcase_21 AC 712 ms
37,332 KB
testcase_22 AC 616 ms
20,020 KB
testcase_23 AC 971 ms
37,244 KB
testcase_24 AC 688 ms
19,976 KB
testcase_25 AC 550 ms
19,764 KB
testcase_26 AC 546 ms
19,860 KB
testcase_27 AC 613 ms
19,928 KB
testcase_28 AC 1,113 ms
37,360 KB
testcase_29 AC 756 ms
37,280 KB
testcase_30 AC 1,051 ms
37,288 KB
testcase_31 AC 657 ms
19,860 KB
testcase_32 AC 709 ms
19,792 KB
testcase_33 AC 61 ms
12,772 KB
testcase_34 AC 86 ms
12,720 KB
testcase_35 AC 654 ms
33,180 KB
testcase_36 AC 722 ms
37,340 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 = 998244353
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + 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.addValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = self.func(self.stage[self.stagenum][idx], value)
  self:update(idx)
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 a = {}
local ainv = {}
for i = 1, n do
  a[i] = io.read("*n")
  if not ainv[a[i]] then ainv[a[i]] = 1
  else ainv[a[i]] = ainv[a[i]] + 1
  end
end
local asorted = {}
for k, _u in pairs(ainv) do table.insert(asorted, k) end
table.sort(asorted)
for i = 1, #asorted do
  ainv[asorted[i]] = i
end
local m = #asorted
local stcnt = SegTree.new(m, function(a, b) return a + b end, 0)
local stsum = SegTree.new(m, function(a, b) return badd(a, b) end, 0)
local asum = 0
for i = 1, n do
  asum = badd(asum, a[i])
end
local leftcnt, leftsum = {}, {}
for i = 1, n do
  local v = a[i]
  local vidx = ainv[v]
  if vidx < m then
    leftcnt[i] = stcnt:getRange(vidx + 1, m)
    leftsum[i] = stsum:getRange(vidx + 1, m)
  else
    leftcnt[i] = 0
    leftsum[i] = 0
  end
  stcnt:addValue(vidx, 1)
  stsum:addValue(vidx, v)
end
local rightcnt, rightsum = {}, {}
for i = 1, n do rightcnt[i], rightsum[i] = 0, 0 end
for i = 1, m do
  stcnt:setValue(i, 0, true)
  stsum:setValue(i, 0, true)
end
stcnt:updateAll()
stsum:updateAll()
for i = n, 1, -1 do
  local v = a[i]
  local vidx = ainv[v]
  if 1 < vidx then
    rightcnt[i] = stcnt:getRange(1, vidx - 1)
    rightsum[i] = stsum:getRange(1, vidx - 1)
  else
    rightcnt[i], rightsum[i] = 0, 0
  end
  stcnt:addValue(vidx, 1)
  stsum:addValue(vidx, v)
end
local ret = 0
for i = 1, n do
  local center = bmul(a[i], bmul(leftcnt[i], rightcnt[i]))
  local left = bmul(leftsum[i], rightcnt[i])
  local right = bmul(leftcnt[i], rightsum[i])
  ret = badd(ret, center + left + right)
end
print(ret)
0