Birthday Bash

4.8

6 votes
Fenwick (Binary Indexed) Trees, Data Structures, Advanced Data Structures
Problem

Emma planned to buy a string S consisting of lowercase English characters of size N as a gift for her friend Ava on her birthday. Ava had Q other friends who also wanted to contribute but were not happy with the string. So all Q of her friends decide to do either of the two operations :

  • 1 x - Give an index x and ask Emma to reverse the string from index x to Nx1.
  • 2 x - Give an index x and ask Emma to change the character at index x to the alphabetically next character. For example, if the character was a, it would become b (assume that a is alphabetically next to z).

All of this was too much for Emma to handle alone. So she asked you to help her find the final string after doing all the operations.

Note : Assume 0-based indexing.

Input Format:

  • The first line of input contains an integer T, denoting the number of test cases.
  • The first line of each test case contains two integers, N and Q, denoting the size of the string and the number of friends of Ava respectively.
  • The following line contains a string of size N.
  • The next Q line contains two integers and is one of two types :
    •  1 x - denoting that Emma needs to reverse the string from index x to Nx1 (it is guaranteed that x will be less than Nx1).
    •  2 x - denoting that Emma needs to change the x'th character to alphabetically next character.

Output Format:
For each test case, print a string of size N denoting the final string.

Constraints:
1T5
1N,Q105
S contains all lower case English alphabets.
0X<N

Sample Input
2
5 3
abcub
1 1
2 2 
1 0
6 2
coding
2 3 
1 2 
Sample Output
bbdua
cojdng
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case, after the first operation, the string becomes aucbb as we are reversing from 1st index to 3rd index. After the second operation, the string becomes audbb as we are changing c to d at the 2nd index and after the third operation string becomes bbdua, as we are reversing the string from index 0 to 4.

In the second test case, after the first operation string becomes codjng as we are changing the i to j at index 3 and after the second operation string becomes cojdng as we are reversing the string from index 2 to 3. 

 

Editor Image

?