結果

問題 No.1435 Mmm......
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-24 15:26:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 907 ms / 2,000 ms
コード長 2,140 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 171,764 KB
最終ジャッジ日時 2024-05-05 05:13:44
合計ジャッジ時間 15,909 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,424 KB
testcase_01 AC 41 ms
55,680 KB
testcase_02 AC 41 ms
55,740 KB
testcase_03 AC 45 ms
62,464 KB
testcase_04 AC 42 ms
55,296 KB
testcase_05 AC 42 ms
55,680 KB
testcase_06 AC 512 ms
101,120 KB
testcase_07 AC 529 ms
104,884 KB
testcase_08 AC 599 ms
111,772 KB
testcase_09 AC 642 ms
108,288 KB
testcase_10 AC 574 ms
104,320 KB
testcase_11 AC 569 ms
104,192 KB
testcase_12 AC 529 ms
101,632 KB
testcase_13 AC 511 ms
101,376 KB
testcase_14 AC 434 ms
93,792 KB
testcase_15 AC 712 ms
112,512 KB
testcase_16 AC 612 ms
104,832 KB
testcase_17 AC 374 ms
96,000 KB
testcase_18 AC 712 ms
112,256 KB
testcase_19 AC 519 ms
101,632 KB
testcase_20 AC 659 ms
108,556 KB
testcase_21 AC 281 ms
114,944 KB
testcase_22 AC 404 ms
109,388 KB
testcase_23 AC 808 ms
169,012 KB
testcase_24 AC 787 ms
171,276 KB
testcase_25 AC 795 ms
171,764 KB
testcase_26 AC 907 ms
169,160 KB
testcase_27 AC 833 ms
169,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():

    n = int(input())
    A = list(map(int, input().split()))

    SA = set(A)
    SA = sorted(list(A))
    atoi = {}
    for i, a in enumerate(SA):
        atoi[a] = i

    N = 2*10**5+50

    j = 0
    bit = BIT(N+1)
    from collections import defaultdict
    ans = 0
    for i in range(n):
        while j < n:
            if i == j:
                bit.add(atoi[A[j]], 1)
                j += 1
            else:
                bit.add(atoi[A[j]], 1)
                M = SA[bit.lower_bound(j-i+1)]
                m1 = SA[bit.lower_bound(1)]
                m2 = SA[bit.lower_bound(2)]
                if M <= m1+m2:
                    j += 1
                else:
                    bit.add(atoi[A[j]], -1)
                    break
        ans += j-i-1
        if i == j:
            j += 1
        else:
            bit.add(atoi[A[i]], -1)
    print(ans)

if __name__ == '__main__':
    main()
0