結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー 👑 tamatotamato
提出日時 2021-12-07 14:28:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 901 ms / 2,000 ms
コード長 2,904 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 87,536 KB
実行使用メモリ 199,048 KB
最終ジャッジ日時 2023-09-21 20:40:11
合計ジャッジ時間 23,347 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 453 ms
86,568 KB
testcase_01 AC 420 ms
84,004 KB
testcase_02 AC 440 ms
85,776 KB
testcase_03 AC 399 ms
84,028 KB
testcase_04 AC 424 ms
85,136 KB
testcase_05 AC 493 ms
121,976 KB
testcase_06 AC 372 ms
88,216 KB
testcase_07 AC 192 ms
97,176 KB
testcase_08 AC 826 ms
197,548 KB
testcase_09 AC 397 ms
114,464 KB
testcase_10 AC 609 ms
165,944 KB
testcase_11 AC 493 ms
136,748 KB
testcase_12 AC 523 ms
121,344 KB
testcase_13 AC 838 ms
194,972 KB
testcase_14 AC 575 ms
155,036 KB
testcase_15 AC 70 ms
71,852 KB
testcase_16 AC 824 ms
197,752 KB
testcase_17 AC 876 ms
199,048 KB
testcase_18 AC 876 ms
196,560 KB
testcase_19 AC 901 ms
198,140 KB
testcase_20 AC 855 ms
197,952 KB
testcase_21 AC 69 ms
71,848 KB
testcase_22 AC 69 ms
71,316 KB
testcase_23 AC 627 ms
169,412 KB
testcase_24 AC 725 ms
192,264 KB
testcase_25 AC 800 ms
194,336 KB
testcase_26 AC 816 ms
194,468 KB
testcase_27 AC 537 ms
191,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    def STfunc(a, b):
        if a[0] < b[0]:
            return a
        else:
            return b

    # クエリは0-indexedで[l, r)
    class SparseTable():
        def __init__(self, A):
            # A: 処理したい数列
            self.N = len(A)
            self.K = self.N.bit_length() - 1
            self.table = [[0] * (self.K + 1) for _ in range(self.N)]
            for i, a in enumerate(A):
                self.table[i][0] = a
            for k in range(1, self.K + 1):
                for i in range(self.N):
                    j = i + (1 << (k - 1))
                    if j <= self.N - 1:
                        self.table[i][k] = STfunc(self.table[i][k - 1], self.table[j][k - 1])
                    else:
                        self.table[i][k] = self.table[i][k - 1]

        def query(self, l, r):
            # [l, r)の最小値を求める
            k = (r - l).bit_length() - 1
            return STfunc(self.table[l][k], self.table[r - (1 << k)][k])

    # adj[0] must be empty list
    def EulerTour(adj, root):
        st = [root]
        ret = []
        seen = [0] * len(adj)
        par = [0] * len(adj)
        depth = [0] * len(adj)
        while st:
            v = st.pop()
            if seen[v]:
                ret.append(v)
                continue
            ret.append(v)
            seen[v] = 1
            if par[v] != 0:
                st.append(par[v])
            for u in adj[v]:
                if seen[u] == 0:
                    st.append(u)
                    par[u] = v
                    depth[u] = depth[v] + 1

        return ret, depth

    N, Q = map(int, input().split())
    S = input().rstrip('\n')

    P = [0] * (N+1)
    adj = [[] for _ in range(N+2)]
    st = []
    for i, s in enumerate(S):
        if s == "(":
            st.append(i+1)
        else:
            j = st.pop()
            P[i+1] = j
            P[j] = i+1
            if st:
                adj[j+1].append(st[-1] + 1)
                adj[st[-1] + 1].append(j+1)
            else:
                adj[j+1].append(1)
                adj[1].append(j+1)

    et, depth = EulerTour(adj, 1)
    depth_list = [None] * len(et)
    left = [-1] * (N + 2)
    right = [-1] * (N + 2)
    for i, v in enumerate(et):
        depth_list[i] = (depth[v], v)
        if left[v] < 0:
            left[v] = i
        right[v] = i
    ST = SparseTable(depth_list)

    for _ in range(Q):
        x, y = map(int, input().split())
        xx, yy = P[x], P[y]
        if x > xx:
            x, xx = xx, x
        if y > yy:
            y, yy = yy, y
        lca_depth, lca = ST.query(min(left[x+1], left[y+1]), max(right[x+1], right[y+1]) + 1)
        if lca != 1:
            print(lca - 1, P[lca - 1])
        else:
            print(-1)


if __name__ == '__main__':
    main()
0