結果

問題 No.2020 Sum of Common Prefix Length
ユーザー siganaisiganai
提出日時 2022-07-23 17:09:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,112 ms / 2,000 ms
コード長 3,058 bytes
コンパイル時間 343 ms
コンパイル使用メモリ 87,348 KB
実行使用メモリ 319,852 KB
最終ジャッジ日時 2023-09-18 16:04:36
合計ジャッジ時間 35,011 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 318 ms
203,536 KB
testcase_01 AC 321 ms
203,740 KB
testcase_02 AC 319 ms
203,424 KB
testcase_03 AC 403 ms
209,900 KB
testcase_04 AC 384 ms
209,752 KB
testcase_05 AC 384 ms
210,172 KB
testcase_06 AC 386 ms
209,900 KB
testcase_07 AC 824 ms
266,420 KB
testcase_08 AC 836 ms
266,364 KB
testcase_09 AC 779 ms
266,584 KB
testcase_10 AC 749 ms
266,324 KB
testcase_11 AC 760 ms
266,080 KB
testcase_12 AC 763 ms
266,056 KB
testcase_13 AC 761 ms
265,996 KB
testcase_14 AC 763 ms
266,224 KB
testcase_15 AC 971 ms
295,820 KB
testcase_16 AC 993 ms
295,500 KB
testcase_17 AC 917 ms
294,624 KB
testcase_18 AC 879 ms
282,984 KB
testcase_19 AC 892 ms
286,036 KB
testcase_20 AC 932 ms
316,528 KB
testcase_21 AC 1,112 ms
305,656 KB
testcase_22 AC 1,046 ms
305,656 KB
testcase_23 AC 1,050 ms
315,204 KB
testcase_24 AC 1,063 ms
314,656 KB
testcase_25 AC 1,061 ms
317,972 KB
testcase_26 AC 793 ms
302,276 KB
testcase_27 AC 326 ms
203,700 KB
testcase_28 AC 916 ms
274,660 KB
testcase_29 AC 902 ms
275,212 KB
testcase_30 AC 910 ms
274,364 KB
testcase_31 AC 887 ms
310,716 KB
testcase_32 AC 907 ms
311,612 KB
testcase_33 AC 864 ms
303,304 KB
testcase_34 AC 969 ms
288,592 KB
testcase_35 AC 959 ms
289,156 KB
testcase_36 AC 919 ms
319,852 KB
testcase_37 AC 1,014 ms
301,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env PyPy3

from collections import Counter, defaultdict, deque
import itertools
import re
import math
from functools import reduce
import operator
import bisect
from heapq import *
import functools
mod=998244353

import sys
input=sys.stdin.readline


class Bit:#0-indexed を 1-indexedに変換するので、0-indexedを入れる
    def __init__(self, n):
        self.size = n + 1
        self.tree = [0] * (n + 2)
        self.depth = (n + 1).bit_length()

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
 
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def lower_bound(self, x):
        """ 累積和がx以上になる最小のindexと、その直前までの累積和 """
        sum_ = 0
        pos = 0
        for i in range(self.depth, -1, -1):
            k = pos + (1 << i)
            if k <= self.size and sum_ + self.tree[k] < x:
                sum_ += self.tree[k]
                pos += 1 << i
        return pos, sum_

n = int(input())
sl = [[] for _ in range(n)]
for i in range(n):
    st = input().rstrip()
    for ss in st:
        sl[i].append(ord(ss) - ord('a'))
sl2 = [sl[i][:] for i in range(n)]
q = int(input())
qry = []
for _ in range(q):
    qe = list(map(str,input().rstrip().split()))
    if qe[0] == '1':
        sl2[int(qe[1]) - 1].append(ord(qe[2]) - ord('a'))
    qry.append(qe)
nex = [[-1] * 26 for _ in range(400000)]
lenn = 1
g = [[] for _ in range(400000)]
for i in range(n):
    now = 0
    for z in sl2[i]:
        if nex[now][z] == -1:
            nex[now][z] = lenn
            g[now].append(lenn)
            g[lenn].append(now)
            lenn += 1
        now = nex[now][z]
def EulerTour(n, X, i0):
    done = [0] * n
    par = [-1] * n
    Q = deque([~i0, i0]) # 根をスタックに追加
    ET = []
    while Q:
        i = Q.pop()
        if i >= 0: # 行きがけの処理
            done[i] = 1
            ET.append(i)
            for a in X[i]:
                if done[a]: continue
                par[a] = i
                Q.append(~a) # 帰りがけの処理をスタックに追加
                Q.append(a) # 行きがけの処理をスタックに追加
 
        else: # 帰りがけの処理
            i = ~i
            ET.append(i)
 
    return ET
ET = EulerTour(lenn,g,0)
beg = [-1] * lenn
end = [-1] * lenn
for i in range(len(ET)):
    if beg[ET[i]] == -1:
        beg[ET[i]] = i
    else:
        end[ET[i]] = i
bit = Bit(lenn * 2)
pos = [0] * n
for i in range(n):
    now = 0
    for z in sl[i]:
        now = nex[now][z]
        bit.add(beg[now],1)
        bit.add(end[now],-1)
    pos[i] = now
for i in range(q):
    if qry[i][0] == '1':
        x = int(qry[i][1]) - 1
        z = ord(qry[i][2]) - ord('a')
        now = nex[pos[x]][z]
        bit.add(beg[now],1)
        bit.add(end[now],-1)
        pos[x] = now
    else:
        x = int(qry[i][1]) - 1
        print(bit.sum(beg[pos[x]]))
0