結果

問題 No.696 square1001 and Permutation 5
ユーザー maspymaspy
提出日時 2023-06-30 14:07:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 4,336 ms / 10,000 ms
コード長 1,756 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 24,248 KB
最終ジャッジ日時 2023-09-21 07:39:24
合計ジャッジ時間 17,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,296 ms
24,248 KB
testcase_01 AC 17 ms
8,172 KB
testcase_02 AC 17 ms
8,084 KB
testcase_03 AC 18 ms
8,104 KB
testcase_04 AC 19 ms
8,172 KB
testcase_05 AC 23 ms
8,612 KB
testcase_06 AC 38 ms
9,192 KB
testcase_07 AC 76 ms
9,976 KB
testcase_08 AC 210 ms
11,672 KB
testcase_09 AC 1,093 ms
16,152 KB
testcase_10 AC 4,336 ms
24,232 KB
testcase_11 AC 603 ms
18,616 KB
testcase_12 AC 17 ms
8,188 KB
testcase_13 AC 17 ms
8,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.set_int_max_str_digits(10**6)

N = int(readline())
P = tuple(map(int, read().split()))

class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i

    def find_kth_element(self, k):
        data = self.data
        size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1

bit = BinaryIndexedTree([0] * (N+1))
inv = [0] * N
for i, x in enumerate(P):
    inv[i] = x - 1 - bit.get_sum(x)
    bit.add(x, 1)
P, inv

nums = inv[::-1]
coefs = list(range(1, N+1))

while True:
    n = len(nums)
    if n == 1:
        break
    for i in range(1, n, 2):
        nums[i - 1] += coefs[i - 1] * nums[i]
        coefs[i - 1] *= coefs[i]
    nums = nums[::2]
    coefs = coefs[::2]

print(nums[0] + 1)
0