#P16458. [UOI 2026] mex plus

    ID: 16444 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 8 Uploaded By: Tags>并查集图论建模2026线段树分治UOI(乌克兰)

[UOI 2026] mex plus

题目描述

From each pair, you need to choose one number for array AA and the other number for array BB. That is, for each pair (x,y)(x, y), one of the following two choices can be made:

  • add xx to array AA and yy to array BB;
  • add yy to array AA and xx to array BB.

After choices are made for all pairs, arrays AA and BB will have the same length.

For an array CC, let mex(C)\operatorname{mex}(C) denote the smallest non-negative integer that does not appear in array CC. For example, if C=[0,1,4,1,2], C = [0, 1, 4, 1, 2], then mex(C)=3\operatorname{mex}(C) = 3, because numbers 00, 11, and 22 appear in the array, while number 33 does not. If C=[1,2,3], C = [1, 2, 3], then mex(C)=0\operatorname{mex}(C) = 0, because number 00 does not appear in the array.

You need to maximize mex(A)+mex(B). \operatorname{mex}(A) + \operatorname{mex}(B).

After the initial set of pairs, qq queries are given. Each query either adds one pair to the set or removes one pair from the set. After each query, you need to find again the maximum possible value of mex(A)+mex(B) \operatorname{mex}(A) + \operatorname{mex}(B) for the current set of pairs.

It is important that after each query, you may choose the order of numbers in all pairs again. That is, the previous choice does not restrict the next answers.

输入格式

The first line contains two integers nn and qq $(1 \le n \le 2 \cdot 10^5, 0 \le q \le 2 \cdot 10^5)$ --- the initial number of pairs and the number of queries.

The next nn lines contain the initial pairs. The ii-th of these lines contains two integers aia_i and bib_i (0aibi109)(0 \le a_i \le b_i \le 10^9).

The next qq lines contain the queries. Each query has one of the following formats: + x y \texttt{+ } x \ y or - x y, \texttt{- } x \ y, where 0xy1090 \le x \le y \le 10^9.

The query + x y\texttt{+ x y} means that the pair (x,y)(x, y) is added to the set.

The query - x y\texttt{- x y} means that one pair (x,y)(x, y) is removed from the set. It is guaranteed that at the moment of such a query, there exists at least one such pair in the set.

输出格式

Print q+1q + 1 integers.

The first integer is the maximum value of $\operatorname{mex}(A) + \operatorname{mex}(B)$ for the initial set of pairs.

Then, after each query, print the maximum value of $\operatorname{mex}(A) + \operatorname{mex}(B)$ for the current set of pairs.

Print each answer on a separate line.

5 1
0 1
0 2
1 1
4 5
2 3
+ 4 5
8
9
5 0
1 1
1 1
1 1
1 1
0 0
4
5 0
1 2
2 3
3 4
4 5
5 6
0
5 0
2 2
3 3
0 1
3 4
0 10
6
5 0
1 3
4 5
0 9
0 0
5 6
3

提示

In the first example, for the initial set of pairs it is optimal to choose, for example, A=[0,2,1,4,3]A = [0, 2, 1, 4, 3], B=[1,0,1,5,2]B = [1, 0, 1, 5, 2]. Then mex(A)=5\operatorname{mex}(A) = 5, mex(B)=3\operatorname{mex}(B) = 3, and the sum is 88. After adding the pair (4,5)(4, 5), one can, for example, choose A=[0,2,1,5,3,4]A = [0, 2, 1, 5, 3, 4], B=[1,0,1,4,2,5]B = [1, 0, 1, 4, 2, 5], then mex(A)=6\operatorname{mex}(A) = 6, mex(B)=3\operatorname{mex}(B) = 3, and the sum is 99.

In the second example, the multiset of values is {0,0,1,1,1,1,1,1,1,1}\{0, 0, 1, 1, 1, 1, 1, 1, 1, 1\}. It is optimal, for example, to choose A=[1,1,1,1,0]A = [1, 1, 1, 1, 0], B=[1,1,1,1,0]B = [1, 1, 1, 1, 0]. Then mex(A)=mex(B)=2\operatorname{mex}(A) = \operatorname{mex}(B) = 2, and the sum is 44.

In the third example, none of the values is equal to 00, so no choice can make 0A0 \in A or 0B0 \in B. Therefore, mex(A)=mex(B)=0\operatorname{mex}(A) = \operatorname{mex}(B) = 0, and the sum is 00.

In the fourth example, it is optimal, for example, to choose A=[2,3,0,3,10]A = [2, 3, 0, 3, 10], B=[2,3,1,4,0]B = [2, 3, 1, 4, 0]. Then mex(A)=1\operatorname{mex}(A) = 1, mex(B)=5\operatorname{mex}(B) = 5, and the sum is 66.

In the fifth example, it is optimal, for example, to choose A=[3,4,9,0,6]A = [3, 4, 9, 0, 6], B=[1,5,0,0,5]B = [1, 5, 0, 0, 5]. Then mex(A)=1\operatorname{mex}(A) = 1, mex(B)=2\operatorname{mex}(B) = 2, and the sum is 33.

Scoring

  • (22 points): n20n \le 20, q=0q = 0;
  • (77 points): bi=109b_i = 10^9 for all initial pairs, and y=109y = 10^9 for all queries;
  • (88 points): ai=0a_i = 0 for all initial pairs, and x=0x = 0 for all queries;
  • (99 points): n500n \le 500, q500q \le 500;
  • (1111 points): q=0q = 0;
  • (1010 points): all queries have the form + x y\texttt{+ x y};
  • (99 points): all queries have the form - x y\texttt{- x y};
  • (1313 points): at any moment, for any three pairs from the current set, if the first pair shares a number with the second pair, and the second pair shares a number with the third pair, then there exists a number that belongs to all three pairs;
  • (2020 points): n,q105n, q \le 10^5;
  • (1111 points): without additional constraints.