Introduction to Segment Trees
What is a Segment Tree?
A segment tree is a binary tree-based advanced data structure that supports flexible range operations. Unlike a Fenwick Tree (Binary Indexed Tree) which is limited to simple point update/range query use cases, segment trees can handle range updates with point queries, and even full range updates with range queries effici ...
Posted on Wed, 20 May 2026 07:33:16 +0000 by Stryks
Heavy-Light Decomposition Template for Tree Path Queries
The following C++ implementation demonstrates a complete Heavy-Light Decomposition (HLD) framework integrated with a lazy propagation segment tree to support efficient path updates and queries on trees. It passes the standard template problem on Luogu.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int MOD;
struct ...
Posted on Mon, 18 May 2026 00:02:42 +0000 by swizzer
Introduction to Segment Trees and Range Queries
Range Extremum Queries and Algorithmic ChoicesRange Maximum/Minimum Query (RMQ) problems involve processing an array of size n to handle multiple range queries and bulk modifications. Different data structures offer varying trade-offs:Brute Force: Simple implementation suitable for small datasets, but query performance is poor.Binary Indexed Tr ...
Posted on Wed, 13 May 2026 21:56:16 +0000 by Niruth
Segment Tree Historical Values and Advanced Tagging Techniques
Maintaining Range Minimum and Historical MaximumWhen a segment tree needs to support range addition, range minimum assignment, range sum, range maximum, and range historical maximum, a standard approach involves tracking the maximum value, strict second maximum value, and the count of maximum values within each node. Operations affecting the mi ...
Posted on Tue, 12 May 2026 19:45:03 +0000 by Pazuzu156
Finding the Leftmost Meeting Point in a Sequence of Buildings
This problem asks us to identify the earliest possible building index where two individuals, starting from distinct locations, can rendezvous. We are provided with an array representing building heights, let's call it buildingElevations, and a series of queries. Each query specifies two initial building indices, startA and startB.
The rule for ...
Posted on Mon, 11 May 2026 11:46:07 +0000 by Tryfan
Segment Tree Divide and Conquer with Rollback Data Structures
Introduction to Time-Based Divide and Conquer
Segment Tree Divide and Conquer is an advanced offline algorithmic technique typicalyl used to solve problems involving dynamic modifications that persist over specific time intervals. The core idea is to map the time dimension onto a segment tree, allowing us to decompose the lifespan of operations ...
Posted on Mon, 11 May 2026 09:35:33 +0000 by minc
CCPC Qinhuangdao Contest: Problem Solutions and Code
Problem A. Is Your School the Kingdom of Construction I
Approach
The official solution provides a clear construction method. We need to generate exactly k coordinate pairs (x, y) where both coordinates are between 1 and n.
First, we construct a base set of edges forming a connected structure. Then, if additional pairs are needed, we fill in the ...
Posted on Sun, 10 May 2026 20:13:05 +0000 by ragefu
Minimizing Interval Length Difference for Common Intersection Using Segment Trees
Given $n$ closed intervals on a number line, the objective is to select exactly $m$ intervals such that they share at least one common coordinate point. The cost of a selection is defined as the difference between the maximum length and the minimum length among the chosen intervals. The length of an interval $[l, r]$ is calculated as $r - l$. T ...
Posted on Fri, 08 May 2026 22:58:06 +0000 by Jak-S
Segment Tree Template for Competitive Programming: Point Updates and Range Queries
The following reusable segment tree implementation uses 0-based indexing with half-open intervals [l, r). It supports point assignment, point addition, range queries, and methods to locate the first or last endex satisfying a custom predicate.
#include <bits/stdc++.h>
template<typename Info>
struct SegmentTree {
int size = 0;
...
Posted on Thu, 07 May 2026 09:24:23 +0000 by ainoy31