結果

問題 No.1435 Mmm......
ユーザー kept1994kept1994
提出日時 2022-05-02 16:17:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 533 ms / 2,000 ms
コード長 7,558 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,572 KB
実行使用メモリ 142,772 KB
最終ジャッジ日時 2023-09-14 12:41:57
合計ジャッジ時間 14,590 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,548 KB
testcase_01 AC 73 ms
71,372 KB
testcase_02 AC 72 ms
71,396 KB
testcase_03 AC 73 ms
71,604 KB
testcase_04 AC 73 ms
71,224 KB
testcase_05 AC 73 ms
71,616 KB
testcase_06 AC 477 ms
120,684 KB
testcase_07 AC 500 ms
124,340 KB
testcase_08 AC 524 ms
128,748 KB
testcase_09 AC 491 ms
126,224 KB
testcase_10 AC 489 ms
123,544 KB
testcase_11 AC 470 ms
122,696 KB
testcase_12 AC 472 ms
121,652 KB
testcase_13 AC 468 ms
121,040 KB
testcase_14 AC 400 ms
105,496 KB
testcase_15 AC 529 ms
129,468 KB
testcase_16 AC 481 ms
124,140 KB
testcase_17 AC 414 ms
108,100 KB
testcase_18 AC 526 ms
129,656 KB
testcase_19 AC 484 ms
121,808 KB
testcase_20 AC 506 ms
127,568 KB
testcase_21 AC 351 ms
139,296 KB
testcase_22 AC 403 ms
128,364 KB
testcase_23 AC 521 ms
142,324 KB
testcase_24 AC 517 ms
142,460 KB
testcase_25 AC 489 ms
142,080 KB
testcase_26 AC 533 ms
142,772 KB
testcase_27 AC 527 ms
142,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys

MOD = 998244353

class SegTree:
    def __init__(self, monoid, bottomList, func, convertLengthToThePowerOf2: bool):
        self.monoid = monoid
        self.func = func
        if convertLengthToThePowerOf2:
            self.actualLen = len(bottomList)
            self.bottomLen = self.getSegLenOfThePowerOf2(len(bottomList))
            self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
            self.segLen = self.bottomLen * 2
            self.tree = [monoid] * self.segLen
        else:
            self.actualLen = len(bottomList)
            self.bottomLen = len(bottomList)
            self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
            self.segLen = self.bottomLen * 2
            self.tree = [monoid] * self.segLen
        self.build(bottomList)

    """
    初期化
    O(self.segLen)
    """
    def build(self, seq):
        # 最下段の初期化
        for i, x in enumerate(seq, self.offset):
            self.tree[i] = x
        # ビルド
        for i in range(self.offset - 1, 0, -1):
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1])

    """
    直近の2べきの長さを算出
    """
    def getSegLenOfThePowerOf2(self, ln: int):
        if ln <= 0:
            return 1
        else:    
            import math
            decimalPart, integerPart = math.modf(math.log2(ln))
            return 2 ** (int(integerPart) + 1)


# ((2, 3), (2, 1), (5, 2)),
# ((2, 3), (2, 1), (5, 2)),                                                                                ((4, 4), (4, 4), (4, 4)), 
# ((2, 1), (2, 0), (2, 1)), ((2, 3), (5, 2), (5, 2)),                                                      ((4, 4), (4, 4), (4, 4)), ((10000000000, 0), (10000000000, 0), (0, 0)),
# ((2, 0), (2, 0), (2, 0)), ((2, 1), (2, 1), (2, 1)), ((5, 2), (5, 2), (5, 2)), ((2, 3), (2, 3), (2, 3)), ((4, 4), (4, 4), (4, 4)), ((10000000000, 0), (10000000000, 0), (0, 0)), ((10000000000, 0), (10000000000, 0), (0, 0)), ((10000000000, 0), (10000000000, 0), (0, 0))]

