#P9598. [JOI Open 2018] 山体滑坡 / Collapse

[JOI Open 2018] 山体滑坡 / Collapse

Problem Description

Mr. I has a CC-day cable construction plan. The plan for day (i+1) (0iC1)(i+1)\ (0\le i\le C-1) is given by three integers Ti,Xi,YiT_i, X_i, Y_i, which mean:

  • If Ti=0T_i=0, they will install a cable connecting town XiX_i and town YiY_i. It is guaranteed that at the beginning of day (i+1)(i+1), there is no cable between towns XiX_i and YiY_i.
  • If Ti=1T_i=1, they will remove a cable connecting town XiX_i and town YiY_i. It is guaranteed that at the beginning of day (i+1)(i+1), there is a cable between towns XiX_i and YiY_i.

Landslides often happen in the country of JOI. If a landslide happens between towns xx and x+1 (0xN2)x+1\ (0\le x\le N-2), then only cables whose two endpoints are both numbered at most xx, or both numbered at least x+1x+1, are usable. In JOI, whenever a landslide happens, they choose some towns to build base stations. The base stations must satisfy that for every town, it can be connected to a base station through some usable cables.

During the construction phase, Mr. I is already thinking about how many base stations should be built when a landslide happens. He has QQ questions. The (j+1)(j+1)-th question is given by two integers Wj,PjW_j, P_j, meaning that he wants to know: after day (Wj+1)(W_j+1) ends, if a landslide happens between towns PjP_j and Pj+1P_j+1, what is the minimum number of base stations that should be built.

As Mr. I’s assistant, you are asked to write a program to answer Mr. I’s questions.

Input Format

On LOJ this is an interactive problem. For convenience, here it is judged in the traditional (non-interactive) way.

The first line contains three integers N,C,QN, C, Q.

The next CC lines each contain three integers Ti,Xi,YiT_i, X_i, Y_i.

The next QQ lines each contain two integers Wj,PjW_j, P_j.

Output Format

Output QQ lines. The (j+1)(j+1)-th line should output DjD_j, the answer to the (j+1)(j+1)-th question.

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

3
2

Hint

Sample

Consider the case with 55 towns. In what follows, (x,y)(x,y) represents a cable connecting town xx and town yy.

  • Suppose that when there are four cables (0,1),(1,3),(2,4),(4,0)(0,1),(1,3),(2,4),(4,0), a landslide happens between towns 11 and 22. Cables (1,3)(1,3) and (4,0)(4,0) become unusable, so the usable cables are (0,1)(0,1) and (2,4)(2,4). You need to build base stations in towns 0,2,30,2,3. The minimum number of base stations is 33.
  • Suppose that when there are six cables (0,1),(0,3),(1,2),(2,4),(4,0)(0, 1), (0, 3), (1, 2), (2, 4), (4, 0) and (4,3)(4,3), a landslide happens between towns 33 and 44. Cables (2,4),(4,0)(2,4),(4,0) and (4,3)(4,3) become unusable, so the usable cables are (0,1),(0,3)(0,1),(0,3) and (1,2)(1,2). You need to build base stations in towns 00 and 44. The minimum number of base stations is 22.

Constraints

This problem has four subtasks. The scores and additional constraints are shown in the table below:

Subtask ID Score NN C,QC,Q Additional Constraints
11 55 2N5 0002\le N\le 5\ 000 1C,Q5 0001\le C,Q\le 5\ 000 None
22 3030 2N100 0002\le N\le 100\ 000 1C,Q100 0001\le C,Q\le 100\ 000 All Pj (0jQ1)P_j\ (0\le j\le Q-1) are equal
33 Ti=0 (0iC1)T_i=0\ (0\le i\le C-1)
44 3535 None

Translated by ChatGPT 5