#P9073. [WC/CTS2023] 楼梯

    ID: 10120 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2023Special JudgeO2优化杨表WCCTSC/CTS

[WC/CTS2023] 楼梯

Background

The giraffe is tired, and he begins to dream.

In the dream, he is falling. He passes through grasslands, through spinning flocks of sheep. He passes through a sea of stars, through flames of feathers filling the sky.

Finally, he stands in front of a screen. On the screen is a pattern that looks like a staircase.

Problem Description

First, we give some formal definitions of a staircase.

We call an ordered pair (x,y)(x,y) of positive integers a cell. We call a set LL (possibly empty) consisting of cells a staircase if and only if it satisfies the following two conditions:

  • If (x,y)L(x,y)\in L and x>1x>1, then (x1,y)L(x-1,y)\in L.
  • If (x,y)L(x,y)\in L and y>1y>1, then (x,y1)L(x,y-1)\in L.

For a staircase LL and a cell (x,y)L(x,y)\in L, we define the sub-staircase generated by the generator cell (x,y)(x,y) as

$$\{(a-x+1, b-y+1) \mid (a,b) \in L, a \ge x, b \ge y\}$$

It is easy to prove that this set is still a staircase. For a staircase LL, we define the number of boundary cells as the number of cells (x,y)L(x,y)\in L that satisfy x=1x=1 or y=1y=1.

To make it easier to understand, we give an intuitive explanation. On the plane, we can arrange all cells into a grid in the order of increasing yy coordinate from left to right, and increasing xx coordinate from top to bottom. Therefore, we also call (x,y)(x,y) the cell in row xx, column yy.

Under this interpretation, if a cell belongs to a staircase, and its upper and left neighbors are not beyond the boundary, then the corresponding cells also belong to this staircase. A sub-staircase is the non-empty staircase formed by the cells in the region to the lower right of the generator cell. The number of boundary cells of a staircase is the total number of cells on the top boundary or the left boundary.

As in the figure below, $(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4),(3,1),(3,2),(3,3),(4,1),(5,1)$ form a valid staircase. The number of boundary cells of this staircase is 88. Among them, the sub-staircase generated with (1,3)(1,3) as the generator cell has 44 boundary cells.

After seeing the staircase on the screen, the giraffe is very curious. He first computes the number of boundary cells pp of this staircase, and is given a positive integer factor qq of pp. He wants to know whether the given staircase has a sub-staircase whose number of boundary cells equals qq. If yes, he wants you to output the generator cell of any such sub-staircase.

Dreams often change, so the giraffe may have many such queries, and the staircase may also change. The initial staircase LL is empty. For i1i \ge 1, let sis_i be the largest positive integer such that (i,si)L(i,s_i)\in L; if it does not exist, let it be 00. Then there are several modifications of one of the following three types:

  • Given positive integers aa and bb, insert bb cells at the end of each of the first aa rows. Formally, for i=1,2,,ai=1,2,\dots,a, add (i,si+1),(i,si+2),,(i,si+b)(i,s_i+1),(i,s_i+2),\dots,(i,s_i+b) into LL.
  • Given positive integers aa and bb, delete bb cells from the end of every row after row aa (including row aa). If there are fewer than bb cells, delete all of them. Formally, for i=a,a+1,a+2,,10100i=a,a+1,a+2,\dots,10^{100}, remove (i,si),(i,si1),,(i,sib+1)(i,s_i),(i,s_i-1),\dots,(i,s_i-b+1) from LL (ignore those that do not exist).
  • Given a positive integer uu, undo the previous uu operations, i.e., restore the staircase to the state before those uu operations. It is guaranteed that these uu operations are all queries or “insert at row end”. Specifically, suppose this is the tt-th operation. We must have t>ut>u, and operations t1,t2,,tut-1,t-2,\dots,t-u are all queries or the first type of modification above. You only need to restore the staircase to the state before the (tu)(t-u)-th operation (of course, you should keep the outputs of the queries).

It can be proven that after each modification, the resulting set is still a staircase.

Input Format

The first line of the input contains a positive integer mm, denoting the total number of operations.

The next mm lines each describe one of four types of operations. The detailed meaning can be found in the Description section. Each line consists of a character and one or two positive integers separated by spaces. Specifically:

  • + a b: Insert bb cells at the end of each of the first aa rows.
  • - a b: Delete bb cells from the end of every row after row aa (including row aa). If there are fewer than bb cells, delete all of them.
  • R u: Undo the previous uu operations, i.e., restore the staircase to the state before those uu operations. It is guaranteed that these uu operations exist and are all queries or “insert at row end”, i.e., the previous uu lines before this one all start with + or ?.
  • ? q: Ask whether there exists a sub-staircase whose number of boundary cells equals qq. If yes, output any valid generator cell. It is guaranteed that qq is a factor of the current staircase’s number of boundary cells.

Output Format

For each query (? operation), output one line.

If there exists a sub-staircase whose number of boundary cells equals qq, output two positive integers x y separated by a space, indicating a valid generator cell (x,y)(x,y). Otherwise, output -1 -1.

18
+ 5 1
+ 2 1
? 3
+ 3 2
? 4
? 4
+ 4 1
? 3
R 3
- 2 2
? 3
- 1 1
? 2
? 4
- 1 2
? 1
- 9 9
? 1
3 1
1 3
2 2
2 4
2 1
1 2
1 1
1 1
1 1
见附件中的 stairs2.in
见附件中的 stairs2.ans
见附件中的 stairs3.in
见附件中的 stairs3.ans
见附件中的 stairs4.in
见附件中的 stairs4.ans
见附件中的 stairs5.in
见附件中的 stairs5.ans
见附件中的 stairs6.in
见附件中的 stairs6.ans

Hint

Sample Explanation #1

The staircase after each modification is shown in the figure below (the arrangement is the same as in the Description section, and the indices of cells are omitted). Note that the undo operation actually only undoes one + operation. There are multiple valid answers for the sample; the given output is just one valid output.

Constraints

For all testdata, 1m3×1051 \le m \le 3 \times 10^5.

  • For + and - operations, 1a,b1091 \le a, b \le 10^9.
  • For R operations, it is guaranteed that the previous consecutive uu operations exist and are all queries or “insert at row end”.
  • For ? operations, 1q10181 \le q \le 10^{18} and it is guaranteed to be a factor of the current staircase’s number of boundary cells.

Let amaxa_{\max} be the maximum aa among all + and - operations, and let bmaxb_{\max} be the maximum bb among all + and - operations.

Test Point m=m = amaxa_{\max} \leq bmaxb_{\max} \leq Special Properties
11 200200 5050 2020 None
22 400400 100100 AB
33 600600 500500 500500 A
44 800800 10610^6 None
55 10310^3
66 30003000 10610^6 B
77 50005000 AB
88 10410^4 10910^9 None
99 3×1043 \times 10^4 A
1010 5×1045 \times 10^4 None
1111 7×1047 \times 10^4
1212 10510^5 B
1313 1.2×1051.2 \times 10^5 None
1414 1.4×1051.4 \times 10^5 10610^6
1515 1.6×1051.6 \times 10^5 AB
1616 1.8×1051.8 \times 10^5 10910^9 None
1717 2×1052 \times 10^5 10610^6 B
1818 2.5×1052.5 \times 10^5
1919 2.7×1052.7 \times 10^5 10910^9 None
2020 3×1053 \times 10^5

The special properties in the additional constraints are as follows:

  • Special Property A: All ? operations occur after all + and - operations. There are no R operations.
  • Special Property B: There are no - operations.

Hint

Please make sure to use appropriate data types to store all results.

Translated by ChatGPT 5