# |               1               |
# |       2       |       3       |
# |   4   |   5   |   6   |   7   |
# | 8 | 9 | 0 | 1 | 2 | 3 | 4 | 5 | <- 0~7に対応
    """
    一点加算 他演算
    O(log(self.bottomLen))
    """
    def pointAdd(self, i: int, val: int):
        i += self.offset
        self.tree[i] += val
        # self.tree[i] = self.func(self.tree[i], val) <- こっちの方が都度の修正は発生しない。再帰が遅くないか次第。
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子

    """
    一点更新
    O(log(self.bottomLen))
    """
    def pointUpdate(self, i: int, val: int):
        i += self.offset
        self.tree[i] = val
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子

    """
    区間取得
    O(log(self.bottomLen))
    """
    def getRange(self, l: int, r: int):
        l += self.offset
        r += self.offset
        vL = self.monoid
        vR = self.monoid
        while l < r:
            if l & 1:
                vL = self.func(vL, self.tree[l])
                l += 1
            if r & 1:
                r -= 1
                vR = self.func(self.tree[r], vR)
            l >>= 1
            r >>= 1
        return self.func(vL, vR)

    """
    一点取得
    O(log(self.bottomLen))
    """
    def getPoint(self, i: int):
        i += self.offset
        return self.tree[i]

    """
    二分探索
    O(log(self.bottomLen))
    ※ セグ木上の二分探索を使う場合は2べきにすること。
    """
    def queryKthItem(self, K: int):
        # print("セグ木上の二分探索を使う場合は2べきにすること。")
        index = 1
        restK = K
        while index < self.offset:
            if restK <= self.tree[index << 1]:
                index <<= 1
            else:
                restK -= self.tree[index << 1] # 左に進む場合は右側の分を差し引く。
                index <<= 1
                index += 1
        return index - self.offset

    def max_right(self, l, is_ok: "function"):
        # print("---", l)
        l += self.offset
        ll = l // (l & -l) # lから始まる含む最も大きいセグメントのインデックス算出。(= 2で割れなくなるまで割る)
        ans = self.monoid
        # print("#", ll)
        # print(self.tree)
        # print(is_ok(self.func(ans, self.tree[ll])))
        while is_ok(self.func(ans, self.tree[ll])): # そのセグメントが条件を満たすかどうかの判定
            # print("#", ll)
            ans = self.func(ans, self.tree[ll])
            ll += 1
            while ~ll & 1: # llの反転 ~ll = -(ll+1)
                ll >>= 1 # lから始まる含む最も大きいセグメントのインデックス算出。(= 2で割れなくなるまで割る)
            if ll == 1: # 最上層まで到達したら全範囲満たすということ。 → (2べきになるようにモノイド埋めする前の)実際の長さを返す。
                return self.actualLen
        # print(ll, "self.offset", self.offset)
        while ll < self.offset:
            ll <<= 1 # 一階層下のセグメントへ移動 (=2倍)
            # print(">", ll)
            # print(ans, self.tree[ll], self.func(ans, self.tree[ll]))
            # print(is_ok(self.func(ans, self.tree[ll])))
            if is_ok(self.func(ans, self.tree[ll])): # 条件を満たすなら同一階層の隣のセグメントの下層へ。満たさないならそのまま下層へ。
                ans = self.func(ans, self.tree[ll])
                ll += 1
        # print("!", ll)
        return ll - self.offset
  
def main():
    N = int(input())
    A = list(map(int, input().split()))

    def f(x, y):
        if x[0][0] < y[0][0]:
            m1 = x[0]
            if m1 == x[1]:
                if y[0][0] == 10 ** 10:
                    m2 = x[1]
                else:
                    m2 = y[0]
            else:
                if x[1][0] < y[0][0]:
                    m2 = x[1]
                else:
                    m2 = y[0]
        else:
            m1 = y[0]
            if m1 == y[1]:
                if x[0][0] == 10 ** 10:
                    m2 = y[1]
                else:
                    m2 = x[0]
            else:
                if y[1][0] < x[0][0]:
                    m2 = y[1]
                else:
                    m2 = x[0]
        if x[2][0] > y[2][0]:
            M = x[2]
        else:
            M = y[2]
        return (m1, m2, M)
    
    seg = SegTree(((10 ** 10, 0), (10 ** 10, 0), (0, 0)), [((aa, i), (aa, i), (aa, i)) for i, aa in enumerate(A)], f, convertLengthToThePowerOf2=True)
    # print(seg.tree)

    ans = 0
    for l in range(N):
        num = seg.max_right(l,lambda x:x[2][0]<=x[0][0]+x[1][0]) - l - 1
        # print(num)
        ans += num
        # print("---")
    print(ans)

    # for l in range(N):
    #     print(seg.max_right(l,lambda x:x[2]<=x[0]+x[1]))
    return

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