結果

問題 No.1435 Mmm......
ユーザー tamatotamato
提出日時 2021-03-20 01:08:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,322 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 138,076 KB
最終ジャッジ日時 2024-04-29 20:56:56
合計ジャッジ時間 20,708 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,480 KB
testcase_01 AC 40 ms
52,352 KB
testcase_02 AC 40 ms
52,736 KB
testcase_03 AC 44 ms
52,864 KB
testcase_04 AC 40 ms
52,736 KB
testcase_05 AC 42 ms
52,608 KB
testcase_06 AC 701 ms
120,764 KB
testcase_07 AC 839 ms
123,192 KB
testcase_08 AC 937 ms
126,396 KB
testcase_09 AC 952 ms
124,412 KB
testcase_10 AC 797 ms
122,212 KB
testcase_11 AC 790 ms
122,692 KB
testcase_12 AC 739 ms
121,280 KB
testcase_13 AC 691 ms
120,720 KB
testcase_14 AC 529 ms
103,032 KB
testcase_15 AC 990 ms
127,476 KB
testcase_16 AC 838 ms
123,536 KB
testcase_17 AC 614 ms
105,092 KB
testcase_18 AC 912 ms
127,288 KB
testcase_19 AC 723 ms
122,008 KB
testcase_20 AC 874 ms
125,368 KB
testcase_21 AC 635 ms
136,972 KB
testcase_22 AC 1,322 ms
127,524 KB
testcase_23 AC 895 ms
137,940 KB
testcase_24 AC 903 ms
137,940 KB
testcase_25 AC 874 ms
138,072 KB
testcase_26 AC 898 ms
138,068 KB
testcase_27 AC 880 ms
138,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
inf = 10**10


def main():
    import sys
    input = sys.stdin.readline

    def segf(A, B):
        m1a, m2a, Ma = A
        m1b, m2b, Mb = B
        m1, m2 = sorted([m1a, m2a, m1b, m2b])[:2]
        M = max(Ma, Mb)
        return [m1, m2, M]

    def check(A):
        return A[0] + A[1] < A[2]

    class SegmentTree:
        def __init__(self, A, initialize=True, segfunc=segf, ident=[inf, inf, -inf]):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.segfunc = segfunc
            self.ident = ident
            if initialize:
                self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.ident] * (self.N0 * 2)

        def update(self, i, x):
            i += self.N0 - 1
            self.data[i] = x
            for _ in range(self.LV):
                i >>= 1
                self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

        def get(self, i):
            return self.data[i + self.N0 - 1]

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            ret_l = self.ident
            ret_r = self.ident
            while l < r:
                if l & 1:
                    ret_l = self.segfunc(ret_l, self.data[l])
                    l += 1
                if r & 1:
                    ret_r = self.segfunc(self.data[r - 1], ret_r)
                    r -= 1
                l >>= 1
                r >>= 1
            return self.segfunc(ret_l, ret_r)

        # return smallest i(l <= i < r) s.t. check(A[i]) == True
        def binsearch(self, l, r, check):
            if not check(self.query(l, r)):
                return r
            l += self.N0 - 1
            val = self.ident
            while True:
                if check(self.segfunc(val, self.data[l])):
                    break
                if l & 1:
                    val = self.segfunc(val, self.data[l])
                    l += 1
                l >>= 1
            while l < self.N0:
                newval = self.segfunc(val, self.data[l * 2])
                if not check(newval):
                    val = newval
                    l = (l << 1) + 1
                else:
                    l <<= 1
            return l - self.N0 + 1

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

    AA = [[a, inf, a] for a in A]
    ST = SegmentTree(AA)
    ans = 0
    for l in range(1, N):
        r = ST.binsearch(l, N+1, check)
        ans += r - l - 1
    print(ans)


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