The segment tree is a very useful algorithm when doing many RMQ problems, we have the following problem: https://codeforces.net/contest/1199/problem/D how to solve it? Well, here is the segment tree to help us, but what is the segment tree? Well, it is a tree where each node is F(x, y) being x and y stored in its 2 children here is an example and the implementation: