結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー SchneeSchnee
提出日時 2021-12-07 03:59:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,631 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,056 KB
実行使用メモリ 17,292 KB
最終ジャッジ日時 2023-09-21 16:33:53
合計ジャッジ時間 15,347 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 WA -
testcase_03 WA -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.readline

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

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

def find_pair(x):
    count_dis = 1
    if s[x - 1] == "(":
        for i in range(1, n):
            if s[x - 1 + i] == "(":
                count_dis += 1
            else:
                count_dis -= 1
                if count_dis == 0:
                    return x + i
    else:
        for i in range(1, n):
            if s[x - 1 - i] == ")":
                count_dis += 1
            else:
                count_dis -= 1
                if count_dis == 0:
                    return x - i

def find_set_b(x):
    count_dis = 0
    for i in range(1, x):
        if s[x-1-i]==")":
            count_dis -= 1
        else:
            count_dis += 1
            if count_dis == 1:
                return x - i

def find_set_t(x):
    count_dis = 0
    for i in range(1, n - x + 1):
        if s[x-1+i]=="(":
            count_dis -= 1
        else:
            count_dis += 1
            if count_dis == 1:
                return x + i

def compare_2set(x, y):
    if min(x) <= min(y) and max(x) >= max(y):
        return min(x), max(x)
    elif min(y) <= min(x) and max(y) >= max(x):
        return min(y), max(y)
    else:
        None

for _ in range(q):
    a, b = int_input()
    
    x, y = (a, find_pair(a)), (b, find_pair(b))
    if ret:= compare_2set(x, y):
        print(*ret)
    
    else:
        top, bottom = max(*x, *y), min(*x, *y)
        a_b = find_set_b(bottom)
        a_t = find_set_t(top)
        if a_b and a_t:
            print(a_b, a_t)
        else:
            print(-1)
0