Candy Compress
Time limit2sMemory limit1024 MB
Maintain a string under insertions of single characters and deletions of substrings, and report the substring removed by each deletion.
- Level
Medium7 of 10
- Topics
- Implementation, Tree, Divide and conquer
- Solved
- No attempts yet
Problem
You are developing a mobile game called Candy Compress. In this game, there are several colored candies lined up from left to right. There are possible colors. At any point in time, the player can choose to add a candy at any position or to remove a subset of neighboring candies to get points depending on the colors of the removed candies.
To develop the game, you need to implement the following data structure. Initially, the data structure loads a -indexed string of characters, which represents the colors of the initial candies. The string consists only of uppercase Latin characters (A to Z). After loading the string, there are operations to support:
- Operation 1: Insert the uppercase Latin character into the string so that it is at the -th position in the new string. In particular, means inserting at the beginning of the string. It is guaranteed that , where is the length of the string just before this operation.
- Operation 2: Remove the characters from the -th to the -th position, inclusive. It is guaranteed that , where is the length of the string just before this operation.
For each Operation 2, the data structure must determine the characters that are removed, so the game can calculate the points to award. In other words, you must find the content of the string from the -th to the -th position just before the operation.
Input
The first line of input contains two integers and (; ). The second line contains a string of uppercase Latin characters, the initial string to be loaded. Each of the next lines is one of the following operations:
1is Operation 1. It is guaranteed that is an uppercase Latin character and , where is the length of the string just before this operation.2is Operation 2. It is guaranteed that , where is the length of the string just before this operation.
The operations are given in the order they are performed. There is at least one Operation 2.
Output
For each Operation 2, in order, output one line containing the characters removed by that operation.