結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | XD |
提出日時 | 2021-12-11 16:03:58 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,213 bytes |
コンパイル時間 | 1,531 ms |
コンパイル使用メモリ | 167,172 KB |
実行使用メモリ | 34,068 KB |
最終ジャッジ日時 | 2023-09-27 03:52:09 |
合計ジャッジ時間 | 5,867 ms |
ジャッジサーバーID (参考情報) |
judge14 / 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 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 7 ms
23,776 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | AC | 137 ms
26,072 KB |
testcase_26 | WA | - |
testcase_27 | AC | 87 ms
34,016 KB |
ソースコード
#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; }