#P17066. [ICPC 2017 Shenyang R] Bridge

[ICPC 2017 Shenyang R] Bridge

Problem Description

Consider a 2×n2 \times n grid graph with nodes (x,y)(x, y) where x{0,1}x \in \{0, 1\} and y{1,2,,n}y \in \{1, 2, \dots, n\}. The initial graph has 3n23n - 2 edges connecting all pairs of adjacent nodes.

You need to maintain the graph with two types of different adjustments. The first one, denoted by “1 x0 y0 x1 y11\ x_0\ y_0\ x_1\ y_1”, adds a new edge between the nodes (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1) which did not exist. The second one, denoted by “2 x0 y0 x1 y12\ x_0\ y_0\ x_1\ y_1”, erases an existing edge between the nodes (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1).

It is sure that, for each adjustment, (x0,y0)(x_0, y_0) and (x1,y1)(x_1, y_1) were adjacent in the original grid graph. That is, they either share the same xx coordinate and y0y1=1|y_0 - y_1| = 1, or they share the same yy coordinate and x0x1=1|x_0 - x_1| = 1. After each adjustment, we guarantee the connectedness of the graph, and you need to calculate the number of bridges in the current graph.

Input Format

The first line of input contains an integer TT (1T10011 \le T \le 1001) which is the total number of test cases. For each test case, the first line contains integers nn (1n2000001 \le n \le 200000) and mm (0m2000000 \le m \le 200000); nn indicates the size of the graph and mm is the number of adjustments. Each of the following mm lines contains an adjustment described as above.

Only one case satisfies n+m2000n + m \ge 2000.

Output Format

For each test case, output mm lines, each of which contains the number of bridges.

2
4 8
2 0 3 1 3
2 0 2 1 2
2 0 4 1 4
1 0 2 1 2
1 0 3 1 3
2 0 1 1 1
1 0 4 1 4
2 1 2 1 3
6 2
2 1 2 1 3
2 0 4 0 5
0
0
7
4
2
4
2
4
1
2