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

#FT0000008. Fenwick – Tổng tiền tố (Fenwick)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Fenwick – Tổng tiền tố (Fenwick)

Fenwick

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an array of nn elements indexed 00 through n−1n-1, with every element initially zero. Process qq update and prefix-sum commands.

Command + i v adds vv to the element at index ii. Command ? i asks for the sum of exactly the first ii elements, namely a0+a1+⋯+ai−1a_0+a_1+\cdots+a_{i-1}. For i=0i=0, the sum is empty and equals 00; for i=ni=n, the query covers the entire array.

The update index is zero-based, whereas the parameter of ? specifies a count of leading elements, not the index of the last included element. Updates remain in effect for later queries.

Input

  • Line 1: n q gives array length and command count.
  • Next qq lines: each line contains one of the following commands, in execution order:
    • + i v: add vv to the element at index ii.
    • ? i: query the sum of the first ii elements.

Output

For each ? i command, print the sum of exactly the first ii elements of the current array, one integer per line. If i=0i=0, print 00.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤q≤301\le q\le 30.
  • 0≤i<n0\le i<n for command +.
  • 0≤i≤n0\le i\le n for command ?.
  • 0≤v≤1090\le v\le 10^9.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 0≤i<n0\le i<n for command +.
  • 0≤i≤n0\le i\le n for command ?.
  • 0≤v≤1090\le v\le 10^9.

Subtask 3 (50 points):

  • 1≤n≤10000001\le n\le 1000000.
  • 1≤q≤10000001\le q\le 1000000.
  • 0≤i<n0\le i<n for command +.
  • 0≤i≤n0\le i\le n for command ?.
  • 0≤v≤1090\le v\le 10^9.

Examples

Input

5 5
+ 0 5
+ 3 2
? 4
+ 0 1
? 1

Output

7
6

Explanation

There are n=5n=5 elements. All values are initially 00.

  1. Command 1 — + 0 5: Element 00 increases from 00 to 0+5=50+5=5. Current array: [5, 0, 0, 0, 0].

  2. Command 2 — + 3 2: Element 33 increases from 00 to 0+2=20+2=2. Current array: [5, 0, 0, 2, 0].

  3. Command 3 — ? 4: Query the first 4 elements: 5+0+0+2=75 + 0 + 0 + 2=7. Print 7.

  4. Command 4 — + 0 1: Element 00 increases from 55 to 5+1=65+1=6. Current array: [6, 0, 0, 2, 0].

  5. Command 5 — ? 1: Query the first 1 elements: 6=66=6. Print 6.

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