結果

問題 No.1703 Much Matching
ユーザー gew1fw
提出日時 2025-06-12 18:04:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,547 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 289,820 KB
最終ジャッジ日時 2025-06-12 18:05:27
合計ジャッジ時間 24,741 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32 TLE * 2 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

class Fenwick2DMax:
    def __init__(self, max_x, max_y):
        self.n = max_x
        self.m = max_y
        self.tree = [[0] * (self.m + 1) for _ in range(self.n + 1)]
    
    def update(self, x, y, value):
        i = x
        while i <= self.n:
            j = y
            while j <= self.m:
                if value > self.tree[i][j]:
                    self.tree[i][j] = value
                else:
                    break  # No need to proceed further as existing value is larger
                j += j & -j
            i += i & -i
    
    def query(self, x, y):
        res = 0
        i = x
        while i > 0:
            j = y
            while j > 0:
                if self.tree[i][j] > res:
                    res = self.tree[i][j]
                j -= j & -j
            i -= i & -i
        return res

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    M = int(input[idx]); idx +=1
    Q = int(input[idx]); idx +=1
    pairs = []
    for _ in range(Q):
        a = int(input[idx]); idx +=1
        b = int(input[idx]); idx +=1
        pairs.append( (a, b) )
    # Sort the pairs by a, then by b
    pairs.sort(key=lambda x: (x[0], x[1]))
    fenwick = Fenwick2DMax(N, M)
    max_ans = 0
    for a, b in pairs:
        current_max = fenwick.query(a-1, b-1)
        current_dp = current_max + 1
        fenwick.update(a, b, current_dp)
        if current_dp > max_ans:
            max_ans = current_dp
    print(max_ans)

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