Bibble and Captain Snappy#4486

Bibble and Captain Snappy (Editorial)

Let's construct a segment tree that supports a summation operation. In each node (representing a fundamental segment ), we'll additionally store a value . If left > 0, it means there is a pending operation in the segment : all nodes in its subtree should be assigned values left, left + 1, ..., left + b - a. Initially, left is set to zero in all nodes.

Now, let's consider the process of propagating values. Suppose a query q(x, y) partially overlaps both subtrees, meaning x \le m < y. In this case:

If a \ne x~(b \ne y), the value should be propagated further.

If x \ge m + 1 (meaning the query segment lies entirely in the right subtree), then the value for the left child remains .

Now, consider a query q(x, y) that is covered by a set of fundamental segments [a_1, b_1], [a_2, b_2], ..., [a_k, b_k]. In this case, assign the value for segment [a_i, b_i] as:

1~+~sum~of~lengths~of~all~previous~segments~[a_j,b_j],~for~which~j < i

For example, suppose the segment tree is built for the range , and the query q(2, 7) covers the following fundamental segments:

Example

Let's consider the following test:

5
5 3 2 4 5
2
2 2 5
1 1 3

Next to each node in the rectangle, the sum over the segment is written.

Algorithm implementation

Declare the array SegTree to store the segment tree.

struct node
{
  long long summa;
  int left;
};
vector<node> SegTree;

The function BuildTree constructs the segment tree.

void BuildTree(int v, int lpos, int rpos)
{
  if (lpos == rpos)
    SegTree[v].sum = a[lpos];
  else
  {
    int mid = (lpos + rpos) / 2;
    BuildTree(2 * v, lpos, mid);
    BuildTree(2 * v + 1, mid + 1, rpos);
    SegTree[v].sum = SegTree[2 * v].sum + SegTree[2 * v + 1].sum;
  }
}

The function Sum computes the sum of numbers from to .

long long Sum(int a, int b)
{
  return 1LL * (a + b) * (b - a + 1) / 2;
}

The function Push propagates the left value from node to its children. The sum in the child nodes is then recomputed.

void Push(int v, int lpos, int mid, int rpos)
{
  if (SegTree[v].left)
  {
     SegTree[2 * v].left = SegTree[v].left;
     SegTree[2 * v].sum = Sum(SegTree[2 * v].left, 
                              SegTree[2 * v].left + mid - lpos);

     SegTree[2 * v + 1].left = SegTree[v].left + mid - lpos + 1;
     SegTree[2 * v + 1].sum = Sum(SegTree[2 * v + 1].left, 
                        SegTree[2 * v + 1].left + rpos - mid - 1);
     SegTree[v].left = 0;
  }
}

The function Update fills the range [left; right] with the sequence of values from, from + 1, from + 2, ... .

void Update(int v, int lpos, int rpos, int left, int right, int from)
{
  if (left > right) return;
  if ((lpos == left) && (rpos == right))
  {
    SegTree[v].left = from;
    SegTree[v].sum = Sum(from, from + right - left);
    return;
  }

  int mid = (lpos + rpos) / 2;
  int midval = mid - left + 1;

If the query segment [left; right] lies entirely within the right subtree [mid + 1; rpos], pass the value to the right child without changes (while setting to zero).

  if (midval < 0) midval = 0;
  Push(v, lpos, mid, rpos);
  Update(2 * v, lpos, mid, left, min(mid, right), from);
  Update(2 * v + 1, mid + 1, rpos, max(left, mid + 1), 
                             right, from + midval);
  SegTree[v].sum = SegTree[2 * v].sum + SegTree[2 * v + 1].sum;
}

The function Summa returns the sum of numbers in the range [left; right].

long long Summa(int v, int lpos, int rpos, int Left, int Right)
{
  if (Left > Right) return 0;
  if ((lpos == Left) && (rpos == Right)) return SegTree[v].sum;
    
  int mid = (lpos + rpos) / 2;
  Push(v, lpos, mid, rpos);

  return Summa(2 * v, lpos, mid, Left, min(mid, Right)) +
         Summa(2 * v + 1, mid + 1, rpos, max(Left, mid + 1), Right);
}

The main part of the program. Read the input array of numbers.

scanf("%d", &n);
a.resize(n + 1);
for(i = 1; i <= n; ++i)
  scanf("%d", &a[i]);

Construct the segment tree.

SegTree.resize(4 * n);
BuildTree(1, 1, n);

Sequentially process queries.

scanf("%d", &q);
for(i = 0; i < q; ++i)
{
  scanf("%d %d %d", &type, &l, &r);
  if(type == 1)
    printf("%lld\n", Summa(1,1,n,l,r));
  else
    Update(1,1,n,l,r,1);
}