Under construction

Still being built. Things may change

Collapse

Time limit6sMemory limit512 MB

Summary
For each query (W, P), count the connected components among towns 0 to P and among towns P+1 to N-1, using only the cables that stay within each side on day W+1.
Level

Medium7 of 10

Topics
Graph, Union-find, Segment tree, Divide and conquer
Solved
No attempts yet

Problem

There are NN towns along a deep linear valley in JOI Country. The towns are numbered 0,1,…,N−10, 1, \ldots, N-1 in order of distance from the sea. Mr. I, the chair of the JOI Country Scientific Committee, maintains bidirectional communication cables between the towns. At the start, there are no cables.

Mr. I has a cable construction plan for CC days. The plan for day i+1i+1 (0≤i≤C−10 \le i \le C-1) is given by three integers TiT_i, XiX_i, YiY_i:

  • If Ti=0T_i = 0, they build a cable between towns XiX_i and YiY_i. No cable between these two towns exists at the start of that day.
  • If Ti=1T_i = 1, they remove the cable between towns XiX_i and YiY_i. That cable exists at the start of that day.

Cliff collapse often happens in JOI Country. If a collapse happens between town xx and town x+1x+1 (0≤x≤N−20 \le x \le N-2), every cable connecting a town numbered at most xx with a town numbered at least x+1x+1 becomes unavailable. When a collapse happens, they choose some towns to install base stations. The base stations must be placed so that every town can reach a base station by following available cables.

Mr. I has QQ questions. Question j+1j+1 is given by two integers WjW_j, PjP_j. It asks for the minimum number of base stations needed if a collapse happens between town PjP_j and town Pj+1P_j+1 at the end of day Wj+1W_j+1.

Input

The first line contains NN, CC, and QQ. Each of the next CC lines contains TiT_i, XiX_i, YiY_i. Each of the next QQ lines contains WjW_j and PjP_j.

Output

Print QQ lines. The (j+1)(j+1)-th line is the answer to the (j+1)(j+1)-th question.

Hint

Consider 5 towns. Below, (x,y)(x, y) denotes a cable connecting towns xx and yy.

  • Suppose there are 4 cables (0,1)(0, 1), (1,3)(1, 3), (2,4)(2, 4), and (4,0)(4, 0) when a collapse happens between towns 1 and 2. Cables (1,3)(1, 3) and (4,0)(4, 0) become unavailable, leaving (0,1)(0, 1) and (2,4)(2, 4). The minimum is 3, with base stations at towns 0, 2, and 3.
  • Suppose there are 6 cables (0,1)(0, 1), (0,3)(0, 3), (1,2)(1, 2), (2,4)(2, 4), (4,0)(4, 0), and (4,3)(4, 3) when a collapse happens between towns 3 and 4. Cables (2,4)(2, 4), (4,0)(4, 0), and (4,3)(4, 3) become unavailable, leaving (0,1)(0, 1), (0,3)(0, 3), and (1,2)(1, 2). The minimum is 2, with base stations at towns 0 and 4.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    3
    2