Editorial

There are many solutions to this problem. One of them is using segment tree.

Author's Solution

GNU C++17