結果

問題 No.2020 Sum of Common Prefix Length
ユーザー souta-1326souta-1326
提出日時 2022-07-12 23:26:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 887 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 86,684 KB
実行使用メモリ 280,456 KB
最終ジャッジ日時 2023-09-06 02:45:08
合計ジャッジ時間 26,930 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 252 ms
183,936 KB
testcase_01 AC 223 ms
183,612 KB
testcase_02 AC 216 ms
183,508 KB
testcase_03 AC 287 ms
190,924 KB
testcase_04 AC 289 ms
191,572 KB
testcase_05 AC 286 ms
191,680 KB
testcase_06 AC 286 ms
191,424 KB
testcase_07 AC 524 ms
209,740 KB
testcase_08 AC 582 ms
210,752 KB
testcase_09 AC 516 ms
210,704 KB
testcase_10 AC 513 ms
210,488 KB
testcase_11 AC 523 ms
210,376 KB
testcase_12 AC 530 ms
210,224 KB
testcase_13 AC 518 ms
211,100 KB
testcase_14 AC 508 ms
210,348 KB
testcase_15 AC 846 ms
279,072 KB
testcase_16 AC 861 ms
280,456 KB
testcase_17 AC 657 ms
223,444 KB
testcase_18 AC 634 ms
214,956 KB
testcase_19 AC 634 ms
217,548 KB
testcase_20 AC 796 ms
253,900 KB
testcase_21 AC 857 ms
246,228 KB
testcase_22 AC 838 ms
246,940 KB
testcase_23 AC 834 ms
246,512 KB
testcase_24 AC 869 ms
253,452 KB
testcase_25 AC 887 ms
251,492 KB
testcase_26 AC 585 ms
223,188 KB
testcase_27 AC 222 ms
183,632 KB
testcase_28 AC 598 ms
230,916 KB
testcase_29 AC 616 ms
234,696 KB
testcase_30 AC 595 ms
230,356 KB
testcase_31 AC 734 ms
250,384 KB
testcase_32 AC 765 ms
250,852 KB
testcase_33 AC 676 ms
225,720 KB
testcase_34 AC 720 ms
240,680 KB
testcase_35 AC 701 ms
240,132 KB
testcase_36 AC 775 ms
259,984 KB
testcase_37 AC 714 ms
247,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
  query = lambda self,p:self.B_v._sum(self.begin[p]+1)

def main():
  N = int(readline())
  S = tuple(list(map(lambda c:ord(c)-97,readline().rstrip())) for _ in range(N))
  Q = int(readline())
  T = [0]*Q
  X = [0]*Q
  C = [0]*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] = ord(I[2][0])-97
  len_node = 1
  nex = [[-1]*26 for i in range(400000)]
  S2 = deepcopy(S)
  for i in range(Q):
    if T[i] == 1:
      S2[X[i]].append(C[i])
  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
      now_node = nex[now_node][z]
 
  V = len_node
  Eul = EulerTour(tuple(tuple(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 zip(T,X,C):
    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()
0