Calculation mistake
Time limit3sMemory limit256 MB
Maintain a string of digits and plus or minus signs under range replacements, and evaluate the arithmetic value of any substring with the calculator's operator rules.
- Level
Hard8 of 10
- Topics
- Segment tree, String, Math
- Solved
- No attempts yet
Problem
Suchan uses a calculator that only adds and subtracts. It has the digit buttons 0 through 9, the operator buttons + and -, an = button that shows the result, and an AC button that returns the calculator to its initial state. There is no C button that clears only the number being typed.
Type an expression in order and press =, and the screen shows the result. The numbers have no range limit, and a number may start with 0. To keep a mistyped operator from spoiling the result, the calculator follows three rules.
- When operators appear one after another, only the last one counts.
- When the expression starts with an operator, a 0 is treated as written in front of it.
- When the expression ends with an operator, that operator is ignored.
For example, press -15+0035-+-3- in order and then =. The calculator evaluates and shows 17. Press only - and then =: the expression becomes , the final - is ignored, and the result is 0.
Without a C button, one wrong digit forces Suchan to press AC and type the whole expression again. To get rid of that, he decided to build the following data structure.
There is a string of length made only of 0 through 9, +, and -. For a string and , let be the substring formed by concatenating the -th through -th characters of .
Implement a data structure that supports the two operations below.
- Replace: change into a new string of length . That is, for every with , set to .
- Evaluate: press
AC, type into the calculator, press=, and report the value shown on the screen.
Input
The first line contains the length of the string, ().
The second line contains the string . Its length is .
The third line contains the number of queries, ().
Each of the next lines contains one operation. The formats are below.
- A replace operation is given as
1 a b T. Here and are integers with , and is a string of length . The lengths of over all replace operations sum to at most . - An evaluate operation is given as
2 a b. Here and are integers with . At least one evaluate operation is given.
Every string in the input consists only of 0 through 9, +, and -.
Output
For each evaluate operation, print the result modulo on its own line. The remainder of an integer modulo is the value of for which integers and satisfy and .