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

#FT0000002. Cây Fenwick – Truy vấn nửa mở (Fenwick Tree - Half-open Range Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cây Fenwick – Truy vấn nửa mở (Fenwick Tree - Half-open Range Queries)

Fenwick Tree - Half-open Range Queries

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of nn nonnegative integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}. Its elements are indexed from 00 to n−1n-1, rather than from 11. Process qq operations in the order received.

An operation 0 p x adds xx to apa_p. An operation 1 l r asks for the sum of values at indices in the half-open interval [l,r)[l,r): those indices ii for which l≤i<rl\le i<r. In particular, the left endpoint is included, but the right endpoint is excluded. The value r=nr=n is allowed.

Each query must use the array after all earlier updates. Print the answer for every type-1 operation in its original order.

Input

  • Line 1: n q gives array length and operation count.
  • 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:
    • 0 p x: add xx at index pp.
    • 1 l r: query the half-open range sum [l,r)[l,r).

Output

For each 1 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.
  • 0≤ai,x≤1090\le a_i,x\le 10^9.
  • 0≤p<n0\le p<n.
  • 0≤l<r≤n0\le l<r\le n.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 0≤ai,x≤1090\le a_i,x\le 10^9.
  • 0≤p<n0\le p<n.
  • 0≤l<r≤n0\le l<r\le n.

Subtask 3 (50 points):

  • 1≤n≤5000001\le n\le 500000.
  • 1≤q≤5000001\le q\le 500000.
  • 0≤ai,x≤1090\le a_i,x\le 10^9.
  • 0≤p<n0\le p<n.
  • 0≤l<r≤n0\le l<r\le n.

Examples

Input

5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3

Output

15
7
25
6

Explanation

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

  1. Command 1 — 1 0 5: Half-open [0,5)[0,5) includes indices 00 through 44, namely [1, 2, 3, 4, 5]; the sum is 1+2+3+4+5=151 + 2 + 3 + 4 + 5=15. Index 55 is excluded.

  2. Command 2 — 1 2 4: Half-open [2,4)[2,4) includes indices 22 through 33, namely [3, 4]; the sum is 3+4=73 + 4=7. Index 44 is excluded.

  3. Command 3 — 0 3 10: Add 1010 to a3a_3 (previously 44), obtaining 1414. Current array: [1, 2, 3, 14, 5].

  4. Command 4 — 1 0 5: Half-open [0,5)[0,5) includes indices 00 through 44, namely [1, 2, 3, 14, 5]; the sum is 1+2+3+14+5=251 + 2 + 3 + 14 + 5=25. Index 55 is excluded.

  5. Command 5 — 1 0 3: Half-open [0,3)[0,3) includes indices 00 through 22, namely [1, 2, 3]; the sum is 1+2+3=61 + 2 + 3=6. Index 33 is excluded.

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