Under construction

Still being built. Things may change

Clothesline

Time limit2sMemory limit1024 MB

Summary
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 NN and the number of queries QQ (1≤N≤1051 \le N \le 10^5, 1≤Q≤1051 \le Q \le 10^5).

The second line is a string of length NN, 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 QQ lines contains a query in the form T $L$ $R$ (1≤L≤R≤N1 \le L \le R \le N). TT is the query type, and LL and RR are the positions of the shirts the query applies to. There are six query types:

  1. If TT is PUNASEID, count the red shirts in positions LL to RR of the line.
  2. If TT is ROHELISI, count the green shirts in positions LL to RR.
  3. If TT is SINISEID, count the blue shirts in positions LL to RR.
  4. If TT is ETTE, cut out the segment from LL to RR and tie it to the start of the line. The two loose ends left behind are tied together so the shirts do not fall.
  5. If TT is TAHA, cut out the segment from LL to RR and tie it to the end of the line. The two loose ends left behind are tied together so the shirts do not fall.
  6. If TT is POORA, cut out the segment from LL to RR, 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.

Examples1

  1. Example 1

    Input
    5 6
    PPSRR
    POORA 1 3
    PUNASEID 1 1
    ETTE 4 5
    ROHELISI 1 2
    TAHA 1 2
    SINISEID 1 1
    
    Expected output
    0
    2
    1