結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー ゼットゼット
提出日時 2024-02-23 23:42:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,139 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 442,788 KB
最終ジャッジ日時 2024-02-23 23:44:11
合計ジャッジ時間 67,066 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,332 KB
testcase_01 AC 39 ms
53,332 KB
testcase_02 AC 39 ms
53,332 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 1,356 ms
216,088 KB
testcase_35 AC 1,198 ms
140,144 KB
testcase_36 AC 1,915 ms
340,588 KB
testcase_37 AC 1,936 ms
143,856 KB
testcase_38 AC 2,996 ms
384,420 KB
testcase_39 AC 39 ms
53,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segtreemin:
  def __init__(self,n):
    self.size=1
    while self.size<n:
      self.size*=2
    self.dat=[10**10]*(self.size*2)
  def update(self,x,a):
    x+=self.size
    self.dat[x]=a
    while x>1:
      x//=2
      self.dat[x]=min(self.dat[2*x],self.dat[2*x+1])
  def querry(self,u,v):
    u+=self.size
    v+=self.size
    score=10**10
    while u<v:
      if u&1:
        score=min(score,self.dat[u])
        u+=1
      if v&1:
        v-=1
        score=min(score,self.dat[v])
      u//=2
      v//=2
    return score
class segtreemax:
  def __init__(self,n):
    self.size=1
    while self.size<n:
      self.size*=2
    self.dat=[0]*(self.size*2)
  def update(self,x,a):
    x+=self.size
    self.dat[x]=a
    while x>1:
      x//=2
      self.dat[x]=max(self.dat[2*x],self.dat[2*x+1])
  def querry(self,u,v):
    u+=self.size
    v+=self.size
    score=0
    while u<v:
      if u&1:
        score=max(score,self.dat[u])
        u+=1
      if v&1:
        v-=1
        score=max(score,self.dat[v])
      u//=2
      v//=2
    return score
class segtree:
  def __init__(self,n):
    self.size=1
    while self.size<n:
      self.size*=2
    self.dat=[0]*(self.size*2)
  def update(self,x,a):
    x+=self.size
    self.dat[x]+=a
    while x>1:
      x//=2
      self.dat[x]=(self.dat[2*x]+self.dat[2*x+1])
  def querry(self,u,v):
    u+=self.size
    v+=self.size
    score=0
    while u<v:
      if u&1:
        score+=self.dat[u]
        u+=1
      if v&1:
        v-=1
        score+=self.dat[v]
      u//=2
      v//=2
    return score
N=int(input())
Zmax=segtreemax(N+1)
Zmin=segtreemin(N+1)
Zsum=segtree(N+1)
Zcount=segtree(N+1)
import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
A=list(map(int,input().split()))
result=[0]*(N+1)
G=[[] for i in range(N+1)]
for i in range(N):
  a,b=map(int,input().split())
  G[a].append(b)
  G[b].append(a)
T={}
R={}
B=A[:]
B=set(B)
B=list(B)
B.sort()
for i in range(len(B)):
  R[B[i]]=i
used=[False]*(N+1)
dist=[-1]*(N+1)
dist[0]=0
def dfs(x):
  used[x]=True
  Zmax.update(R[A[x]],A[x])
  Zmin.update(R[A[x]],A[x])
  Zcount.update(R[A[x]],1)
  Zsum.update(R[A[x]],A[x])
  if not A[x] in T:
    T[A[x]]=1
  else:
    T[A[x]]+=1
  if dist[x]<=1:
    result[x]=-1
  else:
    k1=Zmax.querry(0,N+1)
    k2=Zmin.querry(0,N+1)
    if k1==k2:
      result[x]=1
    else:
      count=Zcount.querry(0,N+1)
      b=(count+1)//2
      l=0
      r=N
      while True:
        m=(l+r)//2
        c=Zcount.querry(0,m+1)
        if c>=b:
          r=m
        else:
          l=m+1
        if l==r:
          break
      c1=Zcount.querry(0,l)
      d1=Zsum.querry(0,l)
      c2=Zcount.querry(l+1,N+1)
      d2=Zsum.querry(l+1,N+1)
      result1=(c1*B[l]-d1)+((d2-k1)-(c2-1)*B[l])
      result2=((c1-1)*B[l]-(d1-k2))+(d2-c2*B[l])
      result[x]=min(result1,result2)
  for y in G[x]:
    if used[y]==True:
      continue
    dist[y]=dist[x]+1
    dfs(y)
  Zcount.update(R[A[x]],-1)
  Zsum.update(R[A[x]],-A[x])
  T[A[x]]-=1
  if T[A[x]]==0:
    Zmax.update(R[A[x]],0)
    Zmin.update(R[A[x]],10**10)
dfs(0)
for i in range(1,N+1):
  print(result[i])
0