#P14577. 磁极变换

    ID: 13378 远端评测题 600~1200ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>洛谷原创O2优化ST 表位运算洛谷月赛

磁极变换

Description

Morey specializes in magnets.

In the course of the research, Morey has manufactured magnets made of 2626 different materials, denoted by the lowercase Latin letters.

Morey has implemented a spectacular technique. First, all the magnets are laid out in a row. When a button is pressed, then for each material type separately, the 1st, 3rd, 5th, ... occurrences become north-poles (N), and the 2nd, 4th, 6th, ... occurrences become south-poles (S). Then, for each material separately, for each positive integer kk, the (2k1)(2k-1)-th and the (2k)(2k)-th magnets of that material attract each other and move toward each other until they collide—if both exist. During the collision, all magnets in the interval between them (inclusive) are destroyed. All such collisions happen simultaneously.

In addition, the ii-th magnet has an associated value aia_i.

If we represent the sequence of magnet materials by a string SS, we denote by f(S)f(S) the sum of the values of the magnets that remain after applying this technique.

Now, there are nn magnets placed in the machine in positions 11 through nn. You are given their material types and values.

Morey wants to process two types of operations:

Update: Change the value of a single magnet.

Query: For a given index interval [l,r][l,r], apply the technique only to the magnets in that interval, and report the sum of the values of the magnets that remain in that interval, i.e. f(S[l,r])f(S_{[l,r]}).

Formally, the operations are:

  • 1 x y: Set axya_x \gets y.

  • 2 l r: Output f(S[l,r])f(S_{[l,r]}).

Input Format

The first line contains a positive integer nn, the number of magnets.

The second line contains a string SS of length nn, consisting of lowercase letters, where SiS_i is the material of the ii-th magnet.

The third line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n, the initial values of the magnets.

The fourth line contains a positive integer qq, the number of operations.

Each of the next qq lines contains three integers describing one operation as above.

Output Format

For each query, output one line containing the integer f(S[l,r])f(S_{[l,r]}).

6
iakioi
1 -4 2 7 -5 3
3
2 1 6
2 1 5
2 3 6
-2
-5
2
6
ecbeca
-1 -6 4 8 2 5
3
2 1 6
1 6 4
2 1 6
5
4

Hint

Hint: Please use faster input/output methods.

Explanation of Examples

For Sample 1:

  • Query [1,6][1,6]: Material i occurs at positions 1,4,61, 4, 6. Magnets at positions 11 and 44 attract and collide, destroying everything between them (i.e., positions [1,4][1,4]). Only magnets at positions 55 and 66 remain , so the remaining sum is 5+3=2-5 + 3 = -2.

  • Query [3,6][3,6]: Material i occurs at positions 44 and 66. Magnets at positions 44 and 66 attract and collide, destroying everything between them (i.e., positions [4,6][4,6]). Only magnets at positions 33 remains with value 22.

For Sample 2: Magnets at positions 11 and 44 attract and collide, destroying everything between them (i.e., positions [1,4][1,4]). Meanwhile, Magnets at positions 22 and 55 attract and collide, destroying everything between them (i.e., positions [2,5][2,5]). Only position 66 remains.

Subtask nn \le qq \le Special Properties Score Time Limit
Subtask 1 10001000 None 1010 0.6s
Subtask 2 10410^4
Subtask 3 2×1052\times 10^5 A 2020 1.2s
Subtask 4 None
Subtask 5 5×1055\times 10^5 5×1055 \times {10}^5 B 3030
Subtask 6 5×1055\times 10^5 None 1010

Special Property A: SS contains only the first 88 letters of the alphabet.

Special Property B: There are no update operations.

For all data, 1n,q5×1051 \le n,q \le 5\times10^5, ai109|a_i| \le 10^9, and SS consists only of lowercase Latin letters.