結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | 👑 ygussany |
提出日時 | 2021-12-07 00:48:11 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 163 ms / 2,000 ms |
コード長 | 1,711 bytes |
コンパイル時間 | 158 ms |
コンパイル使用メモリ | 30,524 KB |
実行使用メモリ | 26,768 KB |
最終ジャッジ日時 | 2023-09-21 16:16:18 |
合計ジャッジ時間 | 5,258 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 69 ms
10,116 KB |
testcase_01 | AC | 69 ms
10,036 KB |
testcase_02 | AC | 67 ms
10,228 KB |
testcase_03 | AC | 57 ms
10,396 KB |
testcase_04 | AC | 72 ms
10,048 KB |
testcase_05 | AC | 61 ms
18,344 KB |
testcase_06 | AC | 37 ms
10,072 KB |
testcase_07 | AC | 4 ms
12,468 KB |
testcase_08 | AC | 123 ms
26,240 KB |
testcase_09 | AC | 15 ms
16,188 KB |
testcase_10 | AC | 54 ms
22,900 KB |
testcase_11 | AC | 46 ms
18,544 KB |
testcase_12 | AC | 66 ms
18,188 KB |
testcase_13 | AC | 117 ms
25,992 KB |
testcase_14 | AC | 47 ms
22,392 KB |
testcase_15 | AC | 2 ms
10,020 KB |
testcase_16 | AC | 123 ms
26,340 KB |
testcase_17 | AC | 123 ms
26,256 KB |
testcase_18 | AC | 122 ms
26,328 KB |
testcase_19 | AC | 122 ms
26,256 KB |
testcase_20 | AC | 124 ms
26,272 KB |
testcase_21 | AC | 2 ms
10,180 KB |
testcase_22 | AC | 2 ms
10,100 KB |
testcase_23 | AC | 59 ms
22,744 KB |
testcase_24 | AC | 61 ms
24,396 KB |
testcase_25 | AC | 90 ms
26,768 KB |
testcase_26 | AC | 163 ms
20,780 KB |
testcase_27 | AC | 72 ms
19,232 KB |
ソースコード
#include <stdio.h> typedef struct Edge { struct Edge *next; int v; } edge; int main() { int i, N, Q; char S[200001]; scanf("%d %d", &N, &Q); scanf("%s", S); int u, w, M = 0, stack[200001], head = 0, indeg[200001] = {}, mate[200001], flag[200001]; edge *adj[200001] = {}, e[200001], *p; for (i = 0; i < N; i++) { if (S[i] == '(') stack[head++] = i + 1; else { head--; mate[i+1] = stack[head]; mate[stack[head]] = i + 1; flag[i+1] = -1; flag[stack[head]] = 1; if (head == 0) continue; u = stack[head-1]; w = stack[head]; e[M].v = w; e[M].next = adj[u]; adj[u] = &(e[M++]); indeg[w]++; } } int j, x, depth[200001], par[200001][20], q[200001], tail; for (i = 1, depth[0] = -1; i <= N; i++) { if (indeg[i] != 0 || flag[i] < 0) continue; q[0] = i; depth[i] = 0; par[i][0] = 0; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; depth[w] = depth[u] + 1; par[w][0] = u; for (j = 1, x = u; x != 0; par[w][j] = par[x][j-1], x = par[x][j-1], j++); par[w][j] = 0; q[tail++] = w; } } } for (i = 1; i <= Q; i++) { scanf("%d %d", &u, &w); if (flag[u] < 0) u = mate[u]; if (flag[w] < 0) w = mate[w]; while (depth[u] > depth[w]) { for (j = 0; depth[par[u][j+1]] >= depth[w]; j++); u = par[u][j]; } while (depth[w] > depth[u]) { for (j = 0; depth[par[w][j+1]] >= depth[u]; j++); w = par[w][j]; } while (u != w && depth[u] != 0) { for (j = 0; par[u][j+1] != par[w][j+1]; j++); u = par[u][j]; w = par[w][j]; } if (u == w && u != 0) printf("%d %d\n", u, mate[u]); else printf("-1\n"); } fflush(stdout); return 0; }