結果

問題 No.992 最長増加部分列の数え上げ
ユーザー neterukunneterukun
提出日時 2020-02-16 04:10:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,998 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 172,480 KB
最終ジャッジ日時 2024-04-16 03:25:37
合計ジャッジ時間 22,526 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
51,840 KB
testcase_01 AC 43 ms
51,968 KB
testcase_02 AC 43 ms
52,224 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def compress(array):
    """座標圧縮したリストを返す"""
    array2 = sorted(set(array))
    memo = {value: index for index, value in enumerate(array2)}
    for i in range(len(array)):
        array[i] = memo[array[i]]
    return array


class SegmentTree():
    """一点加算、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する
    add: i番目にvalをmergeする
    get_sum: 区間[begin, end)のmergeの結果を求める
    
    (最大値, 最大値の個数)のペアはモノイドになる、すごい
    単位元は(0, 1)
    演算についてはmergeを参照
    """
    def __init__(self, n):
        self.n = n
        self.size = 1
        while self.size < n:
            self.size *= 2
        self.node = [(0, 1)] * (2*self.size - 1)

    def add(self, i, val):
        i += (self.size - 1)
        self.node[i] = self.merge(self.node[i], val)
        while i > 0:
            i = (i - 1) // 2
            self.node[i] = self.merge(self.node[2*i + 1], self.node[2*i + 2])

    def get_sum(self, begin, end):
        begin += (self.size - 1)
        end += (self.size - 1)
        s = (0, 1)
        while begin < end:
            if (end - 1) & 1:
                end -= 1
                s = self.merge(s, self.node[end])
            if (begin - 1) & 1:
                s = self.merge(s, self.node[begin])
                begin += 1
            begin = (begin - 1) // 2
            end = (end - 1) // 2
        return s

    def merge(self, a, b):
        """マージする"""
        max_a, cnt_a = a
        max_b, cnt_b = b
        if max_a > max_b:
            return (max_a, cnt_a)
        elif max_a < max_b:
            return (max_b, cnt_b)
        return (max_a, (cnt_a + cnt_b) % MOD)
      
n = int(input())
a = list(map(int, input().split()))
a = compress(a)
MOD = 10 ** 9 + 7

st = SegmentTree(n)
for num in a:
    max_, cnt = st.get_sum(0, num)
    st.add(num, (max_ + 1, cnt))
print(st.get_sum(0, n)[1] % MOD)
0