Editing Segment tree
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 80: | Line 80: | ||
update_rec(2*node+1,mid+1,end,pos,val) | update_rec(2*node+1,mid+1,end,pos,val) | ||
A[node] = min(A[2*node],A[2*node+1]) | A[node] = min(A[2*node],A[2*node+1]) | ||
− | private function query_rec(node | + | private function query_rec(node,a_begin,a_end,s_begin,s_end) |
− | if | + | if a_begin>=s_begin AND a_end<=s_end |
return A[node] | return A[node] | ||
else | else | ||
− | let mid = floor(( | + | let mid = floor((a_begin+a_end)/2) |
let res = ∞ | let res = ∞ | ||
− | if mid>=a_begin | + | if mid>=s_begin AND a_begin<=s_end |
− | res = min(res,query_rec(2*node, | + | res = min(res,query_rec(2*node,a_begin,mid,s_begin,s_end)) |
− | if | + | if a_end>=s_begin AND mid+1<=s_end |
− | res = min(res,query_rec(2*node+1,mid+1, | + | res = min(res,query_rec(2*node+1,mid+1,a_end,s_begin,s_end)) |
return res | return res | ||
function construct(size,a[1..size]) | function construct(size,a[1..size]) | ||
Line 100: | Line 100: | ||
return query_rec(1,1,N,begin,end) | return query_rec(1,1,N,begin,end) | ||
</pre> | </pre> | ||
+ | |||
==Variations== | ==Variations== | ||
The segment tree can be adapted to retrieve not only the minimum element in a range but also various other functions. Here are some examples taken from otherwise difficult contest problems: | The segment tree can be adapted to retrieve not only the minimum element in a range but also various other functions. Here are some examples taken from otherwise difficult contest problems: |