結果

問題 No.515 典型LCP
ユーザー shotoyooshotoyoo
提出日時 2020-09-20 17:49:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 683 ms / 1,000 ms
コード長 1,458 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 131,804 KB
最終ジャッジ日時 2023-09-06 16:25:51
合計ジャッジ時間 6,161 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 683 ms
131,804 KB
testcase_01 AC 646 ms
131,536 KB
testcase_02 AC 292 ms
78,084 KB
testcase_03 AC 78 ms
71,108 KB
testcase_04 AC 74 ms
70,956 KB
testcase_05 AC 186 ms
77,916 KB
testcase_06 AC 216 ms
77,684 KB
testcase_07 AC 193 ms
77,848 KB
testcase_08 AC 261 ms
78,028 KB
testcase_09 AC 207 ms
78,036 KB
testcase_10 AC 224 ms
78,872 KB
testcase_11 AC 217 ms
78,376 KB
testcase_12 AC 218 ms
78,372 KB
testcase_13 AC 199 ms
77,836 KB
testcase_14 AC 117 ms
77,812 KB
testcase_15 AC 180 ms
77,964 KB
testcase_16 AC 180 ms
77,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")



class SparseTable:
    op = min
    def __init__(self, a):
        n = len(a)
        t = []
        c = 1
        l = a
        while True:
            t.append(l)
            c *= 2
            if c>n:
                break
            v = c//2
            l = [self.op(l[i], l[i+v]) for i in range(n-v)] + l[-v:]
        self.t = t
    def query(self, l, r):
        """a[l,r)の最小値
        assert l<r and l,r<n
        """
        # t[i][j]: a[j:j+2^i)の最小値
        b = (r-l).bit_length()-1
        return self.op(self.t[b][l], self.t[b][r-(1<<b)])

def sub(s,t):
    i = 0
    m = min(len(s), len(t))
    while i<m and s[i]==t[i]:
        i += 1
    return i
from operator import itemgetter
n = int(input())
s = [input() for _ in range(n)]
ss = sorted(list(enumerate(s)), key=itemgetter(1))

vals = [sub(ss[i][1], ss[i+1][1]) for i in range(n-1)]
st = SparseTable(vals)

rsa = [None]*n
for i,item in enumerate(ss):
    rsa[item[0]] = i
    
M,x,d = map(int, input().split())
ans = 0
def sub(i,j):
    ii,jj = rsa[i], rsa[j]
    if ii>jj:
        ii,jj = jj,ii
    return st.query(ii,jj)
for k in range(1, M+1):
    i = (x // (n-1)) + 1
    j = (x % (n-1)) + 1
    if i>j:
        i,j = j,i
    else:
        j += 1
    x = (x + d) % (n * (n-1))
#     print(i,j)
    ans += sub(i-1,j-1)
print(ans)
0