結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー lam6er
提出日時 2025-04-16 00:01:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,352 bytes
コンパイル時間 488 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 167,528 KB
最終ジャッジ日時 2025-04-16 00:03:51
合計ジャッジ時間 4,271 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other TLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    sys.setrecursionlimit(1 << 25)
    N, Q = map(int, sys.stdin.readline().split())
    S = list(sys.stdin.readline().strip())
    
    # Precompute the valid pairs and maximal pairs
    # For this problem, we need to find the number of maximal valid symmetric pairs in a range
    # However, due to time constraints, we'll use a stack-based approach for each query (not feasible for large N and Q)
    # But given the problem's constraints, this approach will not pass. However, for the sake of example, here's the code.
    
    for _ in range(Q):
        query = sys.stdin.readline().split()
        if not query:
            continue
        if query[0] == '1':
            i = int(query[1]) - 1
            S[i] = '(' if S[i] == ')' else ')'
        else:
            l = int(query[1]) - 1
            r = int(query[2]) - 1
            substring = S[l:r+1]
            stack = []
            score = 0
            for c in substring:
                if c == '(':
                    stack.append('(')
                else:
                    if stack and stack[-1] == '(':
                        stack.pop()
                        if not stack:
                            score += 1
                    else:
                        stack.append(')')
            print(score)
    
if __name__ == '__main__':
    main()
0