#FT0000001. Cây Fenwick 1 – Cộng điểm và tổng đoạn (Fenwick Tree 1 - Point Add and Range Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 integers , indexed from to . The elements may change while operations are processed in the input order.
An operation 1 x k adds integer to only the element at position , so its new value is . All other elements remain unchanged. The increment may be negative.
An operation 2 x y asks for , the sum of the consecutive elements from position through , including both endpoints. The result uses the current sequence after all preceding updates; a query does not modify the sequence.
Input
- Line 1:
n mgives array length and operation count. - Line 2:
a_1 a_2 ... a_ngives the initial values in index order. - Next lines: each line contains one of the following commands, in execution order:
1 x k: add to position .2 x y: query the inclusive sum from through
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):
- .
- .
- .
- for every
1 x kcommand. - for every
2 x yquery. - Every range sum remains in at all times.
Subtask 2 (40 points):
- .
- .
- .
- for every
1 x kcommand. - for every
2 x yquery. - Every range sum remains in at all times.
Subtask 3 (30 points):
- .
- .
- .
- for every
1 x kcommand. - for every
2 x yquery. - Every range sum remains in 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 elements. The initial values are [1, 5, 4, 2, 3].
-
Command 1 —
1 1 3: At position , the value changes from to . The new array is[4, 5, 4, 2, 3]. -
Command 2 —
2 2 5: The inclusive interval contains[5, 4, 2, 3]. Its sum is , so print14. -
Command 3 —
1 3 -1: At position , the value changes from to . The new array is[4, 5, 3, 2, 3]. -
Command 4 —
1 4 2: At position , the value changes from to . The new array is[4, 5, 3, 4, 3]. -
Command 5 —
2 1 4: The inclusive interval contains[4, 5, 3, 4]. Its sum is , so print16.
Collect the answers to output-producing commands in their input order, printing one answer per line.