PARENTHESES

시간 제한0.3초메모리 제한1024 MB

요약
여는 괄호와 닫는 괄호의 수가 같은 부분 문자열 Q개에 대해, 정규 괄호열로 만들기 위한 최소 교환 횟수를 구한다.
난이도

어려움10점 중 8점

유형
누적 합, 그리디, 수학, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

Who doesn't love parentheses?

Sashka stumbled upon a bracket sequence S=s_1s_2…s_2NS=s\_1 s\_2 \dots s\_{2N} of 2N2N parentheses, from which NN are opening parentheses and NN are closing parentheses. She defines the bracket sequence's clumsiness as the minimum number of required swaps of elements to turn it into a regular parentheses sequence. The regular parentheses sequences follow these rules:

  • The empty sequence is a regular parentheses sequence.
  • SS is a regular non-empty parentheses sequence iff two regular sequences AA and BB exist such that S=‘(‘+A+‘)‘+BS=`(`+A+`)`+B, where ++ denotes the operation concatenation of two sequences.

Sashka wants to know the clumsiness for QQ such sequences T_1,T_2,…,T_QT\_1, T\_2, \dots,T\_Q, where the ii-th of them T_i=s_L_is_L_i+1…s_R_iT\_i=s\_{L\_i} s\_{L\_i+1} \dots s\_{R\_i} consists of all the elements in SS from position L_iL\_i to R_iR\_i inclusive. It is guaranteed that every sequence T_iT\_i consists of equal number of opening and closing parentheses. Write a program parentheses, which answers the QQ questions.

입력

The first line of the standard input contains the three integers NN, QQ and GG, which describe the number of opening parentheses in the sequence, the number of questions and the number of the subtask that the test is from. The second line contains 2N2N parentheses, s_1s_2…s_2Ns\_1 s\_2 \dots s\_{2N} respectively. The last QQ lines of the standard input contain the integers L_iL\_i and R_iR\_i, which describe the positions for the ii-th question.

출력

On the standard output print one number on each line, the ii-th number being the minimum number of required swaps to turn T_iT\_i into a regular parentheses sequence.

제한

  • 1≤N,Q≤2×1051 \leq N, Q \leq 2 \times 10^5
  • 1≤L_i<R_i≤2×N1 \leq L\_i < R\_i \leq 2 \times N
  • All questions are different from one another.

예제2

  1. 예제 1

    입력
    3 1 1
    )())((
    1 6
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5 10 1
    ()))(()(()
    2 9
    6 7
    1 10
    3 8
    7 10
    3 10
    9 10
    4 7
    4 5
    3 6
    
    예상 출력
    2
    0
    1
    1
    1
    1
    0
    1
    1
    1