Point Update Range Sum
Authors: Benjamin Qi, Andrew Wang
Prerequisites
Introduces Segment Tree, Binary Indexed Tree, and Order Statistic Tree (C++ only).
Focus Problem – read through this problem before continuing!
Most gold range query problems require you to support following tasks in time each on an array of size :
- Update the element at a single position (point).
- Query the sum of some consecutive subarray.
Both segment trees and binary indexed trees can accomplish this.
Segment Tree
Focus Problem – read through this problem before continuing!
A segment tree allows you to do point update and range query in time each for any associative operation, not just summation.
Resources
Pro Tip
You can skip more advanced applications such as lazy propagation for now. They will be covered in platinum.
Resources | |||
---|---|---|---|
CSA | Interactive updates. | ||
CPH | Same implementation as AICash below. | ||
CPC | See slides after union-find. Also introduces sqrt bucketing. | ||
cp-algo | Advanced versions will be covered in Plat. |
Implementations
Resources | |||
---|---|---|---|
CF | simple implementation | ||
Benq | based off above |
C++
template<class T> struct Seg { // comb(ID,b) = bconst T ID = 0; T comb(T a, T b) { return a+b; }int n; vector<T> seg;void init(int _n) { n = _n; seg.assign(2*n,ID); }void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }void upd(int p, T val) { // set val at position pseg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }T query(int l, int r) { // sum on interval [l, r]T ra = ID, rb = ID;for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
Java
import java.util.*;import java.io.*;public class segtree {public static final int N = (int)1e5; // limit for array sizepublic static int n; // array sizepublic static long t[] = new long[2*N];public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));n = Integer.parseInt(br.readLine());
Binary Indexed Tree
Implementation is shorter than segment tree, but maybe more confusing at first glance.
Resources
Resources | |||
---|---|---|---|
CSA | interactive | ||
CPH | similar to above | ||
cp-algo | also similar to above | ||
TC |
Implementations
C++
Resources | |||
---|---|---|---|
CF | |||
Benq | based off above |
Java
public static long[] fwt;public static long query(int a, int b){return sum(b) - sum(a - 1);}public static long sum(int i){long sum = 0;while(i > 0){sum += fwt[i];
Finding -th Element
Suppose that we want a data structure that supports all the operations as a set
in C++ in addition to the following:
order_of_key(x)
: counts the number of elements in the set that are strictly less thanx
.find_by_order(k)
: similar tofind
, returns the iterator corresponding to thek
-th lowest element in the set (0-indexed).
Order Statistic Tree
Luckily, such a built-in data structure already exists in C++.
Resources | |||
---|---|---|---|
CF | |||
CPH | brief overview with find_by_order and order_of_key | ||
Benq | code |
To use indexed set locally, you need to install GCC as mentioned in Running Code.
With a BIT
Assumes all updates are in the range .
Resources | |||
---|---|---|---|
CF | log N |
With a Segment Tree
Covered in Platinum.
Example - Inversion Counting
Focus Problem – read through this problem before continuing!
Solution
Solution
Range Sum Problems
General
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
CSES | Easy | Show Sketch | |||
CSES | Easy | Show Sketch | |||
CSES | Easy | Show Sketch | |||
CSES | Easy | View Solution | |||
CSES | Easy | View Solution | |||
Kattis | Easy | Show Sketch | |||
HE | Easy | Show Sketch | |||
CSES | Normal | Show Sketch | |||
CSES | Hard | View Solution |
USACO
Haircut, Balanced Photo, and Circle Cross are just variations on inversion counting.
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
Gold | Easy | External Sol | |||
Gold | Easy | External Sol | |||
Gold | Easy | External Sol | |||
Gold | Easy | External Sol | |||
Plat | Easy | External Sol | |||
Silver | Normal | External Sol | |||
Gold | Hard | External Sol | |||
Old Gold | Hard | External Sol | |||
Plat | Hard | External Sol |
Segment Tree Problems
So far, no Gold problem has required a segment tree.
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
Gold | Hard | External Sol | |||
Plat | Very Hard | Show TagsPURQ | External Sol |