I know how to solve this problem with Cartesian tree. It allows you to reverse any segment in O(log2n). Idea is like in RMQ with group operations. To do this, you need to store in each vertex additional boolrev - need we to reverse this subtree, or not. When we try to modify or query some vertex, check this value and if it's true, swap two childs, and <look comment at bottom>
Idea is like in RMQ with group operations.
To do this, you need to store in each vertex additional bool rev - need we to reverse this subtree, or not.
When we try to modify or query some vertex, check this value and if it's true, swap two childs, and <look comment at bottom>