結果

問題 No.694 square1001 and Permutation 3
ユーザー kohei2019kohei2019
提出日時 2022-03-13 09:55:47
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,060 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 272,972 KB
最終ジャッジ日時 2023-10-17 22:27:36
合計ジャッジ時間 13,298 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 44 ms
55,472 KB
testcase_03 AC 70 ms
69,988 KB
testcase_04 AC 72 ms
72,064 KB
testcase_05 AC 72 ms
72,044 KB
testcase_06 AC 72 ms
72,048 KB
testcase_07 AC 741 ms
88,724 KB
testcase_08 AC 1,618 ms
123,836 KB
testcase_09 MLE -
testcase_10 AC 973 ms
86,608 KB
testcase_11 MLE -
testcase_12 MLE -
testcase_13 AC 45 ms
55,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

class SegTree:
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
        'sum': 0,
        'prd': 1,
        'gcd': 0,
        'lmc': 1,
        '^': 0,
        '&': (1 << 60) - 1,
        '|': 0,
    }

    FUNC = {
        'min': min,
        'max': max,
        'sum': (lambda x, y: x + y),
        'prd': (lambda x, y: x * y),
        'gcd': math.gcd,
        'lmc': (lambda x, y: (x * y) // math.gcd(x, y)),
        '^': (lambda x, y: x ^ y),
        '&': (lambda x, y: x & y),
        '|': (lambda x, y: x | y),
    }

    def __init__(self, ls, mode='min', func=None, default=None):
        """
        要素ls, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|)
        func,defaultを指定すれば任意の関数、単位元での計算が可能
        """
        N = len(ls)
        if default == None:
            self.default = self.DEFAULT[mode]
        else:
            self.default = default
        if func == None:
            self.func = self.FUNC[mode]
        else:
            self.func = func
        self.N = N
        self.K = (N - 1).bit_length()
        self.N2 = 1 << self.K
        self.dat = [self.default] * (2**(self.K + 1))
        for i in range(self.N):  # 葉の構築
            self.dat[self.N2 + i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N2 - 1, -1, -1):
            self.dat[j] = self.func(self.dat[j << 1], self.dat[j << 1 | 1])  # 親が持つ条件

    def leafvalue(self, x):  # リストのx番目の値
        return self.dat[x + self.N2]

    def update(self, x, y):  # index(x)をyに変更
        i = x + self.N2
        self.dat[i] = y
        while i > 0:  # 親の値を変更
            i >>= 1
            self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])
        return

    def query(self, L, R):  # [L,R)の区間取得
        L += self.N2
        R += self.N2
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL, self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)

    def __iter__(self):
        for i in range(self.N):
            yield self[i]

    def __getitem__(self, x): return self.leafvalue(x)
    def __setitem__(self, x, val): return self.update(x, val)

import collections
N = int(input())
ls = [int(input()) for i in range(N)]
ls2 = ls[:]
ind = 0
dc = dict()
ls2.sort()
for i in range(N):
    if ls2[i] in dc:
        continue
    dc[ls2[i]] = ind
    ind +=1
cnter = collections.Counter(ls)
l = [0]*(len(dc))
for key,val in dc.items():
    l[val] += cnter[key]
SG = SegTree(l,mode='sum')
ans = 0
for i in range(N):
    m = SG.query(0, dc[ls[i]])
    ans += m
    SG[dc[ls[i]]] -= 1
print(ans)
l = l + [0]
SG = SegTree(l,mode='sum')
for i in range(N-1):
    f = SG.query(0, dc[ls[i]])
    r = SG.query(dc[ls[i]]+1,len(dc)+2)
    ans += r
    ans -= f
    print(ans)
0