結果

問題 No.2020 Sum of Common Prefix Length
ユーザー souta-1326souta-1326
提出日時 2022-05-24 23:19:42
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,076 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 81,400 KB
実行使用メモリ 260,836 KB
最終ジャッジ日時 2023-10-20 19:00:05
合計ジャッジ時間 22,253 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,328 KB
testcase_01 AC 39 ms
53,328 KB
testcase_02 AC 39 ms
53,328 KB
testcase_03 AC 126 ms
77,496 KB
testcase_04 AC 126 ms
77,368 KB
testcase_05 AC 122 ms
77,536 KB
testcase_06 AC 122 ms
77,552 KB
testcase_07 AC 428 ms
96,700 KB
testcase_08 AC 446 ms
96,756 KB
testcase_09 AC 436 ms
96,784 KB
testcase_10 AC 431 ms
96,664 KB
testcase_11 AC 444 ms
96,792 KB
testcase_12 AC 439 ms
96,752 KB
testcase_13 AC 446 ms
97,040 KB
testcase_14 AC 440 ms
96,796 KB
testcase_15 AC 589 ms
128,136 KB
testcase_16 AC 587 ms
128,432 KB
testcase_17 AC 563 ms
154,800 KB
testcase_18 AC 510 ms
133,748 KB
testcase_19 AC 526 ms
137,820 KB
testcase_20 AC 1,182 ms
260,836 KB
testcase_21 AC 765 ms
220,256 KB
testcase_22 AC 772 ms
220,464 KB
testcase_23 AC 774 ms
218,588 KB
testcase_24 AC 796 ms
238,376 KB
testcase_25 AC 811 ms
237,132 KB
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Fenwick_Tree:
  def __init__(self,N:int):
    self.N = N
    self.dat = [0]*(N+1)
  def add(self,p:int,x:int):
    p += 1
    while p <= self.N:
      self.dat[p] += x
      p += p&-p
  def _sum(self,r:int):
    s = 0
    while r:
      s += self.dat[r]
      r -= r&-r
    return s
  sum = lambda self,l,r:self._sum(r)-self._sum(l)

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,num:int =1):
    self.B_v.add(self.begin[p],num)
    self.B_v.add(self.end[p],-num)
  def query(self,p:int):
    return self.B_v.sum(0,self.begin[p]+1)

def main():
  N = int(input())
  S = [input() for _ in range(N)]
  Q = int(input())
  T = [0]*Q
  X = [0]*Q
  C = [""]*Q
  for i in range(Q):
    I = input().split()
    T[i] = int(I[0]);X[i] = int(I[1])-1
    if T[i]==1:C[i] = I[2]
  
  path = [[0] for _ in range(N)]
  node = [""]
  nex = [[-1]*26]
  S2 = S[:]
  for i in range(Q):
    if T[i] == 1:
      S2[X[i]] += C[i]
  for i in range(N):
    now_node = 0
    for c in S2[i]:
      z = ord(c)-ord("a")
      if nex[now_node][z] == -1:
        nex[now_node][z] = len(node)
        node.append(c)
        nex.append([-1]*26)
      now_node = nex[now_node][z]
      path[i].append(now_node)
  
  V = len(node)
  G = [[] for _ in range(V)]
  for i in range(V):
    for j in range(26):
      if nex[i][j] != -1:
        G[i].append(nex[i][j])
  Eul = EulerTour(G)
  for i in range(N):
    for j in range(len(S[i])):Eul.add(path[i][j+1])
  for i in range(Q):
    if T[i] == 1:
      S[X[i]] += C[i]
      Eul.add(path[X[i]][len(S[X[i]])])
    else:
      print(Eul.query(path[X[i]][len(S[X[i]])]))

if __name__ == "__main__":
  main()
0