Codeforces 380C Sereja and Brackets - Problem Statement

CF 380C - bracket subsequence queries

You have a bracket sequence of length nn. Answer mm queries: for substring [l,r][l, r], find the length of the longest valid bracket subsequence.

A valid bracket sequence has matching pairs. For example, in "(())()", the whole string is valid with length 66.

Constraints: n,m106n, m \le 10^6. You need O(logn)O(\log n) per query, which means segment tree.

Checking each query by scanning takes O(n)O(n) per query. With mm queries, this is too slow. Use a segment tree with custom merge. Uses O(n)O(n) space for the tree.