結果

問題 No.1703 Much Matching
ユーザー gew1fw
提出日時 2025-06-12 14:52:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 1,691 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 218,128 KB
最終ジャッジ日時 2025-06-12 14:55:20
合計ジャッジ時間 8,805 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 2)  # Using 1-based indexing
    
    def update(self, index, value):
        while index <= self.n:
            if self.tree[index] < value:
                self.tree[index] = value
            else:
                break  # No need to proceed further
            index += index & -index
    
    def query(self, index):
        max_val = 0
        while index > 0:
            if self.tree[index] > max_val:
                max_val = self.tree[index]
            index -= index & -index
        return max_val

def main():
    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 = defaultdict(list)
    for _ in range(Q):
        a = int(input[ptr]); ptr +=1
        b = int(input[ptr]); ptr +=1
        pairs[a].append(b)
    
    sorted_x = sorted(pairs.keys())
    ft = FenwickTree(M)
    max_answer = 0
    
    for x in sorted_x:
        ys = sorted(pairs[x])
        temp = {}
        for y in ys:
            if y == 0:
                prev_max = 0
            else:
                prev_max = ft.query(y - 1)
            current_max = prev_max + 1
            temp[y] = current_max
            if current_max > max_answer:
                max_answer = current_max
        # Update Fenwick Tree for each y in this x
        for y in ys:
            current_max = temp[y]
            if ft.query(y) < current_max:
                ft.update(y, current_max)
    
    print(max_answer)

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