#FT0000002. Cây Fenwick – Truy vấn nửa mở (Fenwick Tree - Half-open Range Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 nonnegative integers . Its elements are indexed from to , rather than from . Process operations in the order received.
An operation 0 p x adds to . An operation 1 l r asks for the sum of values at indices in the half-open interval : those indices for which . In particular, the left endpoint is included, but the right endpoint is excluded. The value 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 qgives array length and operation count. - Line 2:
a_0 a_1 ... a_(n-1)gives the initial values in index order. - Next lines: each line contains one of the following commands, in execution order:
0 p x: add at index .1 l r: query the half-open range sum .
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):
- .
- .
- .
- .
- .
Subtask 2 (30 points):
- .
- .
- .
- .
- .
Subtask 3 (50 points):
- .
- .
- .
- .
- .
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 elements. The initial values are [1, 2, 3, 4, 5].
-
Command 1 —
1 0 5: Half-open includes indices through , namely[1, 2, 3, 4, 5]; the sum is . Index is excluded. -
Command 2 —
1 2 4: Half-open includes indices through , namely[3, 4]; the sum is . Index is excluded. -
Command 3 —
0 3 10: Add to (previously ), obtaining . Current array:[1, 2, 3, 14, 5]. -
Command 4 —
1 0 5: Half-open includes indices through , namely[1, 2, 3, 14, 5]; the sum is . Index is excluded. -
Command 5 —
1 0 3: Half-open includes indices through , namely[1, 2, 3]; the sum is . Index is excluded.
Collect the answers to output-producing commands in their input order, printing one answer per line.