PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000090. Đường ngắn sau khi thêm cung (Shortest Distance After Road Addition Queries I)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường ngắn sau khi thêm cung (Shortest Distance After Road Addition Queries I)

Shortest Distance After Road Addition Queries I

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given cities 0,1,…,n−10,1,\dots,n-1 with initially directed roads i→i+1i\to i+1, each query adds a forward road u→vu\to v where u<vu<v. After each query output the shortest path length in number of edges from city 00 to n−1n-1.

Input

First n,qn,q, followed by qq forward road additions $u v` satisfying 0≤u<v<n0\le u<v<n.

Output

Print one shortest path length per query.

Subtasks

  • Subtask 1 (40 points): 3≤n≤7, 1≤q≤123\le n\le7,\ 1\le q\le12. 0≤u<v<n0\le u<v<n, có thể thêm một cung đã tồn tại; tất cả các cung đều hướng tới chỉ số lớn hơn.

  • Subtask 2 (60 points): 3≤n≤30, 1≤q≤253\le n\le30,\ 1\le q\le25. 0≤u<v<n0\le u<v<n, có thể thêm một cung đã tồn tại; tất cả các cung đều hướng tới chỉ số lớn hơn.

Examples

Input

5 3
0 3
1 4
0 4

Output

2
2
1

Explanation

Adding 0→3 yields the two-edge route 0→3→4; adding 1→4 does not improve it; adding 0→4 gives a one-edge path.