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

#SRD0000122. Sạt lở đường cáp (Collapse)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sạt lở đường cáp (Collapse)

Collapse

Source: JOI

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN towns, numbered 0,1,…,N−10,1,\ldots,N-1 along a valley. Initially, no communication cables exist. Over CC days, a cable construction plan is carried out. On day i+1i+1 (0≤i<C0\le i<C), the operation is Ti,Xi,YiT_i,X_i,Y_i:

  • If Ti=0T_i=0, install an undirected cable between XiX_i and YiY_i. That cable is guaranteed to be absent at the start of the day.
  • If Ti=1T_i=1, remove the cable between XiX_i and YiY_i. That cable is guaranteed to be present at the start of the day.

Suppose a landslide occurs between consecutive towns pp and p+1p+1. Every cable connecting a town in {0,…,p}\{0,\ldots,p\} to a town in {p+1,…,N−1}\{p+1,\ldots,N-1\} becomes unusable. Other cables remain usable.

A base station installed in a town can serve every town in its connected component using the remaining cables. Find the minimum number of base stations needed to serve all towns.

Each of the QQ questions gives Wj,PjW_j,P_j: after day Wj+1W_j+1 is completed, suppose a landslide occurs between towns PjP_j and Pj+1P_j+1. Compute the minimum number of base stations. Each question is hypothetical; it does not alter the cable schedule or other questions.

Input

  • The first line contains N,C,QN,C,Q.
  • The next CC lines contain Ti,Xi,YiT_i,X_i,Y_i for day i+1i+1 (i=0…C−1i=0\ldots C-1).
  • The final QQ lines contain Wj,PjW_j,P_j, in question order.

Output

Print exactly QQ lines. Line jj contains one integer: the minimum required number of base stations for question jj.

Subtasks

  • Subtask 1 (5 points): 2≤N≤50002\le N\le 5000; 1≤C≤50001\le C\le 5000, 1≤Q≤50001\le Q\le 5000; Ti∈{0,1}T_i\in\{0,1\}, 0≤Xi,Yi≤N−10\le X_i,Y_i\le N-1, Xi≠YiX_i\ne Y_i; 0≤Wj≤C−10\le W_j\le C-1, 0≤Pj≤N−20\le P_j\le N-2; an edge can only be added if absent and deleted if present.
  • Subtask 2 (30 points): 2≤N≤1000002\le N\le 100000; 1≤C≤1000001\le C\le 100000, 1≤Q≤1000001\le Q\le 100000; Ti∈{0,1}T_i\in\{0,1\}, 0≤Xi,Yi≤N−10\le X_i,Y_i\le N-1, Xi≠YiX_i\ne Y_i; 0≤Wj≤C−10\le W_j\le C-1, 0≤Pj≤N−20\le P_j\le N-2; an edge can only be added if absent and deleted if present. All PjP_j have the same value.
  • Subtask 3 (30 points): 2≤N≤1000002\le N\le 100000; 1≤C≤1000001\le C\le 100000, 1≤Q≤1000001\le Q\le 100000; Ti∈{0,1}T_i\in\{0,1\}, 0≤Xi,Yi≤N−10\le X_i,Y_i\le N-1, Xi≠YiX_i\ne Y_i; 0≤Wj≤C−10\le W_j\le C-1, 0≤Pj≤N−20\le P_j\le N-2; an edge can only be added if absent and deleted if present. All Ti=0T_i=0 (only additions).
  • Subtask 4 (35 points): 2≤N≤1000002\le N\le 100000; 1≤C≤1000001\le C\le 100000, 1≤Q≤1000001\le Q\le 100000; Ti∈{0,1}T_i\in\{0,1\}, 0≤Xi,Yi≤N−10\le X_i,Y_i\le N-1, Xi≠YiX_i\ne Y_i; 0≤Wj≤C−10\le W_j\le C-1, 0≤Pj≤N−20\le P_j\le N-2; an edge can only be added if absent and deleted if present.

Examples

Sample 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

Output

3
2

Explanation

After day 44 (W=3W=3), the existing cables are (0,1),(1,3),(2,4),(0,4)(0,1),(1,3),(2,4),(0,4). A landslide at P=1P=1 leaves components {0,1}\{0,1\}, {2,4}\{2,4\} and {3}\{3\}, so 33 stations are needed. After day 88 (W=7W=7), cutting at P=3P=3 leaves towns 0,1,2,30,1,2,3 connected and town 44 isolated; the answer is 22.