{
  "id": "trees/segment-tree",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/trees/segment-tree",
  "topic": {
    "slug": "trees",
    "title": "Trees"
  },
  "title": "Segment Tree",
  "tagline": "Every node stores the answer for a whole range — so a range query stops early.",
  "vizKind": "tree",
  "complexity": {
    "time": {
      "best": "O(log n) query",
      "average": "O(log n) update",
      "worst": "O(n) to build"
    },
    "space": "O(n)",
    "growth": "log",
    "note": "Build once in O(n). Then every range query and every single-value update is O(log n). A prefix-sum array gives O(1) queries but O(n) updates; a plain array gives O(1) updates but O(n) queries. The segment tree is the one that's fast at both."
  },
  "code": {
    "javascript": "// Every node stores the SUM of a range of the array.\nfunction build(a, lo, hi) {\n  if (lo === hi) return new Node(a[lo], lo, hi);   // a leaf\n\n  const mid = (lo + hi) >> 1;\n  const node = new Node(0, lo, hi);\n  node.left  = build(a, lo, mid);\n  node.right = build(a, mid + 1, hi);\n  node.sum = node.left.sum + node.right.sum;\n  return node;\n}\n\n// Sum of a[l..r] — without touching most of the array.\nfunction query(node, l, r) {\n  if (r < node.lo || node.hi < l) return 0;        // no overlap\n  if (l <= node.lo && node.hi <= r) return node.sum; // fully inside\n\n  return query(node.left, l, r) + query(node.right, l, r);\n}",
    "python": "# Every node stores the SUM of a range of the array.\ndef build(a, lo, hi):\n    if lo == hi: return Node(a[lo], lo, hi)      # a leaf\n\n    mid = (lo + hi) // 2\n    node = Node(0, lo, hi)\n    node.left  = build(a, lo, mid)\n    node.right = build(a, mid + 1, hi)\n    node.sum = node.left.sum + node.right.sum\n    return node\n\n\n# Sum of a[l..r] — without touching most of the array.\ndef query(node, l, r):\n    if r < node.lo or node.hi < l: return 0          # no overlap\n    if l <= node.lo and node.hi <= r: return node.sum # fully inside\n\n    return query(node.left, l, r) + query(node.right, l, r)\n",
    "java": "// Every node stores the SUM of a range of the array.\nNode build(int[] a, int lo, int hi) {\n  if (lo == hi) return new Node(a[lo], lo, hi);   // a leaf\n\n  int mid = (lo + hi) / 2;\n  Node node = new Node(0, lo, hi);\n  node.left  = build(a, lo, mid);\n  node.right = build(a, mid + 1, hi);\n  node.sum = node.left.sum + node.right.sum;\n  return node;\n}\n\n// Sum of a[l..r] — without touching most of the array.\nint query(Node node, int l, int r) {\n  if (r < node.lo || node.hi < l) return 0;         // no overlap\n  if (l <= node.lo && node.hi <= r) return node.sum; // fully inside\n\n  return query(node.left, l, r) + query(node.right, l, r);\n}",
    "cpp": "// Every node stores the SUM of a range of the array.\nNode* build(vector<int>& a, int lo, int hi) {\n  if (lo == hi) return new Node(a[lo], lo, hi);   // a leaf\n\n  int mid = (lo + hi) / 2;\n  Node* node = new Node(0, lo, hi);\n  node->left  = build(a, lo, mid);\n  node->right = build(a, mid + 1, hi);\n  node->sum = node->left->sum + node->right->sum;\n  return node;\n}\n\n// Sum of a[l..r] — without touching most of the array.\nint query(Node* node, int l, int r) {\n  if (r < node->lo || node->hi < l) return 0;           // no overlap\n  if (l <= node->lo && node->hi <= r) return node->sum; // fully inside\n\n  return query(node->left, l, r) + query(node->right, l, r);\n}"
  },
  "defaultInput": [
    5,
    3,
    8,
    2,
    7,
    1,
    6,
    4
  ],
  "defaultTarget": null,
  "inputHint": "Built first, then queried for the sum of a[2..5]. Watch how few nodes it touches.",
  "pitfalls": [
    {
      "wrong": "Descending all the way to the leaves on every query.",
      "right": "Then it's just an O(n) scan with extra steps. The whole speed comes from *stopping* the moment a node's range is fully inside the query and taking its stored answer."
    },
    {
      "wrong": "Getting the three overlap cases wrong.",
      "right": "No overlap → return 0 and abandon the branch. Fully inside → return the stored sum, stop. Partial → split and ask both children. Muddle these and you'll double-count or miss ranges."
    },
    {
      "wrong": "Using a prefix-sum array when the data changes.",
      "right": "Prefix sums are perfect for a static array and terrible for a changing one — every update rebuilds it. If values change, that's exactly what a segment tree is for."
    }
  ],
  "quiz": [
    {
      "q": "What does each node of a segment tree store?",
      "options": [
        "A single value from the array",
        "The answer (e.g. the sum) for a whole range of the array",
        "A pointer to the array",
        "The array's length"
      ],
      "answer": 1,
      "why": "Leaves hold single values; every parent holds the combined answer for its children's ranges. That's why a query can grab an entire range's sum from one node."
    },
    {
      "q": "Why is a range query O(log n) rather than O(n)?",
      "options": [
        "Because the tree is sorted",
        "Because the query stops as soon as a node's range is fully inside the question, taking its pre-computed answer",
        "Because it uses a hash",
        "It isn't — it's O(n)"
      ],
      "answer": 1,
      "why": "It never descends into a node that's entirely contained in the query — it just takes the stored number. Any range decomposes into about log n such nodes."
    },
    {
      "q": "What can a segment tree do that a prefix-sum array can't?",
      "options": [
        "Answer range queries",
        "Handle updates efficiently — changing one value is O(log n), not O(n)",
        "Use less memory",
        "Work on unsorted data"
      ],
      "answer": 1,
      "why": "Prefix sums answer queries in O(1) but must be rebuilt entirely — O(n) — whenever a value changes. A segment tree updates only the log n nodes above that value."
    }
  ],
  "problems": [
    {
      "name": "Range Sum Query - Mutable",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/range-sum-query-mutable/"
    },
    {
      "name": "Range Sum Query 2D - Mutable",
      "difficulty": "Hard",
      "url": "https://leetcode.com/problems/range-sum-query-2d-mutable/"
    },
    {
      "name": "Count of Smaller Numbers After Self",
      "difficulty": "Hard",
      "url": "https://leetcode.com/problems/count-of-smaller-numbers-after-self/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 11,
    "frames": [
      {
        "data": {
          "nodes": [],
          "rootId": null
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 2,
        "variables": {
          "array": "5 3 8 2 7 1 6 4",
          "n": 8
        },
        "explanation": "Array: 5, 3, 8, 2, 7, 1, 6, 4. Question: what's the sum of a range, like a[2..5]? Adding them up each time costs O(n). A segment tree answers it in O(log n) — and unlike a prefix-sum array, it still works when values change.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "found"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "total": 36,
          "nodes": 15
        },
        "explanation": "Built. Every node holds the sum of a *range*: the leaves are single values, and each parent is the sum of its two children. The root, 36, is the sum of everything.",
        "stats": {
          "comparisons": 0,
          "swaps": 7
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 15,
        "variables": {
          "question": "sum of a[2..5]",
          "bruteForce": "4 additions"
        },
        "explanation": "Now ask for the sum of a[2..5]. Brute force would touch 4 values. Watch how few nodes we actually need.",
        "stats": {
          "comparisons": 0,
          "swaps": 7
        },
        "accent": "pivot"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "node": "[0..7]",
          "overlap": "partial"
        },
        "explanation": "[0..7] only partly overlaps [2..5], so its sum is no use on its own. Split, and ask both children.",
        "stats": {
          "comparisons": 1,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "node": "[0..3]",
          "overlap": "partial"
        },
        "explanation": "[0..3] only partly overlaps [2..5], so its sum is no use on its own. Split, and ask both children.",
        "stats": {
          "comparisons": 2,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 16,
        "variables": {
          "node": "[0..1]",
          "overlap": "none"
        },
        "explanation": "[0..1] is completely outside [2..5]. Ignore it — and everything below it. A whole branch, gone.",
        "stats": {
          "comparisons": 3,
          "swaps": 7
        },
        "accent": "discarded"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7",
              "role": "found"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 17,
        "variables": {
          "node": "[2..3]",
          "sum": 10
        },
        "explanation": "[2..3] sits entirely inside [2..5], so its sum — 10 — is already exactly what we need. Take it and stop. We never look at the leaves underneath. *This* is where the log n comes from.",
        "stats": {
          "comparisons": 4,
          "swaps": 7
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7",
              "role": "found"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12",
              "role": "compare"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "node": "[4..7]",
          "overlap": "partial"
        },
        "explanation": "[4..7] only partly overlaps [2..5], so its sum is no use on its own. Split, and ask both children.",
        "stats": {
          "comparisons": 5,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7",
              "role": "found"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12",
              "role": "compare"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11",
              "role": "found"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 17,
        "variables": {
          "node": "[4..5]",
          "sum": 8
        },
        "explanation": "[4..5] sits entirely inside [2..5], so its sum — 8 — is already exactly what we need. Take it and stop. We never look at the leaves underneath. *This* is where the log n comes from.",
        "stats": {
          "comparisons": 6,
          "swaps": 7
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7",
              "role": "found"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12",
              "role": "compare"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11",
              "role": "found"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14",
              "role": "discarded"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 16,
        "variables": {
          "node": "[6..7]",
          "overlap": "none"
        },
        "explanation": "[6..7] is completely outside [2..5]. Ignore it — and everything below it. A whole branch, gone.",
        "stats": {
          "comparisons": 7,
          "swaps": 7
        },
        "accent": "discarded"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "sg0",
              "value": 36,
              "left": "sg1",
              "right": "sg8",
              "role": "compare"
            },
            {
              "id": "sg1",
              "value": 18,
              "left": "sg2",
              "right": "sg5",
              "role": "compare"
            },
            {
              "id": "sg2",
              "value": 8,
              "left": "sg3",
              "right": "sg4",
              "role": "discarded"
            },
            {
              "id": "sg3",
              "value": 5,
              "left": null,
              "right": null
            },
            {
              "id": "sg4",
              "value": 3,
              "left": null,
              "right": null
            },
            {
              "id": "sg5",
              "value": 10,
              "left": "sg6",
              "right": "sg7",
              "role": "found"
            },
            {
              "id": "sg6",
              "value": 8,
              "left": null,
              "right": null
            },
            {
              "id": "sg7",
              "value": 2,
              "left": null,
              "right": null
            },
            {
              "id": "sg8",
              "value": 18,
              "left": "sg9",
              "right": "sg12",
              "role": "compare"
            },
            {
              "id": "sg9",
              "value": 8,
              "left": "sg10",
              "right": "sg11",
              "role": "found"
            },
            {
              "id": "sg10",
              "value": 7,
              "left": null,
              "right": null
            },
            {
              "id": "sg11",
              "value": 1,
              "left": null,
              "right": null
            },
            {
              "id": "sg12",
              "value": 10,
              "left": "sg13",
              "right": "sg14",
              "role": "discarded"
            },
            {
              "id": "sg13",
              "value": 6,
              "left": null,
              "right": null
            },
            {
              "id": "sg14",
              "value": 4,
              "left": null,
              "right": null
            }
          ],
          "rootId": "sg0"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "answer": 18,
          "nodesVisited": 7,
          "bruteForce": 4
        },
        "explanation": "Sum of a[2..5] = 18. We touched 7 nodes, and the green ones did all the work — each handed us a whole range at once. Grow the array to a million and this stays about 20 nodes. And because sums live in the tree, changing one value only updates the log n nodes above it.",
        "stats": {
          "comparisons": 7,
          "swaps": 7
        },
        "accent": "found"
      }
    ]
  }
}