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

#FT0000001. Cây Fenwick 1 – Cộng điểm và tổng đoạn (Fenwick Tree 1 - Point Add and Range Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cây Fenwick 1 – Cộng điểm và tổng đoạn (Fenwick Tree 1 - Point Add and Range Sum)

Fenwick Tree 1 - Point Add and Range Sum

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, indexed from 11 to nn. The elements may change while mm operations are processed in the input order.

An operation 1 x k adds integer kk to only the element at position xx, so its new value is ax+ka_x+k. All other elements remain unchanged. The increment may be negative.

An operation 2 x y asks for ax+ax+1+⋯+aya_x+a_{x+1}+\cdots+a_y, the sum of the consecutive elements from position xx through yy, including both endpoints. The result uses the current sequence after all preceding updates; a query does not modify the sequence.

Input

  • Line 1: n m gives array length and operation count.
  • Line 2: a_1 a_2 ... a_n gives the initial values in index order.
  • Next mm lines: each line contains one of the following commands, in execution order:
    • 1 x k: add kk to position xx.
    • 2 x y: query the inclusive sum from xx through y.y.

Output

For each 2 x y 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 (30 points):

  • 1≤n≤81\le n\le 8.
  • 1≤m≤101\le m\le 10.
  • −231≤ai,k<231-2^{31}\le a_i,k<2^{31}.
  • 1≤x≤n1\le x\le n for every 1 x k command.
  • 1≤x≤y≤n1\le x\le y\le n for every 2 x y query.
  • Every range sum remains in [−231,231)[-2^{31},2^{31}) at all times.

Subtask 2 (40 points):

  • 1≤n≤100001\le n\le 10000.
  • 1≤m≤100001\le m\le 10000.
  • −231≤ai,k<231-2^{31}\le a_i,k<2^{31}.
  • 1≤x≤n1\le x\le n for every 1 x k command.
  • 1≤x≤y≤n1\le x\le y\le n for every 2 x y query.
  • Every range sum remains in [−231,231)[-2^{31},2^{31}) at all times.

Subtask 3 (30 points):

  • 1≤n≤5000001\le n\le 500000.
  • 1≤m≤5000001\le m\le 500000.
  • −231≤ai,k<231-2^{31}\le a_i,k<2^{31}.
  • 1≤x≤n1\le x\le n for every 1 x k command.
  • 1≤x≤y≤n1\le x\le y\le n for every 2 x y query.
  • Every range sum remains in [−231,231)[-2^{31},2^{31}) at all times.

Examples

Input

5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4

Output

14
16

Explanation

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

  1. Command 1 — 1 1 3: At position x=1x=1, the value changes from 11 to 1+(3)=41+(3)=4. The new array is [4, 5, 4, 2, 3].

  2. Command 2 — 2 2 5: The inclusive interval [2,5][2,5] contains [5, 4, 2, 3]. Its sum is 5+4+2+3=145 + 4 + 2 + 3=14, so print 14.

  3. Command 3 — 1 3 -1: At position x=3x=3, the value changes from 44 to 4+(−1)=34+(-1)=3. The new array is [4, 5, 3, 2, 3].

  4. Command 4 — 1 4 2: At position x=4x=4, the value changes from 22 to 2+(2)=42+(2)=4. The new array is [4, 5, 3, 4, 3].

  5. Command 5 — 2 1 4: The inclusive interval [1,4][1,4] contains [4, 5, 3, 4]. Its sum is 4+5+3+4=164 + 5 + 3 + 4=16, so print 16.

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