#K82877. String Operations with Digit Sum Query
String Operations with Digit Sum Query
String Operations with Digit Sum Query
You are given an initially empty string S. Your task is to perform a series of operations on S. There are two types of operations:
- INSERT P X: Insert the string P at position X of S. The position X is 0-indexed.
- QUERY L R: Calculate the sum of all digit characters in the substring of S starting from L to R (both inclusive). Note that the indices L and R are given as 1-indexed and must be converted to 0-indexed when processing.
In the query operation, if we denote the substring from L to R (0-indexed) by \(S[L:R]\), you should compute:
$$\text{result} = \sum_{i=L}^{R} d_i,$$where \(d_i\) is the numeric value of the digit at position \(i\) (if the character is a digit, otherwise it contributes 0).
It is guaranteed that all operations are valid.
inputFormat
The first line of input contains a single integer N representing the number of operations.
The following N lines each contain a command, which is either:
- INSERT P X — insert the string P at position X (0-indexed) of S.
- QUERY L R — query the sum of all digit characters in the substring from L to R (1-indexed).
For every QUERY operation, output the result on a new line.
outputFormat
For each QUERY command, output a single line containing the sum of digit characters in the specified substring of S.
## sample6
INSERT abc123 0
INSERT 456def 6
QUERY 4 9
INSERT xyz 3
QUERY 1 5
QUERY 10 12
21
0
15
</p>