UtilityToolsLab

© 2026 UtilityToolsLab. All rights reserved.

Privacy Policy·Terms of Service·Sitemap
HomeCompetitive ProgrammingSegment Tree

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsMEX CalcInterval MergerMatrix RotationPBDS GeneratorGraph VisualizerStress TesterModulo CalcBinary VisualizerConvex HullPath Finder

Segment Tree Blueprint

Input an array, choose sum/min/max/gcd. See the segment tree visualization and get a complete C++ class template to copy.

How to Use

  1. 1Open Segment Tree Blueprint above. No signup or installation needed.
  2. 2Type or paste your input. Everything runs locally in your browser — nothing leaves your device.
  3. 3See results instantly. Hit Copy to grab the output.
  4. 4Star the tool to save it to Favorites for quick access next time.

Frequently Asked Questions

Is Segment Tree free?

Yes, completely free. No sign-up, no credit card, no usage limits.

Do you store my data?

Never. Everything runs 100% in your browser. Nothing is uploaded to any server.

Does it work on mobile?

Yes. All tools are fully responsive and work on phones, tablets, and desktops.

Segment Tree (SUM) — 7 elements

[0,6]31
[0,3]16
[4,6]15
[0,1]4
[2,3]12
[4,5]11
[6,6]4
[0,0]1
[1,1]3
[2,2]5
[3,3]7
[4,4]9
[5,5]2

C++ Segment Tree Template

// Segment Tree (SUM) — generated by UtilityToolsLab
// Array: {1, 3, 5, 7, 9, 2, 4}  |  N = 7
#include <bits/stdc++.h>
using namespace std;

struct SegTree {
    int n;
    vector<long long> seg;

    SegTree(int n) : n(n), seg(4 * n, 0) {}

    void build(vector<int>& arr, int node, int l, int r) {
        if (l == r) { seg[node] = arr[l]; return; }
        int mid = (l + r) / 2;
        build(arr, 2*node,   l,   mid);
        build(arr, 2*node+1, mid+1, r);
        seg[node] = seg[2*node] + seg[2*node+1];
    }

    void update(int node, int l, int r, int idx, long long val) {
        if (l == r) { seg[node] = val; return; }
        int mid = (l + r) / 2;
        if (idx <= mid) update(2*node,   l,   mid, idx, val);
        else            update(2*node+1, mid+1, r, idx, val);
        seg[node] = seg[2*node] + seg[2*node+1];
    }

    long long query(int node, int l, int r, int ql, int qr) {
        if (qr < l || r < ql) return 0LL;
        if (ql <= l && r <= qr) return seg[node];
        int mid = (l + r) / 2;
        auto lv = query(2*node,   l,   mid, ql, qr);
        auto rv = query(2*node+1, mid+1, r, ql, qr);
        return lv + rv;
    }

    // Public wrappers
    void update(int idx, long long val) { update(1, 0, n-1, idx, val); }
    long long query(int l, int r) { return query(1, 0, n-1, l, r); }
};

int main() {
    vector<int> arr = {1, 3, 5, 7, 9, 2, 4};
    SegTree st(arr.size());
    st.build(arr, 1, 0, arr.size()-1);

    // Example: query range [0, 6]
    cout << st.query(0, 6) << endl;  // sum of entire array

    // Example: update index 0 to 100
    // st.update(0, 100);
}