Skip to content

Segment Tree in Segment Tree

Common Applications

In algorithm competitions, we sometimes need to maintain multi-dimensional information. In such cases, we often require nested data structures like segment trees within segment trees to record information.

Implementation Principle

Consider how to implement single-point modification and range query on a two-dimensional plane using a nested segment tree. We consider the outer segment tree, where the subtrees of the bottom-level nodes from \(1\) to \(n\) represent the segment trees for rows \(1\) to \(n\), respectively. The parent nodes corresponding to these bottom-level nodes then represent the region covered by the subtrees of their two child nodes.

Properties

Space Complexity

Typically, we cannot build a sub-segment tree for every node of the outer segment tree due to excessive space requirements. Nested segment trees generally adopt a dynamic node creation strategy. A single modification involves \(\log{n}\) nodes of the outer segment tree, and for each such node, it involves \(\log{n}\) nodes of its subtree. Thus, the maximum space required for a single modification is \(\log^2{n}\).

Time Complexity

For a query operation, we perform \(\log{n}\) operations on the outer segment tree, each of which involves \(\log{n}\) operations on an inner segment tree. Therefore, the time complexity is \(\log^2{n}\). Modification operations have the same complexity as query operations, also \(\log^2{n}\).

Classic Example

Flowers on Moshang sorts the first dimension and then uses a nested segment tree to maintain the second and third dimensions.

Sample Code

Second dimension query

1
2
3
4
5
6
7
8
int tree_query(int k, int l, int r, int x) {
  if (k == 0) return 0;
  if (1 <= l && r <= sec[x].y) return vec_query(ou_root[k], 1, p, 1, sec[x].z);
  int mid = l + r >> 1, res = 0;
  if (1 <= mid) res += tree_query(ou_ch[k][0], l, mid, x);
  if (sec[x].y > mid) res += tree_query(ou_ch[k][1], mid + 1, r, x);
  return res;
}

Second dimension modification

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void tree_insert(int &k, int l, int r, int x) {
  if (k == 0) k = ++ou_tot;
  vec_insert(ou_root[k], 1, p, sec[x].z);
  if (l == r) return;
  int mid = l + r >> 1;
  if (sec[x].y <= mid)
    tree_insert(ou_ch[k][0], l, mid, x);
  else
    tree_insert(ou_ch[k][1], mid + 1, r, x);
}

Third dimension query

1
2
3
4
5
6
7
8
int vec_query(int k, int l, int r, int x, int y) {
  if (k == 0) return 0;
  if (x <= l && r <= y) return data[k];
  int mid = l + r >> 1, res = 0;
  if (x <= mid) res += vec_query(ch[k][0], l, mid, x, y);
  if (y > mid) res += vec_query(ch[k][1], mid + 1, r, x, y);
  return res;
}

Third dimension modification

1
2
3
4
5
6
7
8
void vec_insert(int &k, int l, int r, int loc) {
  if (k == 0) k = ++tot;
  data[k]++;
  if (l == r) return;
  int mid = l + r >> 1;
  if (loc <= mid) vec_insert(ch[k][0], l, mid, loc);
  if (loc > mid) vec_insert(ch[k][1], mid + 1, r, loc);
}

When dealing with multi-dimensional information problems, if the problem does not require being strictly online, we can also consider divide-and-conquer algorithms such as CDQ Divide and Conquer or Parallel Binary Search to avoid using advanced data structures and reduce implementation difficulty.