結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー XDXD
提出日時 2021-12-11 15:52:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,211 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 167,736 KB
実行使用メモリ 34,132 KB
最終ジャッジ日時 2023-09-27 03:39:31
合計ジャッジ時間 8,464 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 7 ms
23,900 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 8 ms
23,872 KB
testcase_22 AC 7 ms
23,836 KB
testcase_23 AC 90 ms
25,768 KB
testcase_24 AC 97 ms
25,964 KB
testcase_25 AC 134 ms
26,032 KB
testcase_26 WA -
testcase_27 AC 86 ms
34,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s); i<=(t); ++i)
#define form(i,s,t) for(register int i=(s); i>=(t); --i)
#define rep(i,s,t) for(register int i=(s); i<(t); ++i)
using namespace std;
const int N = 2e5 + 4; 
vector<int> T[N]; int sl;
int n, q; char s[N]; int dep[N], stk[N], top, l[N], r[N], tai[N], up[20][N]; 
void dfs(int u) {
	forn (i, 1, 19) up[i][u] = up[i - 1][up[i - 1][u]];
	for (int v : T[u]) up[0][v] = u, dep[v] = dep[u] + 1, dfs(v);
}
inline int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	int lft = dep[u] - dep[v];
	for (int i = 0; lft; lft >>= 1)
		if (lft & 1) u = up[i][u];
	if (u == v) return u;
	form (i, 19, 0) if (up[i][u] != up[i][v])
		u = up[i][u], v = up[i][v];
	return up[0][u];
}
int main() {
	scanf("%d%d", &n, &q);
	scanf("%s", s + 1);
	forn (i, 1, n) {
		if (s[i] == '(') stk[++top] = i, tai[i] = ++sl;
		else {
			T[tai[stk[top - 1]]].push_back(tai[stk[top]]);
			tai[i] = tai[stk[top]];
			r[tai[i]] = i, l[tai[i]] = stk[top]; top -- ;
		}
	}
	dfs(0);
	while (q--) {
		int a, b;
		scanf("%d%d", &a, &b);
		int lc = lca(tai[a], tai[b]);
		if (lc == 0) {
			puts("-1"); continue ;
		} else printf("%d %d\n", l[lc], r[lc]);
	}
	return 0;
}
0