Skip to content
Back to libraryPart 4 · Advanced topics

4.2Fenwick and segment trees

Before you start

It helps to have read 2.1 Prefix sums and 4.1 Trees. Overall level: Advanced.

Practice: Fenwick and segment trees

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Live range totalsYou are given a list of n integers a1, a2, ..., an and q queries to process in order. Query 1 p x sets ap to x. Query 2 l r asks for the sum al + al+1 + ... + ar. Print the answer to every type 2 query.ApplyNot started
  2. 2Live range minimumYou are given a list of n integers a1, a2, ..., an and q queries to process in order. Query 1 p x sets ap to x. Query 2 l r asks for the minimum of al, al+1, ..., ar. Print the answer to every type 2 query.ConnectNot started
  3. 3Locate the kth tokenThere are n positions, numbered 1 to n, and position i holds ai tokens. Then q queries arrive in order. Query 1 p d adds d to ap; d may be negative, but the resulting count is guaranteed to be nonnegative. Query 2 k asks for the smallest position j such that a1 + a2 + ... + aj ≥ k; k is guaranteed to be between 1 and the current total number of tokens. Print the answer to every type 2 query.ReasonNot started
  4. 4Live subtree rewardsA tree has n vertices, numbered 1 to n, and is rooted at vertex 1. Vertex i starts with reward ai. Then q queries arrive in order. Query 1 v x sets the reward of vertex v to x. Query 2 v asks for the sum of rewards in v's subtree, including v. Print the answer to every type 2 query.StretchNot started

Read the lesson