This two weeks is a data structure topic of study, was the topic of the topic of abuse to die = =
Segment tree: Simply say "1,n" the interval Two, "1, (1+n)/2" left subtree, "(1+n)/2+1,n" right sub-tree
This is always divided, until all are "x,x" such an interval. This makes up a tree.
There is such a tree, we can store the interval in the node and Ah, the maximum value of the interval, ah, the minimum, and so on. This is the additional information of the line tree, but also the focus of the topic.
We can use an array (length K) to store the initial value of the original interval, and then according to this contribution, so the number of nodes in this tree is 4*k;
For each node I, the left subtree is i*2, the right subtree is i*2+1, and the parent node is I/2. The sum of the interval is the same as that of the left and right sub-tree interval and the maximum minimum value.
The creation, modification, and query of the segment tree are recursively written.
So when it comes to a single value, it must affect its ancestor nodes, so write it down from the top. Modify the node information after recursion.
The same is true for query line tree queries from top to bottom.
As for the interval modification, I'm still looking at ...
Yes, the complexity of the changes and queries is LGN
Pre-paste Template
1 structnode2 {3 intmaxt,sum;4 intLeft,right;5 };6 structNode tree[4*K];7 intA[k];8 voidBuildintIdintLintR)9 {Tentree[id].left=l;tree[id].right=R; One if(l==R) A { -tree[id].maxt=tree[id].sum=A[l]; - } the Else - { -Build2*id,l, (L+R)/2); -Build2*id+1, (L+R)/2+1, R); +Tree[id].maxt=max (tree[2*id].maxt,tree[2*id+1].maxt); -tree[id].sum=tree[2*id].sum+tree[2*id+1].sum; + } A } at intQuerymax (intIdintLintR) - { - if(L==tree[id].left && r==tree[id].right) - returnTree[id].maxt; - intMid= (tree[id].left+tree[id].right) >>1; - intret=0; in if(r<=mid) -Ret=max (Ret,querymax (id<<1, L,r)); to Else if(l>=mid+1) +Ret=max (Ret,querymax (id<<1)+1, L,r)); - Else the { * intb; $A=querymax (id<<1, l,mid);Panax NotoginsengB=querymax ((id<<1)+1, mid+1, R); - returnMax (A, b); the } + returnret; A } the intUpdateintIdintPosintv) + { - if(Tree[id].left = =tree[id].right) $ { $tree[id].sum=tree[id].maxt=Val; - } - Else the { - intMid= (tree[id].left+tree[id].right) >>1;Wuyi if(pos<=mid) Update (id*2, pos,v); the ElseUpdate (id*2+1, pos,v); -tree[id].sum=tree[id*2].sum+tree[id*2+1].sum; WuTree[id].maxt=max (tree[id*2].maxt,tree[id*2+1].maxt); - } About return 0; $ } - - intQueryintIdintLintR) - { A if(tree[id].left==l&&tree[id].right==R) + returntree[id].sum; the Else - { $ intMid= (tree[id].left+tree[id].right) >>1; the if(R<=mid)returnQuery (id*2, l,r); the Else if(L>mid)returnQuery (id*2+1, L,r) the Else returnQuery (id*2, L,mid) +query (id*2+1, mid+1, R); the } -}
View Code
Segment tree--Data Structure topic learning