結果

問題 No.956 Number of Unbalanced
ユーザー maspymaspy
提出日時 2020-05-09 21:15:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 590 ms / 2,000 ms
コード長 2,648 bytes
コンパイル時間 670 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 128,532 KB
最終ジャッジ日時 2023-09-20 09:20:27
合計ジャッジ時間 14,730 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
77,120 KB
testcase_01 AC 83 ms
77,180 KB
testcase_02 AC 83 ms
77,312 KB
testcase_03 AC 82 ms
77,108 KB
testcase_04 AC 81 ms
77,292 KB
testcase_05 AC 83 ms
76,992 KB
testcase_06 AC 495 ms
98,412 KB
testcase_07 AC 507 ms
99,840 KB
testcase_08 AC 487 ms
97,796 KB
testcase_09 AC 446 ms
99,408 KB
testcase_10 AC 460 ms
98,752 KB
testcase_11 AC 541 ms
97,744 KB
testcase_12 AC 519 ms
98,232 KB
testcase_13 AC 353 ms
128,368 KB
testcase_14 AC 590 ms
99,848 KB
testcase_15 AC 346 ms
122,688 KB
testcase_16 AC 465 ms
99,356 KB
testcase_17 AC 366 ms
121,328 KB
testcase_18 AC 584 ms
99,340 KB
testcase_19 AC 345 ms
123,980 KB
testcase_20 AC 583 ms
99,500 KB
testcase_21 AC 353 ms
128,532 KB
testcase_22 AC 369 ms
121,664 KB
testcase_23 AC 505 ms
99,584 KB
testcase_24 AC 343 ms
124,180 KB
testcase_25 AC 387 ms
96,432 KB
testcase_26 AC 359 ms
96,548 KB
testcase_27 AC 404 ms
96,468 KB
testcase_28 AC 395 ms
96,292 KB
testcase_29 AC 583 ms
99,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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
        self.history = []
        
    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 clear(self):
        for i in self.history:
            while i < self.size:
                self.data[i] = 0
                i += i & -i
        self.history = []

    def add(self, i, x):
        self.history.append(i)
        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

N, *A = map(int, read().split())

U = 10 ** 5
ind = [[] for _ in range(U+1)]
for i,x in enumerate(A):
    ind[x].append(i)

def solve(N, I, bit0, bit1, bit2):
    if not I:
        return 0
    
    def range_add(i,j):
        bit2.add(i, 1)
        bit1.add(i, -2*i+3)
        bit0.add(i, (i-1) * (i-2))
        j += 1
        bit2.add(j, -1)
        bit1.add(j, 2*j-3)
        bit0.add(j, -(j-1) * (j-2))
    
    def range_sum(i,j):
        x = bit0.get_sum(j) + bit1.get_sum(j) * j + bit2.get_sum(j) * j * j
        i -= 1
        x -= bit0.get_sum(i) + bit1.get_sum(i) * i + bit2.get_sum(i) * i * i
        return x
    
    ret = 0
    x = N + 5
    i = I[0]
    range_add(x-i,x)
    x -= i
    inv = 0
    for i,j in zip(I, itertools.chain(I[1:], [N])):
        x += 1  # at i
        n = j - i
        inv += range_sum(x-n,x-1)
        range_add(x-n+1,x)
        x -= n - 1
    return inv // 2

x = 0
bit0 = BinaryIndexedTree([0] * (N+N+10))
bit1 = BinaryIndexedTree([0] * (N+N+10))
bit2 = BinaryIndexedTree([0] * (N+N+10))
for I in ind:
    x += solve(N,I, bit0, bit1, bit2)
    bit0.clear()
    bit1.clear()
    bit2.clear()
print(x)
0