{
    "slug": "segment_tree",
    "term": "Segment Trees",
    "category": "data_structures",
    "difficulty": "advanced",
    "short": "A tree data structure for O(log n) range queries and point updates — supporting sum, min, max over arbitrary array ranges.",
    "long": "Each node stores an aggregate (sum, min, max) over a range. Leaves represent individual elements. Build: O(n). Range query and point update: O(log n) each. Lazy propagation extends to range updates in O(log n). Compare to prefix sum: prefix sum allows O(1) range queries but O(n) updates; segment tree provides O(log n) for both — choose based on query/update ratio.",
    "aliases": [
        "segment tree",
        "range query",
        "lazy propagation"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "range-query"
    ],
    "misconception": "A prefix sum array is always equivalent to a segment tree for range queries — prefix sums allow O(1) range queries but O(n) update; segment tree provides O(log n) for both, making it essential for dynamic data.",
    "why_it_matters": "A prefix sum array answers range queries in O(1) but requires O(n) rebuild on updates — segment trees handle both in O(log n), enabling efficient analytics on frequently-changing data.",
    "common_mistakes": [
        "Not implementing lazy propagation for range updates — naive update is O(n)",
        "Off-by-one errors in range boundaries",
        "Using segment tree for static data — prefix sum is simpler",
        "Not handling out-of-range queries gracefully"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "b_tree_structure",
        "adjacency_matrix_list",
        "disjoint_set"
    ],
    "prerequisites": [
        "big_o_notation",
        "array_data_structure",
        "recursion_patterns"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Segment_tree"
    ],
    "bad_code": "// O(n) range sum — slow for large arrays:\nfunction rangeSum(array $arr, int $l, int $r): int {\n    return array_sum(array_slice($arr, $l, $r - $l + 1));\n}\n// 1000 queries on 1M element array: 1 billion operations",
    "good_code": "// Segment tree — O(log n) per query and update:\nclass SegmentTree {\n    private array $tree;\n    public function query(int $l, int $r): int { /* O(log n) */ }\n    public function update(int $pos, int $val): void { /* O(log n) */ }\n}",
    "quick_fix": "Use a segment tree when you need both range queries (sum, min, max over a range) and point updates efficiently — O(log n) for both vs O(n) for naive array approach",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/segment_tree",
        "html_url": "https://codeclaritylab.com/glossary/segment_tree",
        "json_url": "https://codeclaritylab.com/glossary/segment_tree.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Segment Trees](https://codeclaritylab.com/glossary/segment_tree) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/segment_tree"
            }
        }
    }
}