結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | 👑 ygussany |
提出日時 | 2021-12-07 00:53:39 |
言語 | C (gcc 12.3.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,677 bytes |
コンパイル時間 | 1,142 ms |
コンパイル使用メモリ | 30,164 KB |
実行使用メモリ | 26,396 KB |
最終ジャッジ日時 | 2023-09-21 16:17:21 |
合計ジャッジ時間 | 6,553 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
9,924 KB |
testcase_01 | AC | 70 ms
10,584 KB |
testcase_02 | AC | 68 ms
10,040 KB |
testcase_03 | AC | 57 ms
10,856 KB |
testcase_04 | AC | 72 ms
10,852 KB |
testcase_05 | AC | 67 ms
16,916 KB |
testcase_06 | AC | 40 ms
12,996 KB |
testcase_07 | AC | 6 ms
16,152 KB |
testcase_08 | AC | 145 ms
26,328 KB |
testcase_09 | AC | 16 ms
14,312 KB |
testcase_10 | AC | 62 ms
22,712 KB |
testcase_11 | AC | 54 ms
20,248 KB |
testcase_12 | AC | 74 ms
17,148 KB |
testcase_13 | AC | 137 ms
25,992 KB |
testcase_14 | AC | 53 ms
22,408 KB |
testcase_15 | AC | 3 ms
10,688 KB |
testcase_16 | RE | - |
testcase_17 | AC | 148 ms
26,124 KB |
testcase_18 | AC | 147 ms
26,396 KB |
testcase_19 | AC | 151 ms
26,288 KB |
testcase_20 | AC | 152 ms
26,168 KB |
testcase_21 | AC | 3 ms
10,380 KB |
testcase_22 | AC | 3 ms
9,988 KB |
testcase_23 | AC | 86 ms
22,512 KB |
testcase_24 | RE | - |
testcase_25 | AC | 118 ms
26,212 KB |
testcase_26 | AC | 229 ms
20,644 KB |
testcase_27 | AC | 74 ms
19,116 KB |
ソースコード
#include <stdio.h> typedef struct Edge { struct Edge *next; int v; } edge; int main() { int i, N, Q; char S[200003]; scanf("%d %d", &N, &Q); scanf("%s", S); int u, w, M = 0, stack[200003], head = 0, indeg[200003] = {}, mate[200003], flag[200003]; edge *adj[200003] = {}, e[200003], *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[200003], par[200003][20], q[200003], 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 = par[u][0]; x != 0; par[w][j] = x, x = par[x][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) { for (j = 0; par[u][j+1] != par[w][j+1]; j++); u = par[u][j]; w = par[w][j]; } if (u != 0) printf("%d %d\n", u, mate[u]); else printf("-1\n"); } fflush(stdout); return 0; }