結果

問題 No.2327 Inversion Sum
ユーザー sepa38sepa38
提出日時 2023-05-09 19:39:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 2,656 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 91,644 KB
最終ジャッジ日時 2024-05-04 18:08:18
合計ジャッジ時間 5,712 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
90,624 KB
testcase_01 AC 294 ms
90,700 KB
testcase_02 AC 252 ms
89,016 KB
testcase_03 AC 153 ms
86,592 KB
testcase_04 AC 277 ms
91,084 KB
testcase_05 AC 146 ms
82,792 KB
testcase_06 AC 246 ms
89,488 KB
testcase_07 AC 179 ms
82,620 KB
testcase_08 AC 142 ms
79,528 KB
testcase_09 AC 261 ms
90,772 KB
testcase_10 AC 164 ms
81,456 KB
testcase_11 AC 72 ms
81,604 KB
testcase_12 AC 67 ms
80,256 KB
testcase_13 AC 52 ms
74,368 KB
testcase_14 AC 205 ms
84,668 KB
testcase_15 AC 272 ms
91,644 KB
testcase_16 AC 194 ms
87,012 KB
testcase_17 AC 136 ms
83,456 KB
testcase_18 AC 150 ms
79,472 KB
testcase_19 AC 174 ms
88,960 KB
testcase_20 AC 34 ms
52,864 KB
testcase_21 AC 34 ms
53,120 KB
testcase_22 AC 34 ms
52,480 KB
testcase_23 AC 37 ms
52,864 KB
testcase_24 AC 36 ms
52,608 KB
testcase_25 AC 34 ms
52,736 KB
testcase_26 AC 35 ms
52,480 KB
testcase_27 AC 35 ms
53,120 KB
testcase_28 AC 35 ms
53,120 KB
testcase_29 AC 35 ms
52,992 KB
testcase_30 AC 35 ms
52,864 KB
testcase_31 AC 35 ms
52,608 KB
testcase_32 AC 44 ms
52,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segt:
  def __init__(self, n, ele, calc):
    self.num = 2 ** (n - 1).bit_length()
    self.el = ele
    self.data = [ele] * (2 * self.num)
    self.calc = calc

  def update(self, idx, x):
    idx += self.num - 1
    self.data[idx] = x
    while idx > 0:
      idx = (idx - 1) // 2
      self.data[idx] = self.calc(self.data[2*idx+1], self.data[2*idx+2])
  
  def renew(self, idx, x):
    self.update(idx, self.calc(self.get(idx), x))

  def prod(self, left, right):
    l = left + self.num
    r = right + self.num
    res = self.el
    while l < r:
      if l % 2:
        res = self.calc(res, self.data[l-1])
        l += 1
      if r % 2:
        r -= 1
        res = self.calc(res, self.data[r-1])
      l //= 2
      r //= 2
    return res

  def get(self, idx):
    return self.data[idx+self.num-1]

def add(x, y):
  return x + y


n, m = map(int, input().split())
pk = [list(map(lambda x: int(x)-1, input().split())) for _ in range(m)]
def solve(n, m, pk):
  mod = 998244353
  fac = [1]
  for i in range(n):
    fac.append(fac[i]*(i+1)%mod)
  idx = [-1] * n
  st1 = segt(n, 0, add)
  for p, k in pk:
    st1.update(k, 1)
    idx[p] = k
  st2 = segt(n, 0, add)
  st3 = segt(n, 0, add)
  s = 0
  ans = 0
  for i in range(n):
    #print(i)
    if idx[i] == -1:
      ans += s * fac[n-m-1] % mod
      #print(ans, end = " ")
      cnd = i - st2.prod(0, i)
      ans += cnd * ((n - m) * (n - m - 1) // 2) * fac[n-m-2] % mod
      #print(ans)
    else:
      st2.update(i, 1)
      st3.update(idx[i], 1)
      ans += st3.prod(idx[i]+1, n) * fac[n-m] % mod
      #print(ans, end = " ")
      cnd = i - st2.prod(0, i)
      ans += cnd * ((n - idx[i]) - st1.prod(idx[i], n)) * fac[n-m-1] % mod
      s += idx[i] - st1.prod(0, idx[i])
      #print(ans, s)
    ans %= mod
  return ans


from itertools import permutations
def naive(n, m, pk):
  P = [-1] * n
  for pi, ki in pk:
    P[ki] = pi
  ans = 0
  for p in permutations(range(n)):
    flg = 0
    for i in range(n):
      if P[i] != -1 and p[i] != P[i]:
        flg = 1
    if flg:
      continue
    for i in range(n):
      for j in range(i+1, n):
        ans += p[i] > p[j]
  return ans

print(solve(n, m, pk))

exit()
print(naive(n, m, pk))
print("=" * 10)

import random
for _ in range(100):
  n = random.randint(1, 10)
  usedp = [0] * n
  usedk = [0] * n
  pk = []
  for _ in range(random.randint(0, n*2)):
    p, k = random.randint(0, n-1), random.randint(0, n-1)
    if usedp[p] == 0 and usedk[k] == 0:
      pk.append([p, k])
      usedp[p] = 1
      usedk[k] = 1
  m = len(pk)
  if solve(n, m, pk) != naive(n, m, pk):
    print(n, m)
    for p, k in pk:
      print(p+1, k+1)
    break
0