結果
問題 | No.778 クリスマスツリー |
ユーザー | shotoyoo |
提出日時 | 2021-07-30 19:26:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 450 ms / 2,000 ms |
コード長 | 2,316 bytes |
コンパイル時間 | 263 ms |
コンパイル使用メモリ | 87,140 KB |
実行使用メモリ | 132,840 KB |
最終ジャッジ日時 | 2023-10-13 22:44:48 |
合計ジャッジ時間 | 5,684 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 69 ms
71,084 KB |
testcase_01 | AC | 70 ms
71,180 KB |
testcase_02 | AC | 69 ms
71,084 KB |
testcase_03 | AC | 73 ms
71,220 KB |
testcase_04 | AC | 71 ms
71,084 KB |
testcase_05 | AC | 71 ms
71,168 KB |
testcase_06 | AC | 255 ms
119,736 KB |
testcase_07 | AC | 259 ms
132,840 KB |
testcase_08 | AC | 450 ms
115,508 KB |
testcase_09 | AC | 406 ms
109,072 KB |
testcase_10 | AC | 417 ms
109,004 KB |
testcase_11 | AC | 414 ms
109,064 KB |
testcase_12 | AC | 387 ms
109,360 KB |
testcase_13 | AC | 242 ms
109,508 KB |
testcase_14 | AC | 252 ms
119,896 KB |
ソースコード
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") writef = lambda x: print("{:.12f}".format(x)) class BIT: ### BIT binary def __init__(self, n, values=None): self.bit = [0]*(n+1) self.n = n self.total = 0 if values is not None: for i,v in enumerate(values): self.add(i,v) self.total += v def check(self): l = [] prv = 0 for i in range(1,self.n+1): val = self.query(i) l.append(val-prv) prv = val print(" ".join(map(str, l))) #a1 ~ aiまでの和 O(logn) def query(self,i): res = 0 while i > 0: res += self.bit[i] # res %= M i -= i&(-i) return res def get(self,i): return self.query(i+1) - self.query(i) #ai += x(logN) def add(self,i,x): i += 1 if i==0: raise RuntimeError self.total += x while i <= self.n: self.bit[i] += x # self.bit[i] %= M i += i&(-i) def index(self, v): """a0,...,aiの和がv以上になる最小のindexを求める 存在しないとき配列サイズを返す """ if v <= 0: return 0 if self.total<v: return self.n x = 0 r = 1 while r < self.n: r = r << 1; ll = r while ll>0: if x+ll<self.n and self.bit[x+ll]<v: v -= self.bit[x+ll] x += ll ll = ll>>1 return x n = int(input()) a = list(map(int, input().split())) ns = [[] for _ in range(n)] for u,p in enumerate(a): ns[u+1].append(p) ns[p].append(u+1) bit = BIT(n) # 深さ優先探索 (巻き戻しあり) dfs tree q = [(0, -1)] ans = 0 while q: u,prv = q.pop() if u<0: # 返るときの処理 u = ~u bit.add(u, -1) else: q.append((~u,prv)) ans += bit.query(u) bit.add(u, 1) for v in ns[u]: # 進むときの処理 if v==prv: continue q.append((v,u)) print(ans)