Love Song (Codeforces 1539B)
Tags: DP, strings

Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring "abbcb", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c" three times, so that the resulting string is "abbbbcccbb", its length is $$$10$$$. Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.

Input: The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n\leq 100\,000$$$, $$$1\leq q \leq 100\,000$$$) — the length of the song and the number of questions. The second line contains one string $$$s$$$ — the song, consisting of $$$n$$$ lowercase letters of English letters.Vasya's questions are contained in the next $$$q$$$ lines. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) — the bounds of the question.

Output: Print $$$q$$$ lines: for each question print the length of the string obtained by Vasya.

Note: In the first example Vasya is interested in three questions. In the first question Vasya considers the substring "aba", that transforms to "abba", so the answer is equal to $$$4$$$. In the second question Vasya considers "baca", that transforms to "bbaccca", so the answer is $$$7$$$. In the third question Vasya considers the string "abacaba",that transforms to "abbacccabba" of length $$$11$$$.

Sample Inputs:
7 3
abacaba
1 3
2 5
1 7

7 4
abbabaa
1 3
5 7
6 6
2 4

13 7
sonoshikumiwo
1 5
2 10
7 7
1 13
4 8
2 5
3 9

Sample Outputs:
4
7
11

5
4
1
5

82
125
9
191
62
63
97

Solution (Python):
def i():
    (n, q) = map(int, input().split())
    a = [0]
    c = 0
    ans = ''
    for x in input():
        c += ord(x) - 96
        a.append(c)
    for x in range(q):
        (u, v) = map(int, input().split())
        ans += f'{a[v] - a[u - 1]}\n'
    print(ans)
i()

Solution (Python):
(n, s, *a) = open(0)
b = [0]
for x in s:
    b += (b[-1] + ord(x) - 96,)
for s in a:
    (l, r) = map(int, s.split())
    print(b[r] - b[l - 1])

Solution (Python):
(n, s, *a) = open(0)
b = [0]
for x in s:
    b += (b[-1] + ord(x) - 96,)
for s in a:
    (l, r) = map(int, s.split())
    print(b[r] - b[l - 1])