結果

問題 No.1703 Much Matching
ユーザー lam6er
提出日時 2025-04-15 21:04:02
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,325 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 82,764 KB
実行使用メモリ 181,392 KB
最終ジャッジ日時 2025-04-15 21:09:38
合計ジャッジ時間 22,793 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32 TLE * 2 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

class FenwickTreeMax:
    def __init__(self, size):
        self.size = size
        self.tree = [0] * (self.size + 1)  # 1-based indexing

    def update(self, idx, value):
        while idx <= self.size:
            if self.tree[idx] < value:
                self.tree[idx] = value
            else:
                break  # No need to proceed further
            idx += idx & -idx

    def query(self, idx):
        res = 0
        while idx > 0:
            res = max(res, self.tree[idx])
            idx -= idx & -idx
        return res

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    M = int(input[ptr])
    ptr += 1
    Q = int(input[ptr])
    ptr += 1
    pairs = []
    for _ in range(Q):
        a = int(input[ptr])
        ptr += 1
        b = int(input[ptr])
        ptr += 1
        pairs.append((a, b))
    # Sort pairs by a ascending, then b descending
    pairs.sort(key=lambda x: (x[0], -x[1]))
    ft = FenwickTreeMax(M)
    ans = 0
    for a, b in pairs:
        # Query the maximum value in [1, b-1]
        current_max = ft.query(b - 1)
        current_length = current_max + 1
        if current_length > ans:
            ans = current_length
        ft.update(b, current_length)
    print(ans)

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