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()