#P9073. [WC/CTS2023] 楼梯
[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 of positive integers a cell. We call a set (possibly empty) consisting of cells a staircase if and only if it satisfies the following two conditions:
- If and , then .
- If and , then .
For a staircase and a cell , we define the sub-staircase generated by the generator cell 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 , we define the number of boundary cells as the number of cells that satisfy or .
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 coordinate from left to right, and increasing coordinate from top to bottom. Therefore, we also call the cell in row , column .
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 . Among them, the sub-staircase generated with as the generator cell has boundary cells.

After seeing the staircase on the screen, the giraffe is very curious. He first computes the number of boundary cells of this staircase, and is given a positive integer factor of . He wants to know whether the given staircase has a sub-staircase whose number of boundary cells equals . 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 is empty. For , let be the largest positive integer such that ; if it does not exist, let it be . Then there are several modifications of one of the following three types:
- Given positive integers and , insert cells at the end of each of the first rows. Formally, for , add into .
- Given positive integers and , delete cells from the end of every row after row (including row ). If there are fewer than cells, delete all of them. Formally, for , remove from (ignore those that do not exist).
- Given a positive integer , undo the previous operations, i.e., restore the staircase to the state before those operations. It is guaranteed that these operations are all queries or “insert at row end”. Specifically, suppose this is the -th operation. We must have , and operations are all queries or the first type of modification above. You only need to restore the staircase to the state before the -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 , denoting the total number of operations.
The next 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 cells at the end of each of the first rows.- a b: Delete cells from the end of every row after row (including row ). If there are fewer than cells, delete all of them.R u: Undo the previous operations, i.e., restore the staircase to the state before those operations. It is guaranteed that these operations exist and are all queries or “insert at row end”, i.e., the previous lines before this one all start with+or?.? q: Ask whether there exists a sub-staircase whose number of boundary cells equals . If yes, output any valid generator cell. It is guaranteed that 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 , output two positive integers x y separated by a space, indicating a valid generator cell . 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, .
- For
+and-operations, . - For
Roperations, it is guaranteed that the previous consecutive operations exist and are all queries or “insert at row end”. - For
?operations, and it is guaranteed to be a factor of the current staircase’s number of boundary cells.
Let be the maximum among all + and - operations, and let be the maximum among all + and - operations.
| Test Point | Special Properties | |||
|---|---|---|---|---|
| None | ||||
| AB | ||||
| A | ||||
| None | ||||
| B | ||||
| AB | ||||
| None | ||||
| A | ||||
| None | ||||
| B | ||||
| None | ||||
| AB | ||||
| None | ||||
| B | ||||
| None | ||||
The special properties in the additional constraints are as follows:
- Special Property A: All
?operations occur after all+and-operations. There are noRoperations. - Special Property B: There are no
-operations.
Hint
Please make sure to use appropriate data types to store all results.
Translated by ChatGPT 5