More Applications of Segment Tree
Authors: Benjamin Qi, Andi Qu
Prerequisites
Walking on a Segment Tree, Non-Commutative Combiner Functions
Resources | |||
---|---|---|---|
cp-algo | First two applications and more. |
Walking on a Segment Tree
Focus Problem – read through this problem before continuing!
You want to support queries of the following form on an array (along with point updates).
Find the first such that .
Of course, you can do this in time with a max segment tree and binary searching on the first such that . But try to do this in time.
Solution - Hotel Queries
Instead of binary searching and querying the segment tree separately, let's do them together!
Assume that you know that the answer is in some range . Let .
If , then we know that the answer is in the range . Otherwise, the answer is in the range .
Imagine that the segment tree is a decision tree. We start at the root and move down. When we're at some node that contains and we know that the answer is in the range , we move to the left child if ; otherwise, we move to the right child.
This is convenient because is already stored in the left child, so we can find it in time.
#include <bits/stdc++.h>using namespace std;const int MAXN = 200001;int n;int segtree[4 * MAXN], a[MAXN];void build(int l = 1, int r = n, int node = 1) {if (l == r) segtree[node] = a[l];
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
Old Gold | Normal | External Sol | |||
Plat | Normal | External Sol |
Non-Commutative Combiner Functions
This section is not complete.
previously only considered commutative operations like sum, max
Focus Problem – read through this problem before continuing!
Focus Problem – read through this problem before continuing!
Solution - Subarray Sum Queries
Solution
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
Old Gold | Easy | External Sol | |||
Old Gold | Normal | External Sol | |||
POI | Normal | View Solution | |||
Plat | Hard | Show TagsPURQ, Greedy | External Sol | ||
Balkan OI | Hard |