PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000022. Các loại đá quý của trợ thủ (Just for Sidekicks)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các loại đá quý của trợ thủ (Just for Sidekicks)

Just for Sidekicks

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

You have a row of nn gems, at positions numbered from 11 through nn. Each gem belongs to one of six types, labeled by the digits 1 through 6. Initially type pp has value vpv_p; a string specifies the type of the gem at each position.

Process qq commands in order. Command 1 k p changes the gem at position kk to type pp, without altering the listed price of type pp. Command 2 p v changes the price of the entire gem type pp to vv: every gem currently of that type is now valued at the new price, even though none changes type or location.

Command 3 l r asks for the sum of the current gem values in positions ll through rr inclusive. Each gem's value is always determined by the current price of its type at the time of the query.

Input

  • Line 1: n q gives number of gems and commands.
  • Line 2: v_1 v_2 v_3 v_4 v_5 v_6 gives the initial prices of gem types 1 through 6.
  • Line 3: a string S of exactly nn digits from 1 through 6; the kk-th digit is the gem type at position kk.
  • Next qq lines: each line contains one of the following commands, in execution order:
    • 1 k p: change the gem at position kk to type pp.
    • 2 p v: set the price of gem type pp to vv.
    • 3 l r: query the gem value from ll through rr.

Output

For each 3 l r command, print the requested integer computed from the current state on its own line, in query order. Update commands produce no output.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤q≤301\le q\le 30.
  • 1≤vp,v≤1091\le v_p,v\le 10^9 for all 1≤p≤61\le p\le 6.
  • the gem string contains exactly nn characters, each in {1,2,3,4,5,6}\{1,2,3,4,5,6\}.
  • command 1: 1≤k≤n1\le k\le n, 1≤p≤61\le p\le 6.
  • command 2: 1≤p≤61\le p\le 6.
  • command 3: 1≤l≤r≤n1\le l\le r\le n.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 1≤vp,v≤1091\le v_p,v\le 10^9 for all 1≤p≤61\le p\le 6.
  • the gem string contains exactly nn characters, each in {1,2,3,4,5,6}\{1,2,3,4,5,6\}.
  • command 1: 1≤k≤n1\le k\le n, 1≤p≤61\le p\le 6.
  • command 2: 1≤p≤61\le p\le 6.
  • command 3: 1≤l≤r≤n1\le l\le r\le n.

Subtask 3 (50 points):

  • 1≤n≤2000001\le n\le 200000.
  • 1≤q≤2000001\le q\le 200000.
  • 1≤vp,v≤1091\le v_p,v\le 10^9 for all 1≤p≤61\le p\le 6.
  • the gem string contains exactly nn characters, each in {1,2,3,4,5,6}\{1,2,3,4,5,6\}.
  • command 1: 1≤k≤n1\le k\le n, 1≤p≤61\le p\le 6.
  • command 2: 1≤p≤61\le p\le 6.
  • command 3: 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

5 7
1 2 3 4 5 6
12361
3 1 5
2 6 10
3 2 4
1 1 6
3 1 2
2 1 20
3 1 5

Output

13
15
12
45

Explanation

There are n=5n=5 gems with type string 12361, representing types [1,2,3,6,1]. Their initial type prices are [1,2,3,4,5,6].

  1. 3 1 5: the gem values are 1,2,3,6,11,2,3,6,1; their sum is 1+2+3+6+1=131+2+3+6+1=13. Print 13.

  2. 2 6 10: the price of type 6 changes from 66 to 1010; the type string remains 12361.

  3. 3 2 4: the gem types at positions 2, 3, and 4 are [2,3,6], with current prices 2,3,102,3,10; the sum is 2+3+10=152+3+10=15. Print 15.

  4. 1 1 6: change the first gem from type 1 to type 6, making the string 62361.

  5. 3 1 2: the first two gem values are 1010 and 22, summing to 1212. Print 12.

  6. 2 1 20: type 1's price changes from 11 to 2020. The last gem is type 1 and is now worth 2020, without changing its type.

  7. 3 1 5: the types [6,2,3,6,1] currently have values 10,2,3,10,2010,2,3,10,20. Their sum is 10+2+3+10+20=4510+2+3+10+20=45. Print 45.

The four query answers are 13, 15, 12, and 45.