結果

問題 No.2020 Sum of Common Prefix Length
ユーザー souta-1326souta-1326
提出日時 2022-07-12 22:57:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,159 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 11,172 KB
実行使用メモリ 209,392 KB
最終ジャッジ日時 2023-09-06 02:04:47
合計ジャッジ時間 43,385 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 524 ms
118,904 KB
testcase_01 AC 519 ms
118,904 KB
testcase_02 AC 521 ms
119,012 KB
testcase_03 AC 532 ms
119,200 KB
testcase_04 AC 532 ms
119,296 KB
testcase_05 AC 530 ms
119,304 KB
testcase_06 AC 529 ms
119,300 KB
testcase_07 AC 1,360 ms
141,504 KB
testcase_08 AC 1,330 ms
141,420 KB
testcase_09 AC 1,336 ms
141,356 KB
testcase_10 AC 1,338 ms
141,216 KB
testcase_11 AC 1,370 ms
141,744 KB
testcase_12 AC 1,395 ms
141,164 KB
testcase_13 AC 1,358 ms
141,384 KB
testcase_14 AC 1,448 ms
141,392 KB
testcase_15 AC 1,909 ms
192,804 KB
testcase_16 AC 1,868 ms
192,784 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,801 ms
164,736 KB
testcase_27 AC 536 ms
119,008 KB
testcase_28 AC 1,526 ms
161,696 KB
testcase_29 AC 1,571 ms
162,932 KB
testcase_30 AC 1,544 ms
156,896 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
権限があれば一括ダウンロードができます

ソースコード

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])
  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())
  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] = 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
        #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 i in range(Q):
    if T[i] == 1:
      now_nodes[X[i]] = nex[now_nodes[X[i]]][C[i]]
      Eul.add(now_nodes[X[i]])
    else:
      print(Eul.query(now_nodes[X[i]]))

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