#C5514. Segment Tree Queries
Segment Tree Queries
Segment Tree Queries
This problem involves processing a sequence of queries on an array using the segment tree data structure. You are given an array of N integers and a list of queries. Each query can be one of the following types:
- UPDATE i x: Set the element at index i to x.
- SUM l r: Compute the sum of elements in the subarray from index l to r.
- MIN l r: Find the minimum element in the subarray from index l to r.
You are required to perform the operations in the order they are given and output the result for each query of type SUM
or MIN
.
Segment Tree Overview:
A segment tree is a data structure that allows answering range queries efficiently. It can support various operations such as range sum or range minimum queries. Given the binary combination function \(f\) and a default value, the segment tree is built over an array such that each node represents a range and its value is computed using \(f\) over the range. When an element is updated, the change is propagated up the tree.
The tree is built in \(O(N)\) time and each query and update can be performed in \(O(\log N)\) time.
inputFormat
The input is given in the following format:
N A1 A2 ... AN M query1 query2 ... queryM
Where:
N
is the number of elements in the array.- The next line contains
N
space-separated integers. M
is the number of queries.- Each of the next
M
lines represents a query as described above.
outputFormat
For each query of type SUM
or MIN
, output the result on a new line in the order the queries are provided.
8
5 3 8 6 3 7 4 2
5
UPDATE 3 10
SUM 2 5
MIN 4 7
UPDATE 6 1
MIN 5 8
22
3
1
</p>