結果

問題 No.1300 Sum of Inversions
ユーザー 👑 obakyanobakyan
提出日時 2020-11-30 09:45:38
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 4,109 bytes
コンパイル時間 117 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 37,832 KB
最終ジャッジ日時 2024-09-13 02:17:22
合計ジャッジ時間 26,505 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 866 ms
37,824 KB
testcase_04 AC 817 ms
37,696 KB
testcase_05 AC 647 ms
20,420 KB
testcase_06 AC 932 ms
37,824 KB
testcase_07 AC 897 ms
37,824 KB
testcase_08 AC 1,030 ms
37,828 KB
testcase_09 AC 1,003 ms
37,828 KB
testcase_10 AC 509 ms
20,420 KB
testcase_11 AC 517 ms
20,352 KB
testcase_12 AC 812 ms
37,812 KB
testcase_13 AC 840 ms
37,828 KB
testcase_14 AC 1,122 ms
37,828 KB
testcase_15 AC 1,028 ms
37,828 KB
testcase_16 AC 858 ms
37,828 KB
testcase_17 AC 495 ms
20,420 KB
testcase_18 AC 601 ms
20,416 KB
testcase_19 AC 722 ms
37,824 KB
testcase_20 AC 750 ms
37,824 KB
testcase_21 AC 756 ms
37,828 KB
testcase_22 AC 645 ms
20,288 KB
testcase_23 AC 976 ms
37,824 KB
testcase_24 AC 668 ms
20,196 KB
testcase_25 AC 539 ms
20,416 KB
testcase_26 AC 543 ms
20,292 KB
testcase_27 AC 598 ms
20,420 KB
testcase_28 AC 1,047 ms
37,824 KB
testcase_29 AC 712 ms
37,832 KB
testcase_30 AC 1,015 ms
37,700 KB
testcase_31 AC 620 ms
20,424 KB
testcase_32 AC 705 ms
20,292 KB
testcase_33 AC 64 ms
13,184 KB
testcase_34 AC 81 ms
13,056 KB
testcase_35 AC 637 ms
33,732 KB
testcase_36 AC 710 ms
37,828 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