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

#FT0000005. Robin Hood tò mò (Curious Robin Hood)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Robin Hood tò mò (Curious Robin Hood)

Curious Robin Hood

Source: LightOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Robin Hood stores his money in nn sacks, numbered 00 through n−1n-1. Initially, sack ii holds aia_i coins. Process qq actions in the order supplied.

Command 1 i: remove all coins currently in sack ii and give them away. Print the amount removed and set that sack's contents to 00. If the sack is already empty, the reported amount is 00.

Command 2 i v: add vv coins to sack ii, leaving every other sack unchanged. Command 3 i j: report the total number of coins now contained in sacks ii through jj, including both endpoints, without changing the sacks.

Output the results of commands 1 and 3 in their occurrence order, using the current state after preceding actions.

Input

  • Line 1: n q gives number of sacks and commands.
  • Line 2: a_0 a_1 ... a_(n-1) gives the initial values in index order.
  • Next qq lines: each line contains one of the following commands, in execution order:
    • 1 i: empty sack ii and output the amount removed.
    • 2 i v: add vv coins to sack ii.
    • 3 i j: calculate the total number of coins in sacks ii through jj, including both endpoints.

Output

Command 1 i outputs the number of coins removed before emptying sack ii. Command 3 i j outputs the current sum of money in sacks ii through jj. Print one line for each such command in input order. Command 2 has no output.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤q≤301\le q\le 30.
  • 0≤ai≤10000\le a_i\le 1000.
  • 1≤v≤10001\le v\le 1000.
  • 0≤i≤j<n0\le i\le j<n in command 3.
  • 0≤i<n0\le i<n in command 1, 2.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 0≤ai≤10000\le a_i\le 1000.
  • 1≤v≤10001\le v\le 1000.
  • 0≤i≤j<n0\le i\le j<n in command 3.
  • 0≤i<n0\le i<n in command 1, 2.

Subtask 3 (50 points):

  • 1≤n≤1000001\le n\le 100000.
  • 1≤q≤500001\le q\le 50000.
  • 0≤ai≤10000\le a_i\le 1000.
  • 1≤v≤10001\le v\le 1000.
  • 0≤i≤j<n0\le i\le j<n in command 3.
  • 0≤i<n0\le i<n in command 1, 2.

Examples

Input

5 7
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
2 0 3
3 0 1

Output

5
14
1
13
8

Explanation

There are n=5n=5 elements. The initial values are [3, 2, 1, 4, 5].

  1. Command 1 — 1 4: Sack 44 contains 55 coins. Remove and print 5, then set the sack to 00: [3, 2, 1, 4, 0].

  2. Command 2 — 2 3 4: Add 44 coins to sack 33: 4+4=84+4=8. The sacks now hold [3, 2, 1, 8, 0].

  3. Command 3 — 3 0 3: Sum sacks 00 through 33: 3+2+1+8=143 + 2 + 1 + 8=14. Print 14 without changing any sack.

  4. Command 4 — 1 2: Sack 22 contains 11 coins. Remove and print 1, then set the sack to 00: [3, 2, 0, 8, 0].

  5. Command 5 — 3 0 4: Sum sacks 00 through 44: 3+2+0+8+0=133 + 2 + 0 + 8 + 0=13. Print 13 without changing any sack.

  6. Command 6 — 2 0 3: Add 33 coins to sack 00: 3+3=63+3=6. The sacks now hold [6, 2, 0, 8, 0].

  7. Command 7 — 3 0 1: Sum sacks 00 through 11: 6+2=86 + 2=8. Print 8 without changing any sack.

Collect the answers to output-producing commands in their input order, printing one answer per line.