Clothesline
Time limit2sMemory limit1024 MB
Given a string of three shirt colors, answer per-color counts on ranges while moving segments to the front or back and reversing segments.
- Level
Medium7 of 10
- Topics
- Tree, String, Implementation
- Solved
- No attempts yet
Statement
Jukul has many red, green, and blue shirts from programming contests. He washed them all and hung them in a row on a clothesline. He did not like the order of the shirts on the line.
So Juku computed the most pleasing arrangement of the shirts, along with the moves needed to reach it. Between moves, he wants to check that all his shirts are still on the line. To do that, he wants to know how many shirts of a given color are in a segment of the line.
Input
The first line contains the number of shirts and the number of queries (, ).
The second line is a string of length , the initial order of the shirts. P is a red shirt, R is a green shirt, and S is a blue shirt.
Each of the next lines contains a query in the form T $L$ $R$ (). is the query type, and and are the positions of the shirts the query applies to. There are six query types:
- If is
PUNASEID, count the red shirts in positions to of the line. - If is
ROHELISI, count the green shirts in positions to . - If is
SINISEID, count the blue shirts in positions to . - If is
ETTE, cut out the segment from to and tie it to the start of the line. The two loose ends left behind are tied together so the shirts do not fall. - If is
TAHA, cut out the segment from to and tie it to the end of the line. The two loose ends left behind are tied together so the shirts do not fall. - If is
POORA, cut out the segment from to , reverse it, and tie it back into the same place.
Output
For each PUNASEID, ROHELISI, or SINISEID query, print the answer on a separate line.