結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | fgwiebfaoish |
提出日時 | 2021-12-10 21:20:11 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 476 ms / 2,000 ms |
コード長 | 5,007 bytes |
コンパイル時間 | 1,036 ms |
コンパイル使用メモリ | 110,592 KB |
実行使用メモリ | 76,400 KB |
最終ジャッジ日時 | 2024-07-18 15:29:12 |
合計ジャッジ時間 | 13,274 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 244 ms
25,856 KB |
testcase_01 | AC | 275 ms
26,368 KB |
testcase_02 | AC | 259 ms
25,856 KB |
testcase_03 | AC | 230 ms
25,088 KB |
testcase_04 | AC | 248 ms
26,112 KB |
testcase_05 | AC | 211 ms
39,552 KB |
testcase_06 | AC | 136 ms
27,520 KB |
testcase_07 | AC | 41 ms
26,624 KB |
testcase_08 | AC | 424 ms
64,496 KB |
testcase_09 | AC | 78 ms
33,152 KB |
testcase_10 | AC | 219 ms
50,676 KB |
testcase_11 | AC | 180 ms
41,976 KB |
testcase_12 | AC | 229 ms
40,192 KB |
testcase_13 | AC | 398 ms
63,100 KB |
testcase_14 | AC | 200 ms
47,080 KB |
testcase_15 | AC | 24 ms
18,304 KB |
testcase_16 | AC | 421 ms
64,240 KB |
testcase_17 | AC | 431 ms
64,624 KB |
testcase_18 | AC | 419 ms
64,628 KB |
testcase_19 | AC | 411 ms
63,980 KB |
testcase_20 | AC | 416 ms
64,744 KB |
testcase_21 | AC | 24 ms
18,176 KB |
testcase_22 | AC | 23 ms
18,560 KB |
testcase_23 | AC | 308 ms
51,580 KB |
testcase_24 | AC | 348 ms
57,968 KB |
testcase_25 | AC | 423 ms
62,708 KB |
testcase_26 | AC | 476 ms
76,400 KB |
testcase_27 | AC | 390 ms
73,320 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; static class Program{ const int mod=(int)1e9+7; static List<int>[] li; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; int n=(s[0]>>1)+1,m=s[1]; li=new List<int>[n+1]; for(int i=1;i<=n;i++){li[i]=new List<int>();} var t=sc.S; var h=new int[t.Length]; var d=new int[n+1][]; int p=1,g=-1; Fu(1); void Fu(int a){ g++; while(g<t.Length){ if(t[g]=='('){ li[a].Add(++p); li[p].Add(a); h[g]=p; d[p]=new int[]{g+1,0}; Fu(p); } else{ h[g]=a; d[a][1]=g+1; g++; return; } } } var al=new Alca(li,true,1); StringBuilder sb=new StringBuilder(); for(int i = 0;i<m;i++) { var e=sc.Ia; var f=al.Lca(h[e[0]-1],h[e[1]-1]); sb.Append((f==1?"-1":string.Join(" ",d[f]))+"\n"); } Console.Write(sb); } } public class Alca{ private List<int>[] li; private readonly int[] at,l; public readonly int[] ix,de; private readonly int[][] g; private int f=0; public Alca(List<int>[] li,bool bo,int r){ int n=li.Length,z=(n<<1)-3,m=0; this.li=li; at=new int[z]; ix=new int[n]; de=new int[n]; if(bo){Fu1(r,1);} else{ for(int i = 1;i<n;i++) { if(de[i]==0){Fu1(i,1);f++;} } } m=(int)Log(z,2)+1; g=new int[m][]; g[0]=new int[z]; l=new int[z+1]; for(int i=0;i<z;i++){ g[0][i]=i; l[i+1]=l[i>>1]+1; } for(int i=1,k=1;i<m;i++,k<<=1){ int p=z+1-(k<<1); g[i]=new int[p]; for(int j=0;j<p;j++){g[i][j]=de[at[g[i-1][j]]]<de[at[g[i-1][j+k]]]?g[i-1][j]:g[i-1][j+k];} } } public int Lca(int a,int b){return Get(ix[a],ix[b]);} public int Di(int a,int b){ int p=Get(ix[a],ix[b]); return p!=0?de[a]+de[b]-(de[p]<<1):-1; } private int Get(int a,int b){ if(a>b){(a,b)=(b,a);} int p=l[b-a]; return at[de[at[g[p][a]]]<de[at[g[p][b+1-(1<<p)]]]?g[p][a]:g[p][b+1-(1<<p)]]; } private void Fu1(int a,int g){ at[f]=a; de[a]=g; ix[a]=f; f++; for(int i=0;i<li[a].Count;i++){ if(de[li[a][i]]==0){ Fu1(li[a][i],g+1); at[f]=a; f++; } } } } public class Sc{ public int I{get{return int.Parse(Console.ReadLine());}} public long L{get{return long.Parse(Console.ReadLine());}} public double D{get{return double.Parse(Console.ReadLine());}} public string S{get{return Console.ReadLine();}} public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}} public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}} public string[] Sa{get{return Console.ReadLine().Split();}} public object[] Oa{get{return Console.ReadLine().Split();}} public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}} public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);} public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);} public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}} public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);} public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);} public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}} public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);} public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;} public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;} public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;} public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;} public (T,T) Tp2<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]));} public (T,T,T) Tp3<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]),Ct<T>(s[2]));} public (T,T,T,T) Tp4<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]),Ct<T>(s[2]),Ct<T>(s[3]));} public (T,T,T,T,T) Tp5<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]),Ct<T>(s[2]),Ct<T>(s[3]),Ct<T>(s[4]));} public (T1,T2) Tp2<T1,T2>(){var s=Console.ReadLine().Split();return (Ct<T1>(s[0]),Ct<T2>(s[1]));} public (T1,T1,T2) Tp3<T1,T2>(){var s=Console.ReadLine().Split();return (Ct<T1>(s[0]),Ct<T1>(s[1]),Ct<T2>(s[2]));} private T Ct<T>(string s){return (T)Convert.ChangeType(s,typeof(T));} }