結果

問題 No.778 クリスマスツリー
ユーザー silversmithsilversmith
提出日時 2019-05-03 03:52:44
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,798 bytes
コンパイル時間 1,779 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 436,256 KB
最終ジャッジ日時 2023-08-30 05:03:27
合計ジャッジ時間 4,789 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
75,780 KB
testcase_01 AC 73 ms
71,052 KB
testcase_02 AC 73 ms
71,348 KB
testcase_03 AC 74 ms
71,288 KB
testcase_04 AC 74 ms
71,416 KB
testcase_05 AC 75 ms
71,492 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(200001)

class sorted_list:
    def __init__(self):
        self.list = []
    
    def _idx(self,val):
        if len(self.list) == 0:
            return 0
        else:
            left = 0
            right = len(self.list) -1
            mid = int((left + right)/2)
            if val < self.list[left]:
                return 0
            elif val > self.list[right]:
                return len(self.list)
            else:
                while right -left > 1:
                    mid = int((left + right)/2)
                    if val < self.list[mid]:
                        right = mid
                    else:
                        left = mid
                return mid if val < self.list[mid] else mid +1

    def insert(self,val):
        idx = self._idx(val)
        if idx < len(self.list):
            self.list.insert(self._idx(val), val)
        else:
            self.list.append(val)
    
    def remove(self, val):
        self.list.remove(val)

    def order_of_key(self, val):
        return self._idx(val)




class Node:
    def __init__(self):
        #self.parent_Node = None
        self.child_Node = []
         

class tree:
    def __init__(self, Nodes):
        self.Nodes = Nodes
        self.sorted_list=sorted_list()
    def push_edge(self,low, upper):
        self.Nodes[upper].child_Node.append(low)
    def dns(self, val):
        r = self.sorted_list.order_of_key(val)
        self.sorted_list.insert(val)
        r += sum([self.dns(node) for node in self.Nodes[val].child_Node])
        self.sorted_list.remove(val)
    
        return r
    

N = int(input())
A = map(int, input().split())


t = tree([Node() for i in range(N)])
for idx, a in enumerate(A):
    t.push_edge(idx + 1, a)
A = None
N = None
print(t.dns(0))
0