Skip to content
We areTOP 1%
DSA · Competitive Programmer · Pattern 4 of 9

Segment Tree & Fenwick Tree

13 problems.
Your progress0%
1st Visit
0/13
2nd Visit
0/13
Revise
0/13

About this pattern

Range-query structures answer questions about subarrays while the array keeps changing. A Fenwick tree, or binary indexed tree, supports point updates and prefix sums in O(log n) with very little code, and also counts inversions or smaller elements once values are compressed to ranks. A segment tree is more general: each node stores an aggregate of its range, such as a sum, a minimum or a small struct, and any associative merge works. Lazy propagation extends it to range updates. Signals: many range queries mixed with updates, n and q around 2 * 10^5, or counting pairs that satisfy an order condition. Every operation costs O(log n).

struct Fenwick {                              // 1-indexed point add, prefix sum
    int n; vector<long long> t;
    Fenwick(int n) : n(n), t(n + 1, 0) {}
    void add(int i, long long v) { for (; i <= n; i += i & -i) t[i] += v; }
    long long sum(int i) { long long s = 0; for (; i > 0; i -= i & -i) s += t[i]; return s; }
};
struct SegTree {                              // 0-indexed point set, minimum on [l, r)
    int n; vector<long long> t;
    SegTree(int n) : n(n), t(2 * n, LLONG_MAX) {}
    void set(int i, long long v) { for (t[i += n] = v; i > 1; i >>= 1) t[i >> 1] = min(t[i], t[i ^ 1]); }
    long long query(int l, int r, long long res = LLONG_MAX) {
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) res = min(res, t[l++]);
            if (r & 1) res = min(res, t[--r]);
        }
        return res;
    }
};

Problems

Segment Tree & Fenwick Tree questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Range Sum Query - MutableLeetCodeMedium Solve Range Sum Query - Mutable on LeetCode (opens in a new tab)
2Xenia and Bit OperationsCodeforcesMedium 1700Solve Xenia and Bit Operations on Codeforces (opens in a new tab)
3Count of Smaller Numbers After SelfLeetCodeHard Solve Count of Smaller Numbers After Self on LeetCode (opens in a new tab)
4Pashmak and Parmida's problemCodeforcesHard 1800Solve Pashmak and Parmida's problem on Codeforces (opens in a new tab)
5Count of Range SumLeetCodeHard Solve Count of Range Sum on LeetCode (opens in a new tab)
6Enemy is weakCodeforcesHard 1900Solve Enemy is weak on Codeforces (opens in a new tab)
7MultisetCodeforcesHard 1900Solve Multiset on Codeforces (opens in a new tab)
8Longest Increasing Subsequence IILeetCodeHard Solve Longest Increasing Subsequence II on LeetCode (opens in a new tab)
9Sereja and BracketsCodeforcesHard 2000Solve Sereja and Brackets on Codeforces (opens in a new tab)
10XOR on SegmentCodeforcesHard 2000Solve XOR on Segment on Codeforces (opens in a new tab)
11SUM and REPLACECodeforcesHard 2000Solve SUM and REPLACE on Codeforces (opens in a new tab)
12Ant colonyCodeforcesHard 2100Solve Ant colony on Codeforces (opens in a new tab)
13A Simple TaskCodeforcesHard 2300Solve A Simple Task on Codeforces (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account