結果
問題 | No.2020 Sum of Common Prefix Length |
ユーザー | souta-1326 |
提出日時 | 2022-07-12 23:32:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 885 ms / 2,000 ms |
コード長 | 2,125 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,444 KB |
実行使用メモリ | 288,100 KB |
最終ジャッジ日時 | 2024-06-23 21:43:48 |
合計ジャッジ時間 | 24,064 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 179 ms
171,452 KB |
testcase_01 | AC | 177 ms
171,516 KB |
testcase_02 | AC | 174 ms
171,408 KB |
testcase_03 | AC | 259 ms
190,256 KB |
testcase_04 | AC | 254 ms
190,200 KB |
testcase_05 | AC | 250 ms
190,280 KB |
testcase_06 | AC | 253 ms
190,248 KB |
testcase_07 | AC | 525 ms
226,860 KB |
testcase_08 | AC | 519 ms
226,004 KB |
testcase_09 | AC | 537 ms
226,136 KB |
testcase_10 | AC | 539 ms
226,356 KB |
testcase_11 | AC | 537 ms
226,200 KB |
testcase_12 | AC | 519 ms
226,120 KB |
testcase_13 | AC | 527 ms
226,456 KB |
testcase_14 | AC | 535 ms
226,128 KB |
testcase_15 | AC | 885 ms
288,100 KB |
testcase_16 | AC | 876 ms
287,600 KB |
testcase_17 | AC | 615 ms
237,856 KB |
testcase_18 | AC | 573 ms
230,452 KB |
testcase_19 | AC | 585 ms
232,804 KB |
testcase_20 | AC | 646 ms
261,568 KB |
testcase_21 | AC | 726 ms
257,052 KB |
testcase_22 | AC | 809 ms
257,564 KB |
testcase_23 | AC | 745 ms
256,372 KB |
testcase_24 | AC | 773 ms
261,612 KB |
testcase_25 | AC | 755 ms
261,116 KB |
testcase_26 | AC | 508 ms
237,752 KB |
testcase_27 | AC | 171 ms
171,508 KB |
testcase_28 | AC | 623 ms
247,332 KB |
testcase_29 | AC | 619 ms
249,196 KB |
testcase_30 | AC | 622 ms
244,716 KB |
testcase_31 | AC | 638 ms
259,172 KB |
testcase_32 | AC | 642 ms
262,048 KB |
testcase_33 | AC | 577 ms
241,992 KB |
testcase_34 | AC | 679 ms
257,184 KB |
testcase_35 | AC | 669 ms
257,616 KB |
testcase_36 | AC | 789 ms
275,344 KB |
testcase_37 | AC | 690 ms
264,536 KB |
ソースコード
import sys from copy import deepcopy readline = sys.stdin.readline class Fenwick_Tree: def __init__(self,N:int): self.N = N self.dat = [0]*(N+1) def inc(self,p:int): p += 1 while p <= self.N: self.dat[p] += 1 p += p&-p def dec(self,p:int): p += 1 while p <= self.N: self.dat[p] -= 1 p += p&-p def _sum(self,r:int): s = 0 while r: s += self.dat[r] r -= r&-r return s class EulerTour: def __init__(self,G): self.N = len(G) self.begin = [0]*self.N self.end = [0]*self.N self.B_v = Fenwick_Tree(self.N*2) cnt = 0 f = 0 itr = [0]*self.N par = [0]*self.N par[f] = -1 while f != -1: if itr[f] == 0: self.begin[f] = cnt;cnt+=1 if itr[f] == len(G[f]): self.end[f] = cnt;cnt+=1 f = par[f] continue par[G[f][itr[f]]] = f itr[f]+=1 f = G[f][itr[f]-1] def add(self,p:int): self.B_v.inc(self.begin[p]) self.B_v.dec(self.end[p]) def query(self,p:int): return self.B_v._sum(self.begin[p]+1) def main(): N = int(readline()) S = [list(map(lambda c:ord(c)-97,readline().rstrip())) for _ in range(N)] Q = int(readline()) Query = [tuple()]*Q for i in range(Q): I = readline().split() Query[i] = tuple([int(I[0]),int(I[1])-1,(0 if I[0]=="2" else ord(I[2][0])-97)]) len_node = 1 nex = [[-1]*26 for i in range(400000)] S2 = deepcopy(S) for t,x,c in Query: if t == 1: S2[x].append(c) for i in range(N): now_node = 0 for z in S2[i]: if nex[now_node][z] == -1: nex[now_node][z] = len_node len_node += 1 #nex.append([-1]*26) now_node = nex[now_node][z] V = len_node Eul = EulerTour([[elem for elem in nex[i] if elem != -1] for i in range(V)]) now_nodes = [0]*N for i in range(N): for z in S[i]: now_nodes[i] = nex[now_nodes[i]][z] Eul.add(now_nodes[i]) for t,x,c in Query: if t == 1: now_nodes[x] = nex[now_nodes[x]][c] Eul.add(now_nodes[x]) else: print(Eul.query(now_nodes[x])) if __name__ == "__main__": main()