結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー ゼットゼット
提出日時 2024-02-24 05:00:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 6,293 ms / 7,000 ms
コード長 3,461 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 438,844 KB
最終ジャッジ日時 2024-02-24 05:02:10
合計ジャッジ時間 94,896 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,460 KB
testcase_01 AC 45 ms
53,460 KB
testcase_02 AC 44 ms
53,460 KB
testcase_03 AC 3,163 ms
156,524 KB
testcase_04 AC 2,960 ms
154,992 KB
testcase_05 AC 1,755 ms
125,544 KB
testcase_06 AC 561 ms
89,516 KB
testcase_07 AC 1,409 ms
119,524 KB
testcase_08 AC 2,008 ms
130,768 KB
testcase_09 AC 3,240 ms
166,196 KB
testcase_10 AC 583 ms
89,244 KB
testcase_11 AC 2,257 ms
136,724 KB
testcase_12 AC 535 ms
87,040 KB
testcase_13 AC 2,115 ms
132,272 KB
testcase_14 AC 4,312 ms
200,548 KB
testcase_15 AC 4,028 ms
192,976 KB
testcase_16 AC 1,895 ms
125,592 KB
testcase_17 AC 3,350 ms
167,960 KB
testcase_18 AC 1,309 ms
111,640 KB
testcase_19 AC 3,976 ms
192,324 KB
testcase_20 AC 2,057 ms
132,340 KB
testcase_21 AC 2,445 ms
134,872 KB
testcase_22 AC 2,386 ms
136,796 KB
testcase_23 AC 97 ms
75,528 KB
testcase_24 AC 106 ms
75,628 KB
testcase_25 AC 112 ms
76,264 KB
testcase_26 AC 105 ms
75,660 KB
testcase_27 AC 134 ms
76,008 KB
testcase_28 AC 4,390 ms
203,064 KB
testcase_29 AC 4,479 ms
203,328 KB
testcase_30 AC 4,365 ms
203,132 KB
testcase_31 AC 4,338 ms
202,916 KB
testcase_32 AC 4,680 ms
203,124 KB
testcase_33 AC 6,293 ms
438,844 KB
testcase_34 AC 1,535 ms
216,076 KB
testcase_35 AC 1,353 ms
140,416 KB
testcase_36 AC 2,040 ms
340,684 KB
testcase_37 AC 2,974 ms
146,944 KB
testcase_38 AC 3,831 ms
385,452 KB
testcase_39 AC 41 ms
53,460 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=[10**10]*(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])
      l=0
      r=N
      b=count//2+1
      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)
      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