結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー Schnee
提出日時 2021-12-07 02:53:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 953 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 18,204 KB
最終ジャッジ日時 2024-07-07 10:14:04
合計ジャッジ時間 8,796 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other AC * 5 TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.readline

def int_input():
    return map(int,input().split())

n, q = int_input()
s = input()

pairs = {}
singles = []

for i in range(n):
    if s[i] == "(":
        singles.append(i)
    else:
        a = singles.pop(-1) + 1
        pairs[a] = (a, i + 1)
        pairs[i + 1] = (a, i + 1)

def find_min_including_set(qs):
    x = pairs[qs[0]]
    y = pairs[qs[1]]
    bottom = min(x[0], y[0])
    top = max(x[1], y[1])
    if bottom < n + 1 - top:
        for i in range(bottom):
            j = bottom - i
            candidate = pairs[j]
            if candidate[1] >= top:
                return candidate
    else:
        for i in range(n + 1 - top):
            k = top + i
            candidate = pairs[k]
            if candidate[0] <= bottom:
                return candidate

for _ in range(q):
    i = tuple(int_input())
    if ans := find_min_including_set(i):
        print(*ans)
    else:
        print(-1)
0