#FT0000008. Fenwick – Tổng tiền tố (Fenwick)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Fenwick – Tổng tiền tố (Fenwick)
Fenwick
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Consider an array of elements indexed through , with every element initially zero. Process update and prefix-sum commands.
Command + i v adds to the element at index . Command ? i asks for the sum of exactly the first elements, namely . For , the sum is empty and equals ; for , 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 qgives array length and command count. - Next lines: each line contains one of the following commands, in execution order:
+ i v: add to the element at index .? i: query the sum of the first elements.
Output
For each ? i command, print the sum of exactly the first elements of the current array, one integer per line. If , print .
Subtasks
Subtask 1 (20 points):
- .
- .
- for command
+. - for command
?. - .
Subtask 2 (30 points):
- .
- .
- for command
+. - for command
?. - .
Subtask 3 (50 points):
- .
- .
- for command
+. - for command
?. - .
Examples
Input
5 5
+ 0 5
+ 3 2
? 4
+ 0 1
? 1
Output
7
6
Explanation
There are elements. All values are initially .
-
Command 1 —
+ 0 5: Element increases from to . Current array:[5, 0, 0, 0, 0]. -
Command 2 —
+ 3 2: Element increases from to . Current array:[5, 0, 0, 2, 0]. -
Command 3 —
? 4: Query the first 4 elements: . Print7. -
Command 4 —
+ 0 1: Element increases from to . Current array:[6, 0, 0, 2, 0]. -
Command 5 —
? 1: Query the first 1 elements: . Print6.
Collect the answers to output-producing commands in their input order, printing one answer per line.