#PS0000176. Số giá trị sau khi bỏ thao tác (Program)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số giá trị sau khi bỏ thao tác (Program)
Program
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An integer variable starts at zero. A program contains commands: + adds one and - subtracts one. For each query , skip commands through inclusive, execute the remaining commands in their original order, and start again at . Count distinct values visited, including the initial zero. Queries are independent and do not modify the original program.
Input
Each file contains one case. The first line contains . The second contains the length- string . Each of the next lines contains .
Output
Print lines, each giving the distinct-value count for its query.
Subtasks
- Subtask 1 (20 points): ; ; ; the string contains only
+and-. - Subtask 2 (30 points): ; ; ; the string contains only
+and-; all commands are identical. - Subtask 3 (50 points): ; ; ; the string contains only
+and-.
Example 1
Input
8 4
-+--+--+
1 8
2 8
2 5
1 1
Output
1
2
4
4
Explanation
Deleting all commands leaves only zero. The next three sets are {0,-1}, {0,-1,-2,-3}, {0,1,-1,-2}.
Example 2
Input
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4
Output
3
3
4
2
3
2
1
2
2
2
Explanation
For (2,2), the remaining program is +++, visiting 0,1,2,3. Query (1,4) leaves only zero. Evaluate the other queries analogously.