Problem p02964 (python)

Fixed submission:
from bisect import bisect_right
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))
a_to_i = [[] for _ in range(max(A)+1)]
for i in range(N):
    a_to_i[A[i]].append(i)
next_index = [-1]*N
for i in range(N):
    a = A[i]
    j = bisect_right(a_to_i[a], i)
    if j == len(a_to_i[a]):
        next_index[i] = a_to_i[a][0]+N
    else: next_index[i] = a_to_i[a][j]
now = 0
cnt = 0
while True:
    i = next_index[now]
    if i>=N:
        i%=N
        cnt+=1
    else: now = i
    i+=1
    if i>=N:
        cnt+=1
        i%=N
    now = i
    if now == 0:
        break
K%=cnt
if K==0:
    print()
    sys.exit()
ans = [A[0]]
K*=N
K-=1
now = 0
while now<K:
    ni = next_index[now%N]+1
    if(now//N)*N+ni-1 == K:
        ans.pop()
        break
    if (now//N)*N+ni>K:
        now+=1
        ans.append(A[now%N])
    else:
        now = (now//N)*N+ni
        ans[-1] = A[now%N]




print(*ans, sep = ' ')

Buggy submission (verdict: Runtime Error):
import random #入力整数列Aの生成に使用

class Node:
    def __init__(self, data): #コンストラクタ
        self.data = data #ノードがもつ数値
        self.left = None #左エッジ
        self.right = None #右エッジ


class BST:
    def __init__(self, number_list): #コンストラクタ
        self.root = None #ルートノード初期化
        for node in number_list: #数値を持つ配列から二分木を生成
            self.insert(node) #挿入メソッドを使ってノードを挿入する
    #挿入
    def insert(self, data):
        n = self.root
        if n == None:
            self.root = Node(data)
            return
        else:
            while True:
                entry = n.data
                if data < entry:
                    if n.left is None:
                        n.left = Node(data)
                        return
                    n = n.left
                elif data > entry:
                    if n.right is None:
                        n.right = Node(data)
                        return
                    n = n.right
                else:
                    n.data = data
                    return
    #検索機能(インターフェース)
    def search(self, search):
        searcher = self._search_bool(search)
        if searcher is None:
            return False
        elif searcher == True:
            return True
        elif searcher == False:
            return False

    #検索機能本体(出力:boolean),深さ優先探索
    #nodeのvisitedはpopで代用
    def _search_bool(self, search):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node.data == search:
                    return True
                if node.right is not None:
                    lst.append(node.right)
                if node.left is not None:
                    lst.append(node.left)
            return False

    def inorder(self,node): #中順探索 l->r->p^n
        if node is not None:
            self.inorder(node.left)
            print(node.data)
            self.inorder(node.right)
B = BST([0])
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
index = [-1]*(10**5*2+10)
last = [-1]*(N+1)
for i in range(N)[::-1]:
    ind = index[A[i]]
    if ind>=0:
        last[i] = last[ind+1]
        index[A[i]] = -1
    else:
        last[i] = A[i]
        index[A[i]] = i
first_index = [-1]*(10**5*2+10)
for i in range(N):
    if first_index[A[i]]<0:
        first_index[A[i]] = i
loop = [0]
ind = 0
while True:
    next_index = (first_index[last[ind]]+1)%N
    if B.search(next_index):
        for i in range(len(loop)):
            if loop[i]==next_index:
                start = i
                break
        break
    loop.append(next_index)
    B.insert(next_index)
    ind = next_index
    
ll = len(loop)

index = [[] for _ in range(10**5*2+10)]
ans = []
if K-1>=ll:
    ii = (K-start)%(ll-start)-1+start
    if (K-start)%(ll-start)==0:
        ii+=ll
else:
    ii = K-1
for i in range(loop[ii], N):
    index[A[i]].append(i)
i=loop[ii]
while i<N:
    if index[A[i]][-1]==i:
        ans.append((str(A[i])))
        i+=1
    else:
        i=index[A[i]][bisect.bisect_left(index[A[i]], i)+1]+1
print(' '.join(ans))

Buggy submission (verdict: Runtime Error):
import random #入力整数列Aの生成に使用

class Node:
    def __init__(self, data): #コンストラクタ
        self.data = data #ノードがもつ数値
        self.left = None #左エッジ
        self.right = None #右エッジ


class BST:
    def __init__(self, number_list): #コンストラクタ
        self.root = None #ルートノード初期化
        for node in number_list: #数値を持つ配列から二分木を生成
            self.insert(node) #挿入メソッドを使ってノードを挿入する
    #挿入
    def insert(self, data):
        n = self.root
        if n == None:
            self.root = Node(data)
            return
        else:
            while True:
                entry = n.data
                if data < entry:
                    if n.left is None:
                        n.left = Node(data)
                        return
                    n = n.left
                elif data > entry:
                    if n.right is None:
                        n.right = Node(data)
                        return
                    n = n.right
                else:
                    n.data = data
                    return
    #検索機能(インターフェース)
    def search(self, search):
        searcher = self._search_bool(search)
        if searcher is None:
            return False
        elif searcher == True:
            return True
        elif searcher == False:
            return False

    #検索機能本体(出力:boolean),深さ優先探索
    #nodeのvisitedはpopで代用
    def _search_bool(self, search):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node.data == search:
                    return True
                if node.right is not None:
                    lst.append(node.right)
                if node.left is not None:
                    lst.append(node.left)
            return False

    def inorder(self,node): #中順探索 l->r->p^n
        if node is not None:
            self.inorder(node.left)
            print(node.data)
            self.inorder(node.right)
B = BST([0])
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
index = [-1]*(10**5*2+10)
last = [-1]*(N+1)
for i in range(N)[::-1]:
    ind = index[A[i]]
    if ind>=0:
        last[i] = last[ind+1]
        index[A[i]] = -1
    else:
        last[i] = A[i]
        index[A[i]] = i
first_index = [-1]*(10**5*2+10)
for i in range(N):
    if first_index[A[i]]<0:
        first_index[A[i]] = i
loop = [0]
ind = 0
while True:
    next_index = (first_index[last[ind]]+1)%N
    if B.search(next_index):
        for i in range(len(loop)):
            if loop[i]==next_index:
                start = i
                break
        break
    loop.append(next_index)
    B.insert(next_index)
    ind = next_index
    
ll = len(loop)

index = [[] for _ in range(10**5*2+10)]
ans = []
if K-1>=ll:
    ii = loop[(K-start)%(ll-start)-1+start]
else:
    ii = K-1
for i in range(loop[ii], N):
    index[A[i]].append(i)
i=loop[ii]
while i<N:
    if index[A[i]][-1]==i:
        ans.append((str(A[i])))
        i+=1
    else:
        i=index[A[i]][bisect.bisect_left(index[A[i]], i)+1]+1
print(' '.join(ans))

Buggy submission (verdict: Runtime Error):
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
index = [-1]*(10**5*2+10)
last = [-1]*(N+1)
for i in range(N)[::-1]:
    ind = index[A[i]]
    if ind>=0:
        last[i] = last[ind+1]
        index[A[i]] = -1
    else:
        last[i] = A[i]
        index[A[i]] = i
first_index = [-1]*(10**5*2+10)
for i in range(N):
    if first_index[A[i]]<0:
        first_index[A[i]] = i
loop = [0]
ind = 0
while True:
    next_index = first_index[last[ind]]+1
    if next_index == N or next_index == 0:
        break
    loop.append(next_index)
    ind = next_index
ll = len(loop)
index = [[] for _ in range(10**5+10)]
ans = []
for i in range(loop[K%ll-1], N):
    index[A[i]].append(i)
i=loop[K%ll-1]
while i<N:
    if index[A[i]][-1]==i:
        ans.append((str(A[i])))
        i+=1
    else:
        i=index[A[i]][bisect.bisect_left(index[A[i]], i)+1]+1
print(' '.join(ans))