Can you answer these queries - 1#2906

Can you answer these queries - 1 (Editorial)

To answer the queries, we use a segment tree. However, storing only the maximum subsegment sum in a vertex is not enough: knowing for two adjacent segments, we cannot find for their union, since the best subsegment may cross the boundary between them.

Therefore, each vertex stores four values for its segment:

Suppose these values are known for adjacent segments and . Then for their union:

\mathit{sum} = L.\mathit{sum} + R.\mathit{sum}, \\ \mathit{pref} = \max(L.\mathit{pref}, L.\mathit{sum} + R.\mathit{pref}), \\ \mathit{suf} = \max(R.\mathit{suf}, R.\mathit{sum} + L.\mathit{suf}), \\ \mathit{best} = \max(L.\mathit{best}, R.\mathit{best}, L.\mathit{suf} + R.\mathit{pref})

Indeed, the best subsegment of the union either lies entirely in , or lies entirely in , or crosses the boundary. In the last case it consists of some suffix of and some prefix of , and the maximum such sum equals L.\mathit{suf} + R.\mathit{pref}. The reasoning for the prefix and the suffix is similar.

To answer a query Q(x, y), we split the segment into fundamental segments of the tree and merge their information from left to right using the same function. The answer is the value of the resulting union. Building the tree takes , and each query is processed in O(\log n).

Example

Let us build the segment tree for the sequence 3, -4, 5, -2, 6, -7, -1, 2. Each vertex shows the values (\mathit{sum}, \mathit{pref}, \mathit{suf}, \mathit{best}) of its segment.

Let us consider the queries.

: the segment coincides with the root, the answer is \mathit{best} = 9. This value was obtained when merging the segments and as L.\mathit{suf} + R.\mathit{pref} = 3 + 6 = 9 and corresponds to the subsegment 5, -2, 6.

: the segment is split into and . Merging (-4, -4, -4, -4) and (3, 5, 3, 5), we get \mathit{best} = \max(-4, 5, -4 + 5) = 5.

: the segment is split into and . Merging (-2, -2, -2, -2) and (0, 6, 2, 6), we get \mathit{best} = \max(-2, 6, -2 + 6) = 6.

: the segment is split into and . We get \mathit{best} = \max(-7, -1, -7 + (-1)) = -1. All numbers are negative, so the best subsegment consists of a single largest number.

Algorithm Implementation

The input sequence is stored in the array .

vector<int> a;

A vertex of the segment tree is stored in the structure node, which contains four values for the corresponding segment: the sum , the maximum prefix sum , the maximum suffix sum and the maximum subsegment sum .

struct node
{
  int sum;  // sum of all numbers in the segment
  int pref; // maximum prefix sum
  int suf;  // maximum suffix sum
  int best; // maximum subsegment sum
};

The segment tree is stored in the vector .

vector<node> SegTree;

The function Combine computes the information about the union of two adjacent segments and from the information about each of them. The best prefix of the union either lies entirely in , or contains the whole and a prefix of . Similarly, the best suffix either lies in , or contains the whole and a suffix of . The best subsegment lies in , lies in or crosses the boundary — then it consists of a suffix of and a prefix of .

node Combine(node L, node R)
{
  node res;
  res.sum = L.sum + R.sum;
  res.pref = max(L.pref, L.sum + R.pref);
  res.suf = max(R.suf, R.sum + L.suf);
  res.best = max(max(L.best, R.best), L.suf + R.pref);
  return res;
}

The function BuildTree builds the segment tree. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}].

void BuildTree(int v, int lpos, int rpos)
{
  if (lpos == rpos)

For a leaf corresponding to a single number , all four values equal this number: the subsegment, prefix and suffix must be non-empty.

    SegTree[v].sum = SegTree[v].pref = SegTree[v].suf = SegTree[v].best = a[lpos];

For an internal vertex, we build both children and merge their information with the function Combine.

  else
  {
    int mid = (lpos + rpos) / 2;
    BuildTree(2*v, lpos, mid);
    BuildTree(2*v+1, mid+1, rpos);
    SegTree[v] = Combine(SegTree[2*v], SegTree[2*v+1]);
  }
}

The function Query returns the information (a node structure) about the segment [\mathit{left}; \mathit{right}]. Vertex corresponds to the segment [\mathit{lpos}; \mathit{rpos}]. If the query segment coincides with the segment of the vertex, return the information stored in it.

node Query(int v, int lpos, int rpos, int left, int right)
{
  if ((lpos == left) && (rpos == right))
    return SegTree[v];

If the query segment lies entirely in the left half or entirely in the right half, descend only into the corresponding child.

  int mid = (lpos + rpos) / 2;
  if (right <= mid)
    return Query(2*v, lpos, mid, left, right);
  if (left > mid)
    return Query(2*v+1, mid+1, rpos, left, right);

Otherwise, the query segment crosses the middle. Split it into the parts [\mathit{left}; \mathit{mid}] and [\mathit{mid} + 1; \mathit{right}], find the information about each part and merge it with the function Combine. Note that, unlike an ordinary sum segment tree, the answers of the parts cannot simply be added — they must be merged.

  return Combine(Query(2*v, lpos, mid, left, mid),
                 Query(2*v+1, mid+1, rpos, mid+1, right));
}

The main part of the program. Read the input sequence.

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

Build the segment tree.

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

Process the queries one by one. For each query , print the field of the information about the segment .

scanf("%d", &m);
for (i = 0; i < m; i++)
{
  scanf("%d %d", &x, &y);
  printf("%d\n", Query(1, 1, n, x, y).best);
}