結果

問題 No.694 square1001 and Permutation 3
ユーザー 👑 obakyanobakyan
提出日時 2021-05-04 22:04:02
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,285 ms / 3,000 ms
コード長 1,385 bytes
コンパイル時間 71 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 31,460 KB
最終ジャッジ日時 2023-09-30 23:40:42
合計ジャッジ時間 8,577 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 583 ms
6,372 KB
testcase_08 AC 723 ms
10,776 KB
testcase_09 AC 1,228 ms
31,460 KB
testcase_10 AC 568 ms
6,292 KB
testcase_11 AC 1,285 ms
31,332 KB
testcase_12 AC 1,244 ms
31,196 KB
testcase_13 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local band = bit.band
local FenwickTree = {}
FenwickTree.create = function(self, n)
  self.n = n
  self.v = {}
  for i = 1, n do self.v[i] = 0 end
end
FenwickTree.add = function(self, pos, val)
  while pos <= self.n do
    self.v[pos] = self.v[pos] + val
    pos = pos + band(pos, -pos)
  end
end
FenwickTree.sum = function(self, r)
  local ret = 0
  while 0 < r do
    ret = ret + self.v[r]
    r = r - band(r, -r)
  end
  return ret
end
FenwickTree.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = FenwickTree})
  obj:create(n)
  return obj
end

local n = io.read("*n")
local c = 0
local a = {}
local amap = {}
for i = 1, n do
  a[i] = io.read("*n")
  amap[a[i]] = true
end
local auniq = {}
for k, _u in pairs(amap) do table.insert(auniq, k) end
table.sort(auniq)
local an = #auniq
for i = 1, an do
  amap[auniq[i]] = i
end
for i = 1, n do
  a[i] = amap[a[i]]
end

local fw = FenwickTree.new(an)
for i = 1, n do
  c = c + fw:sum(a[i])
  fw:add(a[i], 1)
end
c = mfl(n * (n - 1) / 2) - c
print(c)
local acnt = {}
for i = 1, an do acnt[i] = 0 end
for i = 1, n do
  local ai = a[i]
  acnt[ai] = acnt[ai] + 1
end
for i = 2, an do
  acnt[i] = acnt[i] + acnt[i - 1]
end

for i = 1, n - 1 do
  local ai = a[i]
  local largecnt = n - acnt[ai]
  local smallcnt = 1 < ai and acnt[ai - 1] or 0
  c = c + largecnt - smallcnt
  print(c)
end
0