Pizza Marathon
Time limit5sMemory limit1024 MB
Answer sums of prices over pizzas whose names start with pizza i's name and have length at least k, with point price updates.
- Level
Medium7 of 10
- Topics
- Trie, Tree, Segment tree, String
- Solved
- No attempts yet
Problem
Tired of the hard work underground, the Ninja Turtles decided to take a vacation. How better to spend free time when you are green and carry a shell on your back? A week-long pizza marathon, of course!
The turtles have the menu of their favorite pizzeria. Each pizza has a name and a price. The size of a pizza equals the length of its name. Some pizzas the turtles have already tried. To order, they pick a pizza they liked and order all pizzas similar to it with size at least . Pizza is similar to pizza if is a prefix of .
The turtles cannot travel around the city themselves, so they asked April to report changes at the pizzeria, in particular changes in prices.
During the week, Donatello wrote down pizza orders and price changes in his notebook. At the end of the week he tried to total all orders, found there were too many, and asked you to help.
Input
The first line contains two integers and (, ), the number of pizzas on the menu and the number of notes in the notebook. The next lines describe the menu: the -th line contains the name and the initial price () of the -th pizza, separated by a space. Names consist only of lowercase Latin letters. The total length of all names does not exceed . All names are distinct.
The next lines contain Donatello's notes in one of the following formats:
- "? i k": the turtles ordered pizzas similar to the -th pizza with size at least (, ).
- "! i j": April reported that the price of the -th pizza increased by (, ).
Output
For each order, print its total cost on a separate line.