Python Code Folding
Time limit2sMemory limit1024 MB
Given a correct Python code with indent levels, toggle folding of blocks by query and report how many lines remain visible after each print query.
- Level
Medium6 of 10
- Topics
- Tree, Segment tree, DFS
- Solved
- No attempts yet
Section 1
Python is a high-level programming language known for its concise and readable syntax. It is widely used in data analysis, artificial intelligence, web development, and other fields, and it has a large community and ecosystem.
Python code is made of the following elements.
- Line is one row of Python code. In this problem, each line is either a header or a simple statement.
- Block forms structures such as loops and conditionals. A block consists of several lines and is split into a header and a body.
- To separate blocks, each line is preceded by zero or more tab characters. These tab characters are called indents, and the number of tab characters before a line is its indent level. In every example in this problem, each tab is shown as 4 spaces.
- Header starts a block and is exactly one line.
- Simple statement is any line that is not a header.
- Body is the inside of a block and consists of one or more lines.
Correct Python code satisfies at least one of the following conditions.
- It consists only of simple statements with indent level 0.
- It starts with a header of indent level 0, and at least one line of code follows the header. If you remove the header and decrease the indent level of every remaining line by 1, the result is correct Python code. In this case, the code is a block, and all the code after the header is the body of the block.
- It is the concatenation of two correct Python codes.
Here are examples of incorrect Python code, along with the reason each one fails.
a=1
b=3
c=5
It has only simple statements, but not every line has indent level 0.
for i in range(n):
for x in a:
print(s)
The body of the first block does not contain at least one line of code.
for i in range(n):
for j in range(n):
print(i+j)
The first line is a header, but removing it and decreasing every remaining indent level by 1 does not produce correct code.
Below is a correct Python code that reads an integer from the user and prints a right triangle made of '*' characters.
1| n = int(input())
2| a = []
3| for i in range(1, n+1):
4| s = ""
5| for j in range(i):
6| s += "*"
7| a.append(s)
8| for s in a:
9| print(s)
Many code editors offer Code Folding. This feature hides the body of a block and shows only its header, which makes long code easier to read. Formally, Code Folding is defined as follows.
At first, every block is unfolded. When the code contains a block, Code Folding can fold or unfold that block. When a block is folded, every line in its body becomes hidden. Folding a block that contains another block does not change the folded state of the inner block.
For example, folding the second block of the code above hides line 6.
1| n = int(input())
2| a = []
3| for i in range(1, n+1):
4| s = ""
5| for j in range(i): ...
7| a.append(s)
8| for s in a:
9| print(s)
Next, folding the first block hides lines 4, 5, 6, and 7.
1| n = int(input())
2| a = []
3| for i in range(1, n+1): ...
8| for s in a:
9| print(s)
If you unfold the first block again, the second block stays folded, so line 6 remains hidden, the same as in the previous example.
Given a correct Python code, write a program that performs Code Folding and outputs the number of lines visible after each query.
Input
The first line contains the number of lines and the number of queries . ,
Each of the next lines contains an integer and a lowercase letter . The indent level of line is , and is h if the line is a header or s if it is a simple statement.
Each of the following lines contains one query.
t: Among the blocks in the code that are currently not hidden by a folded ancestor, if the -th block is unfolded, fold it. If it is folded, unfold it. The -th block is never hidden by a folded outer block. the total number of header linesp: Print the number of lines currently visible. At least one such query is given.
The given code always satisfies the conditions of correct Python code.
Output
Execute the queries in order. For each p query, print the number of lines visible at that moment, one per line.