Editing Segment tree

Jump to: navigation, search

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,t_begin,t_end,a_begin,a_end)
+
     private function query_rec(node,a_begin,a_end,s_begin,s_end)
           if t_begin>=a_begin AND t_end<=a_end
+
           if a_begin>=s_begin AND a_end<=s_end
 
               return A[node]
 
               return A[node]
 
           else
 
           else
               let mid = floor((t_begin+t_end)/2)
+
               let mid = floor((a_begin+a_end)/2)
 
               let res = ∞
 
               let res = ∞
               if mid>=a_begin AND t_begin<=a_end
+
               if mid>=s_begin AND a_begin<=s_end
                     res = min(res,query_rec(2*node,t_begin,mid,a_begin,a_end))
+
                     res = min(res,query_rec(2*node,a_begin,mid,s_begin,s_end))
               if t_end>=a_begin AND mid+1<=a_end
+
               if a_end>=s_begin AND mid+1<=s_end
                     res = min(res,query_rec(2*node+1,mid+1,t_end,a_begin,a_end))
+
                     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:

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)