#P6301. 集合

集合

Problem Description

You need to maintain online a sorted set SS of natural numbers and support the following operations:

  1. Given a number xx, if xx is not in the set SS, then insert xx into SS.
  2. Given a number xx, if xx is already in the set SS, then delete xx from SS.

To prove that you are maintaining SS, you must answer the following queries during the operations:

  1. Output the value of the minimum element in SS. If S=S=\varnothing, return -1.
  2. Output the value of the maximum element in SS. If S=S=\varnothing, return -1.
  3. Output the number of elements in SS.
  4. Given a number xx, determine whether xx is in the set. If yes, return 1; otherwise, return 0.
  5. Given a number xx, let T=S[0,x)T=S\cap[0^-,x). Output the value of the maximum element in TT. If T=T=\varnothing, return -1.
  6. Given a number xx, let T=S[x,n)T=S\cap[x,n). Output the value of the minimum element in TT. If T=T=\varnothing, return -1.

To prove that you maintain SS online, for all operations 1,21,2 and queries 6,7,86,7,8 after the first query, the actual parameter xx equals the parameter xx' given in the input plus the return value last\text{last} of the previous query. That is, x=x+lastx=x'+\text{last}.

It is guaranteed that 0x<n0\le x<n.

Initially, S=S=\varnothing.

Input Format

The input has (m+1)(m+1) lines, where the meaning of mm is given below.

The first line contains two positive integers n,mn,m, where nn is the upper bound of the parameter xx in operations and queries, and mm is the total number of operations and queries.
The next mm lines each contain one or two natural numbers, representing one operation or one query. Each line is guaranteed to be in one of the following eight forms:

  1. 1 x', meaning perform operation 11.
  2. 2 x', meaning perform operation 22.
  3. 3, meaning answer query 33.
  4. 4, meaning answer query 44.
  5. 5, meaning answer query 55.
  6. 6 x', meaning answer query 66.
  7. 7 x', meaning answer query 77.
  8. 8 x', meaning answer query 88.

Output Format

To avoid too much output, you only need to output one line with one integer, which is the sum of the return values of all queries.

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

8

Hint

Sample Explanation

The operations actually performed and the queries actually answered are as follows:

1 0
1 1
1 3
1 3
3		->  0
7 0		-> -1
7 1		->  0
8 3		->  3
4		->  3
2 3
4		->  1
6 3		->  0
5		->  2

Therefore, the output is 0+(1)+0+3+3+1+0+2=80+(-1)+0+3+3+1+0+2=8.

Constraints

Test Point ID n=n= m=m= Score
11 2202^{20} 2142^{14} 55
22 2252^{25} 2172^{17}
33 2302^{30} 2202^{20} 1010
44 2222^{22} 1515
55
66 2232^{23} 2525
77

For 100%100\% of the testdata, 220n2302^{20}\le n\le2^{30}, 214m2232^{14}\le m\le 2^{23}, and 0x<n0\le x<n.

Hint

The input size of this problem is large, so a faster input method is recommended.

00^- denotes a value slightly smaller than 00, and [0,x)[0^-,x) guarantees that TT in the 7th operation is always well-defined.

Translated by ChatGPT 5