Problem form Atcoder (DP and segment tree)

i was able to solve this question in O(n^2) using DP but that will give TLE. How to use segment tree to reduce the complexity. Thanks for help

problem link- https://atcoder.jp/contests/abl/tasks/abl_d