結果

問題 No.515 典型LCP
ユーザー mkawa2mkawa2
提出日時 2023-08-18 18:36:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 746 ms / 1,000 ms
コード長 2,995 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 86,884 KB
実行使用メモリ 142,316 KB
最終ジャッジ日時 2023-08-18 18:36:39
合計ジャッジ時間 7,711 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 746 ms
115,756 KB
testcase_01 AC 695 ms
142,316 KB
testcase_02 AC 376 ms
86,596 KB
testcase_03 AC 105 ms
71,732 KB
testcase_04 AC 103 ms
71,476 KB
testcase_05 AC 225 ms
83,840 KB
testcase_06 AC 243 ms
83,964 KB
testcase_07 AC 231 ms
84,236 KB
testcase_08 AC 292 ms
84,048 KB
testcase_09 AC 309 ms
86,056 KB
testcase_10 AC 317 ms
86,508 KB
testcase_11 AC 321 ms
86,196 KB
testcase_12 AC 316 ms
86,156 KB
testcase_13 AC 294 ms
86,040 KB
testcase_14 AC 232 ms
91,604 KB
testcase_15 AC 217 ms
89,584 KB
testcase_16 AC 220 ms
89,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class SparseTable:
    def __init__(self, op, e, aa):
        self.op = op
        self.e = e
        self.w = w = len(aa)
        self.h = h = w.bit_length()
        table = [aa]+[[e() for _ in range(w)] for _ in range(h-1)]
        tablei1 = table[0]
        for i in range(1, h):
            tablei = table[i]
            for j in range(w-(1 << i)+1):
                rj = j+(1 << (i-1))
                tablei[j] = op(tablei1[j], tablei1[rj])
            tablei1 = tablei
        self.table = table

    # [l,r)の取得
    def prod(self, l, r):
        if l == r: return self.e()
        i = (r-l).bit_length()-1
        tablei = self.table[i]
        return self.op(tablei[l], tablei[r-(1 << i)])

    # ok(prod(l,r))=Trueとなる最大r
    def max_right(self, l, ok):
        for i in range(self.h)[::-1]:
            if l+(1 << i) > self.w: continue
            if ok(self.table[i][l]): l += 1 << i
        return l

    # ok(prod(l,r))=Trueとなる最小l
    def min_left(self, r, ok):
        for i in range(self.h)[::-1]:
            l = r-(1 << i)
            if l < 0: continue
            if ok(self.table[i][l]): r = l
        return r

from collections import defaultdict

n = II()
ss = [None]*n
for i in range(n): ss[i] = [ord(c)-96 for c in SI()]+[0]

lcp = [-1]*n
lr = [(0, n)]
ii = list(range(n))
j = 0
while lr:
    plr, lr = lr, []
    for l, r in plr:
        c2i = defaultdict(list)
        for p in range(l, r):
            i = ii[p]
            c = ss[i][j]
            c2i[c].append(i)
        for i in c2i[0]:
            ii[l] = i
            if lcp[l-1] == -1: lcp[l-1] = j
            l += 1
        for c in sorted(c2i):
            if c==0:continue
            d = len(c2i[c])
            if d > 1: lr.append((l, l+d))
            if lcp[l-1] == -1: lcp[l-1] = j
            for i in c2i[c]:
                ii[l] = i
                l += 1
    j += 1
# print(lcp)
# print(ii)

sp = SparseTable(min, lambda: 400000, lcp)

i2p = [0]*n
for p, i in enumerate(ii): i2p[i] = p
m, x, d = LI()
ans=0
for _ in range(m):
    i = x//(n-1)
    j = x%(n-1)
    if i > j: i, j = j, i
    else: j += 1
    p,q=i2p[i],i2p[j]
    if p>q:p,q=q,p
    ans+=sp.prod(p, q)
    # print(i,j,p,q,ans)
    x = (x+d)%(n*(n-1))
print(ans)
0