Sereja and Brackets
HardRating 2000CodeforcesSegment Tree & Fenwick Tree
Approach hintTry it first, then revealHide
Each segment-tree node stores matched pairs, unmatched ( and unmatched ). Merging two children matches min(left open, right close) new pairs; the answer to a query is twice the matched count.