We use a segment tree with lazy propagation. Since we are only interested in divisibility by , it is enough to know the remainders of the numbers modulo . Each vertex of the segment tree is assigned an array , where is the number of elements of its segment that give remainder when divided by . Then the answer to a query of type is the sum of the values over the fundamental segments into which the segment is split.
Consider how a vertex changes when all numbers of its segment are increased by . A number with remainder gets remainder , with remainder — remainder , and with remainder — remainder . Therefore, the array is cyclically shifted by one position: the new value of \mathit{cnt}[(t + 1) \bmod 3] equals the old value of . For example, for the numbers 0, 1, 1, 0 we have \mathit{cnt} = (2, 2, 0), and after adding one we get the numbers 1, 2, 2, 1 and \mathit{cnt} = (0, 2, 2).
Thus, the new state of a vertex is computed in without accessing its children. Therefore, during a range increment we update only the vertices of the fundamental segments and defer the increment for their children: the field stores the value (modulo ) by which all numbers of the subtree must be increased. When descending into the children, the deferred value is pushed down.
Both operations take O(\log n), and the whole program runs in O(n + q \log n).
Example
Let us trace the operations from the example for n = 4. Initially x = (0, 0, 0, 0).
Operation 1 0 3: all four numbers are divisible by , the answer is .
Operation 0 1 2: x = (0, 1, 1, 0). The segment is split into the fundamental segments and — two leaves from different subtrees. Their arrays are shifted, after which the function Pull recomputes the values of their ancestors.
Operation 0 1 3: x = (0, 2, 2, 1). Here the segment is split into the fundamental segments and . The vertex is updated as a whole: its array is cyclically shifted, and is written to the field . The children of this vertex are not changed yet.
Operation 1 0 0: x_0 = 0 is divisible by , the answer is .
Operation 0 0 3: all numbers are increased, x = (1, 0, 0, 2). The segment coincides with the root, so only the array of the root is shifted: (1, 1, 2) \to (2, 1, 1).
Operation 1 3 3: while descending to the leaf , we push the deferred increments. We get x_3 = 2, the answer is .
Operation 1 0 3: the answer equals of the root, that is, (the numbers x_1 = 0 and x_2 = 0).
Algorithm Implementation
A vertex of the segment tree is stored in the structure node. The value equals the number of elements of the corresponding segment that give remainder when divided by . The field contains the deferred increment modulo that has not yet been passed to the children of the vertex.
struct node
{
int cnt[3]; // cnt[t] - number of elements with remainder t modulo 3
int add; // deferred increment (modulo 3) for the children
};The segment tree is stored in the vector .
vector<node> SegTree;The function Shift increases all numbers of the segment corresponding to vertex by . A number with remainder will have remainder (t + s) \bmod 3 after the increase, so the value is moved to the cell c[(t + s) \bmod 3]. The array is thus cyclically shifted by positions. The increment itself is stored in to be passed to the children later.
// increase all numbers in vertex v by s
void Shift(int v, int s)
{
int c[3];
for (int t = 0; t < 3; t++)
c[(t + s) % 3] = SegTree[v].cnt[t];
for (int t = 0; t < 3; t++)
SegTree[v].cnt[t] = c[t];
SegTree[v].add = (SegTree[v].add + s) % 3;
}The function Push performs the push: if vertex has a deferred increment, apply it to both children and reset in vertex .
void Push(int v)
{
if (SegTree[v].add == 0) return;
Shift(2*v, SegTree[v].add);
Shift(2*v+1, SegTree[v].add);
SegTree[v].add = 0;
}The function Pull recomputes the values of vertex from the values of its children: the number of elements with remainder in the segment equals the sum of such counts in the left and right halves.
void Pull(int v)
{
for (int t = 0; t < 3; t++)
SegTree[v].cnt[t] = SegTree[2*v].cnt[t] + SegTree[2*v+1].cnt[t];
}The function BuildTree builds the segment tree. Initially all numbers are , so each leaf has \mathit{cnt}[0] = 1, and the other values are zero. For an internal vertex, we build both children and compute its values with the function Pull.
void BuildTree(int v, int lpos, int rpos)
{
if (lpos == rpos)
SegTree[v].cnt[0] = 1;
else
{
int mid = (lpos + rpos) / 2;
BuildTree(2*v, lpos, mid);
BuildTree(2*v+1, mid+1, rpos);
Pull(v);
}
}The function Update increases by all numbers in the segment [\mathit{left}; \mathit{right}]. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}]. If the query segment is empty, return. If the query segment coincides with the segment of the vertex, apply a shift by to the vertex and do not descend into the children — the increment for them is deferred.
// increase by 1 all numbers in the segment [left; right]
void Update(int v, int lpos, int rpos, int left, int right)
{
if (left > right) return;
if ((lpos == left) && (rpos == right))
{
Shift(v, 1);
return;
}Otherwise, before descending into the children, push the deferred increment, recursively process both halves and recompute the values of vertex .
Push(v);
int mid = (lpos + rpos) / 2;
Update(2*v, lpos, mid, left, min(mid, right));
Update(2*v+1, mid+1, rpos, max(left, mid+1), right);
Pull(v);
}The function Query returns the number of elements in the segment [\mathit{left}; \mathit{right}] divisible by . If the query segment coincides with the segment of the vertex, the answer is stored in .
// number of elements divisible by 3 in the segment [left; right]
int Query(int v, int lpos, int rpos, int left, int right)
{
if (left > right) return 0;
if ((lpos == left) && (rpos == right))
return SegTree[v].cnt[0];Otherwise, push the deferred increment to the children and sum the answers for the left and right parts of the segment.
Push(v);
int mid = (lpos + rpos) / 2;
return Query(2*v, lpos, mid, left, min(mid, right)) +
Query(2*v+1, mid+1, rpos, max(left, mid+1), right);
}The main part of the program. Read and and build the segment tree. The numbers are indexed from zero, so the root corresponds to the segment [0; n - 1].
scanf("%d %d", &n, &q);
SegTree.resize(4 * n);
BuildTree(1, 0, n - 1);Process the operations one by one. For an operation of type , increase the numbers in the segment ; for an operation of type , print the number of elements in it divisible by .
for (i = 0; i < q; i++)
{
scanf("%d %d %d", &type, &a, &b);
if (type == 0)
Update(1, 0, n - 1, a, b);
else
printf("%d\n", Query(1, 0, n - 1, a, b));
}