結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー mkawa2mkawa2
提出日時 2021-12-09 00:49:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 2,696 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,940 KB
最終ジャッジ日時 2024-07-16 11:45:23
合計ジャッジ時間 13,139 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 279 ms
83,024 KB
testcase_01 AC 282 ms
81,936 KB
testcase_02 AC 293 ms
82,756 KB
testcase_03 AC 273 ms
81,664 KB
testcase_04 AC 295 ms
82,940 KB
testcase_05 AC 313 ms
86,832 KB
testcase_06 AC 226 ms
80,312 KB
testcase_07 AC 116 ms
82,384 KB
testcase_08 AC 487 ms
103,312 KB
testcase_09 AC 171 ms
85,632 KB
testcase_10 AC 290 ms
95,616 KB
testcase_11 AC 271 ms
89,216 KB
testcase_12 AC 304 ms
87,168 KB
testcase_13 AC 419 ms
102,552 KB
testcase_14 AC 265 ms
92,928 KB
testcase_15 AC 35 ms
52,876 KB
testcase_16 AC 433 ms
102,768 KB
testcase_17 AC 452 ms
103,424 KB
testcase_18 AC 450 ms
103,552 KB
testcase_19 AC 450 ms
102,952 KB
testcase_20 AC 437 ms
103,040 KB
testcase_21 AC 37 ms
53,120 KB
testcase_22 AC 36 ms
52,608 KB
testcase_23 AC 240 ms
94,420 KB
testcase_24 AC 254 ms
100,224 KB
testcase_25 AC 345 ms
101,184 KB
testcase_26 AC 537 ms
113,736 KB
testcase_27 AC 251 ms
113,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = 18446744073709551615
inf = 4294967295
# md = 10**9+7
# md = 998244353
md = 10**8+9

class LCA:
    # 頂点は0~n-1
    def __init__(self, to, root=0):
        self.to = to
        self.n = len(to)
        self.parents = [-1]*(self.n+1)
        self.depth = [0]*self.n
        self.__dfs(root)
        self.max_level = max(self.depth).bit_length()
        self.ancestor = [self.parents]+[[-1]*(self.n+1) for _ in range(self.max_level)]
        row0 = self.ancestor[0]
        for lv in range(self.max_level):
            row1 = self.ancestor[lv+1]
            for u in range(self.n):
                row1[u] = row0[row0[u]]
            row0 = row1

    def __dfs(self, root):
        stack = [root]
        while stack:
            u = stack.pop()
            pu = self.parents[u]
            du = self.depth[u]
            for v in self.to[u]:
                if v == pu: continue
                self.parents[v] = u
                self.depth[v] = du+1
                stack.append(v)

    # 最小共通祖先
    def anc(self, u, v):
        diff = self.depth[u]-self.depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        lv = 0
        while diff:
            if diff & 1: u = self.ancestor[lv][u]
            lv, diff = lv+1, diff >> 1
        if u == v: return u
        for lv in range(self.depth[u].bit_length()-1, -1, -1):
            anclv = self.ancestor[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self.parents[u]

n, q = LI()
n2 = n//2
s = SI()

par = [0]
uu = []
ll = [-1]*(n2+1)
rr = [-1]*(n2+1)
to = [[] for _ in range(n2+1)]
u = 1
for i, c in enumerate(s):
    if c == "(":
        to[par[-1]].append(u)
        ll[u] = i
        par.append(u)
        uu.append(u)
        u += 1
    else:
        v = par.pop()
        rr[v] = i
        uu.append(v)
# pDB(uu)
# pDB(ll)
# pDB(rr)

lca = LCA(to)

for _ in range(q):
    x, y = LI()
    u = uu[x-1]
    v = uu[y-1]
    a = lca.anc(u, v)
    if a:
        print(ll[a]+1, rr[a]+1)
    else:
        print(-1)
0