Broken Calculator Game
Time limit4sMemory limit1024 MB
Given a formula of digits and operators, apply Q range additions mod 12 to its characters and report the evaluated value after each update.
- Level
Hard8 of 10
- Topics
- Segment tree, String, Simulation
- Solved
- No attempts yet
Problem
You find a suspicious calculator on the road. As you pick it up, a murderer appears behind you. You run as hard as you can, but you end up at a dead end. From behind you hear 'Let's start the game,' and a formula appears on the calculator. A life-or-death game begins.
The game is to evaluate the formula on the calculator. Following the rules of this broken calculator is not easy.
- The formula has length N and consists of the digits 0 to 9 and the operators '+' and '*'.
- When evaluating the formula, ignore every operator that is at the start of the formula or directly follows another operator. Then, if the last character of the formula is an operator, ignore that operator too.
- When evaluating the formula, '*' is computed before '+'.
- The value of the formula is always taken modulo .
- A formula that contains only operators has the value -1.
For example, '**11+*3*+5++' is read as '11+3*5', so the answer is 26. '**+++**+++' gives -1.
You planned to evaluate the formula once and end the game. However, participants with suspiciously high ratings kept pressuring the setters to add query problems, so the formula on the calculator changes Q times.
Each change is written as l r x. It adds x to every digit from the l-th to the r-th character of the formula. Only when processing queries, '+' and '*' are also treated as numbers, with values 10 and 11. If the sum is 12 or more, apply the remainder modulo 12.
Compute the values of all Q+1 formulas shown on the calculator and survive the game!
Input
The first line contains N and Q, where and .
The second line contains the formula of length N.
From the third line on, each of the Q lines contains one query, where and .
Output
The first line prints the value of the given formula modulo . If the formula consists only of operators, print -1.
From line 2 to line Q+1, line i prints the value of the formula after applying the (i-1)-th query, modulo . If the formula consists only of operators, print -1.