The problem requires processing queries of two types: assigning a value to one array element and computing the sum of the elements on the segment [l; r]. If we store only the array itself, an assignment takes , but each sum query requires operations. If we store prefix sums, a sum is computed in , but each assignment requires recomputing prefix sums. With a large number of queries, both approaches are too slow.
We use a segment tree for sums. Each of its vertices stores the sum of the elements of its segment, and the value in an internal vertex equals the sum of the values of its two children. The segment tree performs both operations in O(\log n). When computing a sum, an empty part of a segment corresponds to — the neutral element for addition.
Initially all array elements are zero, so the sums in all vertices of the tree are zero as well. It is enough to fill the tree array with zeros; no separate construction of the tree is needed.
The values of the elements reach 10^9, so the sum on a segment can reach 10^5 \cdot 10^9 = 10^{14} and does not fit into the int type. The values in the tree are stored in the long long type.
Let us consider a solution using a Fenwick tree. It supports two operations, each performed in O(\log n):
adding a number to the element a_i;
computing the prefix sum sum(i) = a_0 + a_1 + \ldots + a_i.
Initially all array elements are zero, so a Fenwick tree filled with zeros already corresponds to the initial array. The indices in the queries range from to ; the element a_0 is not used and is always zero.
Assignment. A Fenwick tree can only add a number to an element, but cannot assign a new value to it. However, the current value of an element can be computed via prefix sums:
Therefore, the assignment a_i = x is replaced by adding to a_i the difference between the new and the old values:
Sum on a segment. The sum of the elements on the segment [l; r] equals the difference of two prefix sums:
The values of the elements reach 10^9, so the sums can reach 10^{14} and are stored in the long long type.
Example
Initially the array consists of zeros. After the queries , and , the array becomes (0, 2, 1, 2, 0). The segment tree for it is shown below. Each vertex shows its segment and the sum of the elements on it.
When the query is executed, only the vertices on the path from the leaf to the root are recomputed: the leaf receives the value , the vertex — the value 2 + 1 = 3, and the root — the value 3 + 2 = 5.
The queries , , , and return the values of the leaves: 0, 2, 1, 2, 0. The segment of the query coincides with the segment of the root, so the answer is taken directly from the root and equals (the highlighted vertex).
Let us simulate the queries from the example using the Fenwick tree, n = 5. Initially the array is (0, 0, 0, 0, 0). For each assignment query, the table shows how the old value of the element is computed and which number is added; for each sum query, it shows the difference of prefix sums.
query | computation | result |
old value sum(2) - sum(1) = 0 - 0 = 0, add 2 - 0 = 2 | (0, 2, 0, 0, 0) | |
old value sum(3) - sum(2) = 2 - 2 = 0, add 1 - 0 = 1 | (0, 2, 1, 0, 0) | |
old value sum(4) - sum(3) = 3 - 3 = 0, add 2 - 0 = 2 | (0, 2, 1, 2, 0) | |
sum(1) - sum(0) = 0 - 0 | ||
sum(2) - sum(1) = 2 - 0 | ||
sum(3) - sum(2) = 3 - 2 | ||
sum(4) - sum(3) = 5 - 3 | ||
sum(5) - sum(4) = 5 - 5 | ||
sum(5) - sum(0) = 5 - 0 |
For example, before the query the array is (0, 2, 1, 2, 0), so sum(4) = 0 + 2 + 1 + 2 = 5, sum(3) = 0 + 2 + 1 = 3, and the answer is 5 - 3 = 2.
In the example, all assignments are applied to zero elements. Let us see what would happen with a repeated assignment, for example at the end. The old value is sum(3) - sum(2) = 3 - 2 = 1, so 5 - 1 = 4 is added to a_3, and the element becomes .
Algorithm implementation
The segment tree is stored in the array SegTree of length 4 \cdot MAX, where is the maximum number of array elements. A sum on a segment may exceed the limits of the int type, so the values in the tree have the long long type.
#define MAX 100010
long long SegTree[4 * MAX];The function Summa returns the sum of the elements on the segment [left; right]. Vertex corresponds to the segment [lpos; rpos].
long long Summa(int v, int lpos, int rpos, int left, int right)
{If the query segment is empty (left > right), return — the neutral element for addition.
if (left > right) return 0;If the query segment coincides with the segment [lpos; rpos] of vertex , return the sum stored in it.
if ((left == lpos) && (right == rpos))
return SegTree[v];Otherwise, find the middle of the segment mid = (lpos + rpos) / 2.
int mid = (lpos + rpos) / 2;Split the query segment into two parts: [left; \min(right, mid)] lies in the segment of the left child, and [\max(left, mid + 1); right] lies in the segment of the right child. One of the parts may be empty. Recursively compute the sums on both parts and return their sum.
return Summa(2*v, lpos, mid, left, min(right, mid)) +
Summa(2*v+1, mid + 1, rpos, max(left, mid + 1), right);
}The function update assigns the value to the element with index . Vertex corresponds to the segment [lpos; rpos].
void update(int v, int lpos, int rpos, int pos, int val)
{If the segment of vertex consists of one element (lpos = rpos), then is the leaf corresponding to position . Write the value into it.
if (lpos == rpos)
SegTree[v] = val;
else
{Otherwise, determine which half of the segment contains position , and recursively descend into the left or right child.
int mid = (lpos + rpos) / 2;
if (pos <= mid) update(2*v, lpos, mid, pos, val);
else update(2*v+1, mid + 1, rpos, pos, val);After returning from the recursion, recompute the sum in vertex . In this way, all vertices on the path from the leaf to the root are updated.
SegTree[v] = SegTree[2*v] + SegTree[2*v+1];
}
}The main part of the program. Initially all array elements are zero, so all sums in the segment tree are zero as well. Fill the array SegTree with zeros; no separate construction of the tree is needed.
memset(SegTree, 0, sizeof(SegTree));Read the size of the array and the number of queries . The \n character in the format skips the line break, so that the next character read is the query letter.
scanf("%d %d\n", &n, &k);Process the queries one by one. First, read the query letter . If it is 'A', assign the value to the element with index . Otherwise, print the sum on the segment [l; r]. The \n character in the format "%lld %lld\n" skips the line break after the query.
for (j = 0; j < k; j++)
{
scanf("%c", &command);
if (command == 'A')
{
scanf("%lld %lld\n", &i, &x);
update(1, 1, n, i, x);
}
else
{
scanf("%lld %lld\n", &l, &r);
printf("%lld\n", Summa(1, 1, n, l, r));
}
}Algorithm implementation — Fenwick tree
The array Fenwick of size stores the Fenwick tree. A global array is initially filled with zeros, which corresponds to the initial zero array.
#define MAX 100010
long long Fenwick[MAX];The function Summma0_i returns the prefix sum sum(i) = a_0 + a_1 + \ldots + a_i.
// Fenwick[0] + Fenwick[1] + ... + Fenwick[i]
long long Summma0_i(long long i)
{
long long result = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
result += Fenwick[i];
return result;
}The function IncElement adds to the element a_i. The indices of the elements range from to , so the loop runs while i \le n.
// Fenwick[i] = Fenwick[i] + delta
void IncElement(long long i, long long delta)
{
for (; i <= n; i = (i | (i+1)))
Fenwick[i] += delta;
}The main part of the program. Read the size of the array and the number of queries . The \n character in the format skips the line break, so that the next character read is the query letter.
scanf("%d %d\n",&n,&k);Process the queries one by one. First, read the query letter .
for(j = 0; j < k; j++)
{
scanf("%c",&command);Query . The current value of the element a_i equals sum(i) - sum(i - 1). To assign the value to it, replace with the difference x - sum(i) + sum(i - 1) and add it to a_i using the function IncElement.
if (command == 'A')
{
scanf("%lld %lld\n",&i,&x);
x = x - Summma0_i(i) + Summma0_i(i-1);
IncElement(i,x);Query . The sum on the segment [l; r] equals sum(r) - sum(l - 1).
} else
{
scanf("%lld %lld\n",&l,&r);
printf("%lld\n",Summma0_i(r) - Summma0_i(l-1));
}
}