【Topic】Line segment tree

First, a segment tree is a "tree", and a complete binary tree. At the same time, the word "line segment" reflects another feature of the line segment tree: each node represents a "line segment", or an interval. In fact, the root node of a segment tree represents the "whole" interval, and its left and right subtrees are also a segment tree, representing the left and right half of the interval, respectively.
Here we can give an example to illustrate the usual construction method of line segment tree. Take the RMQ problem as an example:
there are N numbers arranged in a row, and each time the minimum number in a certain segment is asked.
When constructing, let the root node represent the interval [0, N-1], that is, an interval composed of all N numbers, and then divide the interval into two halves, which are represented by the left and right subtrees respectively. It is not difficult to prove that the number of nodes in such a segment tree is only 2N-1, which is O(N) level.

Example:
Maximum value
Max.pas
[Title description]
Perform M(1<=M<=100000) operations on a sequence consisting of N (1<=N<=100000) numbers A1...An. There are two types of operations:
(1) 1 xy: Indicates that A[x] is modified to y;
(1) 2 xy: Inquires about the maximum value between x and y.
[Input]
Input N (1<=N<=100000) in the first line, indicating the length of the sequence, and input the original sequence in the next N line; input M (1<=M<=100000) in the next line to indicate the number of operations, Next M lines, each line is 1 xy or 2 xy
[Output]
For each operation (2), output the corresponding answer.
[Sample input]
5
1
2
3
4
5
3
2 1 4
1 3 5
2 2 4
[Sample output]
4
5

[Limitations]
Ensure that all numbers in the sequence are in the range of longint.
Routine :

uses math;
var
        n,i,j,kind,x,y,m:longint;
        a,f:array[0..300000]of longint;
procedure tree(v,l,r:longint);//建树
var
        mid:longint;
begin
        mid:=(l+r) div 2;
        if l<r then
        begin
                tree(v*2,l,mid);
                tree(v*2+1,mid+1,r);
        end
        else
        begin
                f[v]:=a[l];
                exit;
        end;
        f[v]:=max(f[v*2],f[v*2+1]);
end;
procedure change(v,l,r,x,y:longint);//转换
var
        mid:longint;
begin
        mid:=(l+r) div 2;
        if l=r then
        begin
                f[v]:=y;
                exit;
        end;
        if x<=mid then change(v*2,l,mid,x,y)
        else change(v*2+1,mid+1,r,x,y);
        f[v]:=max(f[v*2],f[v*2+1])
end;
function find(v,l,r,x,y:longint):longint;//找答案
var
        mid:longint;
begin
        mid:=(l+r) div 2;
        if (x=l)and(y=r) then exit(f[v]);
        if (x<=mid)and(y<=mid) then exit(find(v*2,l,mid,x,y));
        if (x<=mid)and(y>mid) then exit(max(find(v*2,l,mid,x,mid),find(v*2+1,mid+1,r,mid+1,y)));
        if (x>mid)and(y>mid) then exit(find(v*2+1,mid+1,r,x,y));
end;
begin
        readln(n);
        for i:=1 to n do
                readln(a[i]);
        tree(1,1,n);
        readln(m);
        for i:=1 to m do
        begin
                readln(kind,x,y);
                if kind=1 then
                        change(1,1,n,x,y)
                else writeln(find(1,1,n,x,y));
        end;
end.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326047899&siteId=291194637