結果

問題 No.1687 What the Heck?
ユーザー lllllll88938494lllllll88938494
提出日時 2022-09-12 23:10:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,092 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 108,848 KB
最終ジャッジ日時 2023-08-20 02:01:43
合計ジャッジ時間 5,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,168 KB
testcase_01 AC 78 ms
71,136 KB
testcase_02 WA -
testcase_03 AC 78 ms
71,260 KB
testcase_04 AC 79 ms
71,100 KB
testcase_05 AC 78 ms
71,232 KB
testcase_06 AC 76 ms
71,032 KB
testcase_07 AC 210 ms
86,328 KB
testcase_08 AC 211 ms
91,288 KB
testcase_09 WA -
testcase_10 AC 152 ms
82,848 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 311 ms
108,220 KB
testcase_14 AC 307 ms
108,600 KB
testcase_15 AC 316 ms
108,772 KB
testcase_16 AC 303 ms
108,848 KB
testcase_17 AC 122 ms
77,512 KB
testcase_18 AC 300 ms
108,824 KB
testcase_19 AC 101 ms
76,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    
    def lb(self, x):
        sv = 0
        svid = 0 
        topid = pow(2,(self.size).bit_length()-1)

        while topid:
            if svid + topid <= self.size and sv + self.tree[svid+topid] <= x:
                sv += self.tree[svid+topid]
                svid += topid
            topid //= 2
        return svid + 1

n=int(input())
a=list(map(int,input().split()))
bit=Bit(n)
for i in range(1,n+1):
    bit.add(i,1)

x,y=0,0
for i in range(n-1,-1,-1):
    t=bit.lb(bit.sum(a[i]))
    if t == n+1:
        t1=bit.lb(bit.sum(a[i])-1)
        if t1 == a[i]:
            bit.add(a[i],-1)
        else:
            t2=bit.lb(0)
            bit.add(t2,-1)
            y += i+1
    else:
        bit.add(t,-1)
        x += i+1

print(x-y)
0