luogu#P8045. [COCI 2015/2016 #4] HAN

[COCI 2015/2016 #4] HAN

Background

Han did not want to study alone, so he invited his friend Dominik over. After they studied together for an evening, Dominik went home. To his surprise, a police officer stopped him and believed that he was drunk. As everyone knows, in this situation, by solving a series of carefully designed tasks to test a person’s cognitive ability, one can prove whether they are sober. If we can trust Dominik, the conversation would go like this—

Police officer: Let’s start with an easy question... What is the complexity of bubble sort?
Dominik: Too easy, O(n2)\mathcal O(n^2).
Police officer: Very good, next question. Please recite the English alphabet backwards.
Dominik: Too boring, zyxwvutsrqponmlkjihgfedcba\texttt{zyxwvutsrqponmlkjihgfedcba}.
Police officer: You clearly memorized that, it does not count. Let me test you with another question. Imagine that all letters from a\texttt{a} to z\texttt{z} are placed clockwise in order on a circle. Initially, you start from the letter a\texttt{a} and read letters clockwise. After you finish reading each letter, I can tell you to read in the opposite direction, or I can ask how many times you have read a certain letter so far. Ready? Start!
Dominik: Uh... a\texttt{a}, b\texttt{b}, c\texttt{c}...

The police officer’s last question obviously stumped Dominik. Now, please help Dominik answer these questions.

Problem Description

Specifically, the police officer will give qq commands. Each command is one of the following two types:

  • SMJER n\bf SMJER~n: After Dominik says the nn-th letter, he must start reading letters in the opposite direction to the current one.
  • UPIT n x\bf UPIT~n~x: Dominik needs to answer how many times the letter xx appears among the first nn letters he has said.

For each UPIT n x\bf UPIT~n~x command, output the answer.

Input Format

The first line contains an integer qq, which is the number of commands given by the police officer.
Then follow qq lines describing the qq commands. Each line first contains a string ss and an integer nn. If ss is UPIT\bf UPIT, then after the integer nn you also need to input a character xx.

The testdata guarantees that for all qq commands, nn is strictly increasing. Formally, i[1,q)\forall i\in [1,q), ni<ni+1n_i<n_{i+1}.

Output Format

For each UPIT n x\bf UPIT~n~x command, output one line with an integer, which is the number of times the character xx appears among the first nn letters Dominik has said.

5
UPIT 1 b
UPIT 3 b
SMJER 4
UPIT 7 a
UPIT 10 z
0
1
2
1
5
SMJER 1
SMJER 2
SMJER 3
UPIT 5 a
UPIT 7 w
2
1
4
UPIT 100 a
UPIT 200 c
UPIT 300 a
UPIT 400 b
4
8
12
16

Hint

[Sample 1 Explanation]

The first 1010 letters Dominik says, in order, are: a\texttt{a}, b\texttt{b}, c\texttt{c}, d\texttt{d}, c\texttt{c}, b\texttt{b}, a\texttt{a}, z\texttt{z}, y\texttt{y}, x\texttt{x}.

[Sample 2 Explanation]

The first 77 letters Dominik says, in order, are: a\texttt{a}, z\texttt{z}, a\texttt{a}, z\texttt{z}, y\texttt{y}, x\texttt{x}, w\texttt{w}.

[Constraints]

For 40%40\% of the testdata, n1000n\leqslant 1000.
For 60%60\% of the testdata, n105n\leqslant 10^5.
For all testdata, 1q1051\leqslant q\leqslant 10^5, 1n1091\leqslant n\leqslant 10^9, and xx contains only lowercase letters.

[Source]

This problem is from COCI 2015-2016 CONTEST 4 T2 HAN, and according to the original testdata configuration, the full score is 8080 points.

Translated and organized by Eason_AC.

Translated by ChatGPT 5