Score Betting
Time limit1sMemory limit1024 MB
Given prefix and suffix lists of scored strings with updates, find the max and min total score of any nonempty string over letters and digits.
- Level
Hard9 of 10
- Topics
- Trie, String matching, Tree, Segment tree
- Solved
- No attempts yet
Problem
Amel and Nina bet on scores using strings made of lowercase English letters and digits -. Amel builds a string that earns the highest possible score, and Nina builds a string that earns the lowest possible score. Each string they build must have length at least 1.
The score of a string is the sum of two parts added together:
- the total score of the strings in the prefix list that are prefixes of
- the total score of the strings in the suffix list that are suffixes of
If no string in a list satisfies the condition, that list contributes . Both lists start empty.
The prefix list and the suffix list keep changing during the bet, so Amel and Nina have trouble deciding at each moment which string would be most favorable to build.
Help Amel and Nina process the following queries.
< str X: Adds the stringstrwith score to the prefix list. Ifstris already in the list, its score is changed to .> str X: Adds the stringstrwith score to the suffix list. Ifstris already in the list, its score is changed to .+: Prints the maximum score Amel can earn.-: Prints the minimum score Nina can earn.
Input
The first line contains the number of queries ().
Each of the next lines contains one query.
- , and is an integer.
- Every string in the input has length at least 1 and consists only of lowercase English letters. Digits to do not appear in the input strings.
- The total length of all strings in the input does not exceed .
- There is at least one
+or-query.
Output
For each + and - query, print the result on its own line.
Hint
In the sample output, the strings that earn each score are, in order, abc, 111, abcd, ab123, and abc.