Necklace Factory
Time limit3sMemory limit256 MB
Simulate a necklace of N colored beads under rotate, flip, swap, and paint commands, and count the same-color runs in the whole necklace or in a segment.
- Level
Hard8 of 10
- Topics
- Segment tree, Array, Implementation
- Solved
- No attempts yet
Problem

Company T sells necklaces with colored beads. The necklaces have fashionable designs, varied styles, and affordable prices, so they are popular with young people. Company T has decided to launch a necklace 'buffet' production system so that customers can choose which necklace design they find most beautiful.
The necklace buffet production system has a hardware system and a software system. The software system interacts with the user and controls the hardware system, and the hardware system receives commands from the software and produces the specified necklace. The hardware is already finished, but the software has not been developed yet. Company T asked you, a competitor at the National Olympiad in Informatics, to write a software simulation system.
A necklace has beads, and each bead has a color from 1, 2, ..., . The necklace is fixed to a flat board. One place on the board is position 1, and going clockwise from there, the other places are positions 2, 3, ..., .
Your program must support the following commands.
Input
The first line contains two integers and , the number of beads and the number of colors.
The second line contains integers , the colors of the beads at positions 1 through , where 1 ≤ ≤ .
The third line contains an integer , the number of queries. The next lines each contain one command in one of the formats above.
Output
For each C and CS command, print the answer on its own line.
Constraints
, , .
Hints
The R command rotates the beads, not the position numbers. The F command always uses position 1 as its axis of symmetry.
The CS command counts sections inside a segment. Note that when the queried segment has length , it is still treated as a segment.