luogu#P16327. 线段

    ID: 16155 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>线段树二分树状数组洛谷原创O2优化前缀和差分洛谷月赛

线段

Problem Description

You have a multiset SS, where each element is a segment. Initially, there are nn elements, and the ii-th element is the segment [li,ri][l_i,r_i].

Then there are qq operations. In each operation, a segment [L,R][L,R] is given. We simultaneously check every current element [li,ri][l_i,r_i]. If it intersects with [L,R][L,R], then the intersecting part is split out from the original segment, and the remaining parts are added back as new elements, while the original element is removed. The newly added elements do not participate in the current operation, and the segment [L,R][L,R] itself is not added into SS.

Formally, let l=max(li,L)l'=\max(l_i,L) and r=min(ri,R)r'=\min(r_i,R). If lrl'\le r', then:

  • Add the segment [l,r][l',r'] to SS.
  • If li<ll_i<l', add the segment [li,l1][l_i,l'-1] to SS.
  • If r<rir'<r_i, add the segment [r+1,ri][r'+1,r_i] to SS.
  • Remove the segment [li,ri][l_i,r_i] from SS.

After each operation, output the current number of elements in SS.

Note that if a newly generated element is identical to an existing one, all copies are kept. Similarly, when an element is removed, only one copy is deleted even if multiple identical copies exist.

Input Format

The first line contains two positive integers n,qn,q, denoting the initial number of elements and the number of operations.

The next nn lines each contain two positive integers li,ril_i,r_i, describing the initial segments [li,ri][l_i,r_i].

The next qq lines each contain two positive integers Li,RiL_i,R_i, describing the segment [Li,Ri][L_i,R_i] given in each operation.

Output Format

Print qq lines, each containing a single integer — the number of elements after the corresponding operation.

5 3
1 1
10 10
3 3
3 3
1 10
2 8
3 10
1 3
7
8
9

10 10
2 9
10 10
2 8
2 5
2 9
2 2
1 10
10 10
2 9
2 2
2 10
1 6
1 10
3 4
2 9
1 9
3 7
2 10
2 10
2 10
11
16
16
28
29
29
34
34
34
34

Hint

Explanation for Sample 1

Initially there are 55 segments: [1,1],[10,10],[3,3],[3,3],[1,10][1,1],[10,10],[3,3],[3,3],[1,10].

For the first operation, the given segment is [2,8][2,8]. The segments intersecting it are [3,3],[3,3],[1,10][3,3],[3,3],[1,10]. After the operation, both [3,3][3,3] remain unchanged, while [1,10][1,10] becomes [1,1],[2,8],[9,10][1,1],[2,8],[9,10]. So there are 77 elements in total.

For the second operation, the given segment is [3,10][3,10]. The intersecting elements are [10,10],[3,3],[3,3],[2,8],[9,10][10,10],[3,3],[3,3],[2,8],[9,10]. Among them, only [2,8][2,8] changes, becoming [2,2],[3,8][2,2],[3,8]. So there are 88 elements in total.

For the third operation, the given segment is [1,3][1,3]. The only intersecting element that changes is [3,8][3,8], which becomes [3,3],[4,8][3,3],[4,8]. So there are 99 elements in total.

Constraints

For all test cases, 1n,q5×1051 \le n,q \le 5\times 10^5, 1liri1091\le l_i \le r_i \le 10^9, and 1LiRi1091 \le L_i \le R_i \le 10^9.

Subtask n,qn,q\le Special Property Score
11 200200 None 2020
22 20002000
33 5×1055\times 10^5 Yes
44 None 4040
  • Special property: It is guaranteed that Li=1L_i=1.