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

#FT0000011. Cây Fenwick 2 – Cộng đoạn và hỏi điểm (Fenwick Tree 2 - Range Add Point Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cây Fenwick 2 – Cộng đoạn và hỏi điểm (Fenwick Tree 2 - Range Add Point Query)

Fenwick Tree 2 - Range Add Point Query

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of nn integers indexed from 11. An update can change several consecutive elements at once, and an element may be affected by multiple earlier updates.

Operation 1 l r k adds integer kk to each element al,al+1,…,ara_l,a_{l+1},\ldots,a_r. Both endpoints are included, and kk may be negative. Where updated intervals overlap, their increments accumulate.

Operation 2 x requests the current value of the single element axa_x, not the prefix sum through xx. Apply operations in order and print one answer for every type-2 command.

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 l r k: add kk to all elements in [l,r][l,r].
    • 2 x: query the current value of axa_x.

Output

For each 2 x 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.
  • ai,ka_i,k are integers.
  • 1≤l≤r≤n1\le l\le r\le n and −231≤k<231-2^{31}\le k<2^{31} in command 1 l r k.
  • 1≤x≤n1\le x\le n in query 2 x.
  • ∣ai∣≤230|a_i|\le 2^{30} for every element at every moment.

Subtask 2 (40 points):

  • 1≤n≤100001\le n\le 10000.
  • 1≤m≤100001\le m\le 10000.
  • ai,ka_i,k are integers.
  • 1≤l≤r≤n1\le l\le r\le n and −231≤k<231-2^{31}\le k<2^{31} in command 1 l r k.
  • 1≤x≤n1\le x\le n in query 2 x.
  • ∣ai∣≤230|a_i|\le 2^{30} for every element at every moment.

Subtask 3 (30 points):

  • 1≤n≤5000001\le n\le 500000.
  • 1≤m≤5000001\le m\le 500000.
  • ai,ka_i,k are integers.
  • 1≤l≤r≤n1\le l\le r\le n and −231≤k<231-2^{31}\le k<2^{31} in command 1 l r k.
  • 1≤x≤n1\le x\le n in query 2 x.
  • ∣ai∣≤230|a_i|\le 2^{30} for every element at every moment.

Examples

Input

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

Output

6
10

Explanation

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

  1. Command 1 — 1 2 4 2: Add 22 to every element of [2,4][2,4]; updated array: [1, 7, 6, 4, 3].

  2. Command 2 — 2 3: The element at position 33 currently equals 66; print 6. This is not a prefix sum.

  3. Command 3 — 1 1 5 -1: Add −1-1 to every element of [1,5][1,5]; updated array: [0, 6, 5, 3, 2].

  4. Command 4 — 1 3 5 7: Add 77 to every element of [3,5][3,5]; updated array: [0, 6, 12, 10, 9].

  5. Command 5 — 2 4: The element at position 44 currently equals 1010; print 10. This is not a prefix sum.

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