結果

問題 No.2020 Sum of Common Prefix Length
ユーザー souta-1326souta-1326
提出日時 2022-07-12 14:44:21
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,151 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 276,496 KB
最終ジャッジ日時 2024-06-23 12:08:43
合計ジャッジ時間 24,908 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,688 KB
testcase_01 AC 37 ms
52,736 KB
testcase_02 AC 37 ms
52,864 KB
testcase_03 AC 108 ms
77,992 KB
testcase_04 AC 109 ms
78,244 KB
testcase_05 AC 110 ms
77,904 KB
testcase_06 AC 104 ms
78,236 KB
testcase_07 AC 323 ms
95,872 KB
testcase_08 AC 321 ms
95,744 KB
testcase_09 AC 331 ms
95,744 KB
testcase_10 AC 329 ms
95,744 KB
testcase_11 AC 328 ms
95,820 KB
testcase_12 AC 322 ms
95,744 KB
testcase_13 AC 330 ms
95,852 KB
testcase_14 AC 325 ms
96,228 KB
testcase_15 AC 442 ms
138,496 KB
testcase_16 AC 446 ms
138,496 KB
testcase_17 AC 516 ms
147,504 KB
testcase_18 AC 455 ms
128,128 KB
testcase_19 AC 455 ms
131,608 KB
testcase_20 AC 867 ms
236,320 KB
testcase_21 AC 736 ms
208,196 KB
testcase_22 AC 747 ms
208,392 KB
testcase_23 AC 741 ms
204,948 KB
testcase_24 AC 783 ms
220,368 KB
testcase_25 AC 779 ms
218,536 KB
testcase_26 AC 1,941 ms
261,252 KB
testcase_27 AC 40 ms
52,736 KB
testcase_28 AC 376 ms
109,696 KB
testcase_29 AC 384 ms
111,104 KB
testcase_30 AC 387 ms
109,056 KB
testcase_31 AC 1,473 ms
276,496 KB
testcase_32 TLE -
testcase_33 TLE -
testcase_34 AC 498 ms
134,136 KB
testcase_35 AC 482 ms
134,712 KB
testcase_36 AC 1,410 ms
229,504 KB
testcase_37 AC 541 ms
154,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
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 = [readline().rstrip() for _ in range(N)]
  Q = int(readline())
  T = [0]*Q
  X = [0]*Q
  C = [""]*Q
  for i in range(Q):
    I = readline().split()
    T[i] = int(I[0]);X[i] = int(I[1])-1
    if T[i]==1:C[i] = I[2][0]
  
  path = [[0] for _ in range(N)]
  len_node = 1
  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)-97
      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]
      path[i].append(now_node)
  
  V = len_node
  G = [[] for _ in range(V)]
  for i in range(V):
    G[i].extend(elem for elem in nex[i] if elem != -1)
  Eul = EulerTour(G)
  len_S = list(map(len,S))
  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:
      len_S[X[i]] += 1
      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