結果

問題 No.399 動的な領主
ユーザー とりゐとりゐ
提出日時 2023-03-23 21:22:24
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 6,472 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 102,260 KB
最終ジャッジ日時 2024-09-18 15:36:47
合計ジャッジ時間 21,336 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,272 KB
testcase_01 AC 41 ms
53,836 KB
testcase_02 AC 95 ms
75,756 KB
testcase_03 AC 90 ms
76,160 KB
testcase_04 AC 246 ms
79,348 KB
testcase_05 AC 374 ms
81,612 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 AC 247 ms
79,116 KB
testcase_11 AC 351 ms
81,948 KB
testcase_12 AC 1,743 ms
100,000 KB
testcase_13 AC 1,597 ms
100,072 KB
testcase_14 AC 395 ms
102,260 KB
testcase_15 AC 609 ms
98,128 KB
testcase_16 AC 964 ms
98,208 KB
testcase_17 TLE -
testcase_18 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]


class lazy_segtree():
    def update(self,k):self.d[k]=self.op(self.d[2*k],self.d[2*k+1])
    def all_apply(self,k,f):
        self.d[k]=self.mapping(f,self.d[k])
        if (k<self.size):self.lz[k]=self.composition(f,self.lz[k])
    def push(self,k):
        self.all_apply(2*k,self.lz[k])
        self.all_apply(2*k+1,self.lz[k])
        self.lz[k]=self.identity
    def __init__(self,V,OP,E,MAPPING,COMPOSITION,ID):
        self.n=len(V)
        self.log=(self.n-1).bit_length()
        self.size=1<<self.log
        self.d=[E for i in range(2*self.size)]
        self.lz=[ID for i in range(self.size)]
        self.e=E
        self.op=OP
        self.mapping=MAPPING
        self.composition=COMPOSITION
        self.identity=ID
        for i in range(self.n):self.d[self.size+i]=V[i]
        for i in range(self.size-1,0,-1):self.update(i)
    def set(self,p,x):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        self.d[p]=x
        for i in range(1,self.log+1):self.update(p>>i)
    def get(self,p):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        return self.d[p]
    def prod(self,l,r):
        assert 0<=l and l<=r and r<=self.n
        if l==r:return self.e
        l+=self.size
        r+=self.size
        for i in range(self.log,0,-1):
            if (((l>>i)<<i)!=l):self.push(l>>i)
            if (((r>>i)<<i)!=r):self.push(r>>i)
        sml,smr=self.e,self.e
        while(l<r):
            if l&1:
                sml=self.op(sml,self.d[l])
                l+=1
            if r&1:
                r-=1
                smr=self.op(self.d[r],smr)
            l>>=1
            r>>=1
        return self.op(sml,smr)
    def all_prod(self):return self.d[1]
    def apply_point(self,p,f):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        self.d[p]=self.mapping(f,self.d[p])
        for i in range(1,self.log+1):self.update(p>>i)
    def apply(self,l,r,f):
        assert 0<=l and l<=r and r<=self.n
        if l==r:return
        l+=self.size
        r+=self.size
        for i in range(self.log,0,-1):
            if (((l>>i)<<i)!=l):self.push(l>>i)
            if (((r>>i)<<i)!=r):self.push((r-1)>>i)
        l2,r2=l,r
        while(l<r):
            if (l&1):
                self.all_apply(l,f)
                l+=1
            if (r&1):
                r-=1
                self.all_apply(r,f)
            l>>=1
            r>>=1
        l,r=l2,r2
        for i in range(1,self.log+1):
            if (((l>>i)<<i)!=l):self.update(l>>i)
            if (((r>>i)<<i)!=r):self.update((r-1)>>i)
    def max_right(self,l,g):
        assert 0<=l and l<=self.n
        assert g(self.e)
        if l==self.n:return self.n
        l+=self.size
        for i in range(self.log,0,-1):self.push(l>>i)
        sm=self.e
        while(1):
            while(l%2==0):l>>=1
            if not(g(self.op(sm,self.d[l]))):
                while(l<self.size):
                    self.push(l)
                    l=(2*l)
                    if (g(self.op(sm,self.d[l]))):
                        sm=self.op(sm,self.d[l])
                        l+=1
                return l-self.size
            sm=self.op(sm,self.d[l])
            l+=1
            if (l&-l)==l:break
        return self.n
    def min_left(self,r,g):
        assert (0<=r and r<=self.n)
        assert g(self.e)
        if r==0:return 0
        r+=self.size
        for i in range(self.log,0,-1):self.push((r-1)>>i)
        sm=self.e
        while(1):
            r-=1
            while(r>1 and (r%2)):r>>=1
            if not(g(self.op(self.d[r],sm))):
                while(r<self.size):
                    self.push(r)
                    r=(2*r+1)
                    if g(self.op(self.d[r],sm)):
                        sm=self.op(self.d[r],sm)
                        r-=1
                return r+1-self.size
            sm=self.op(self.d[r],sm)
            if (r&-r)==r:break
        return 0

mask=(1<<20)-1
def op(a,b):
  a0=a>>20
  a1=a&mask
  b0=b>>20
  b1=b&mask
  return ((a0+b0)<<20)+a1+b1
e=0
def mapping(f,x):
  x0=x>>20
  x1=x&mask
  return ((x0+f*x1)<<20)+x1
def composition(f,g):
  return f+g
id=0


class HeavyLightDecomposition:
  def __init__(self,n,edge):
    self.n=n
    self.sub_size=[1]*n
    self.par=[-1]*n
    
    todo=[0]
    while todo:
      v=todo.pop()
      if v>=0:
        for u in edge[v]:
          if u!=self.par[v]:
            todo.append(~u)
            todo.append(u)
            self.par[u]=v
      else:
        v=~v
        self.sub_size[self.par[v]]+=self.sub_size[v]
    
    self.head=[-1]*n
    self.head[0]=0
    self.order=[-1]*n
    self.heavy_child=[-1]*n
    
    todo=[0]
    cnt=0
    while todo:
      v=todo.pop()
      self.order[v]=cnt
      cnt+=1
      mx=0
      for u in edge[v]:
        if u!=self.par[v] and mx<self.sub_size[u]:
          mx=self.sub_size[u]
          self.heavy_child[v]=u
      for u in edge[v]:
        if self.par[v]!=u and self.heavy_child[v]!=u:
          self.head[u]=u
          todo.append(u)
      if self.heavy_child[v]!=-1:
        self.head[self.heavy_child[v]]=self.head[v]
        todo.append(self.heavy_child[v])
  
  def for_each_edge(self,u,v):
    paths=[]
    while True:
      if self.order[u]>self.order[v]:
        u,v=v,u
      if self.head[u]!=self.head[v]:
        paths.append((self.order[self.head[v]],self.order[v]+1))
        v=self.par[self.head[v]]
      else:
        paths.append((self.order[u]+1,self.order[v]+1))
        return paths
  
  def for_each_vertex(self,u,v):
    paths=[]
    while True:
      if self.order[u]>self.order[v]:
        u,v=v,u
      if self.head[u]!=self.head[v]:
        paths.append((self.order[self.head[v]],self.order[v]+1))
        v=self.par[self.head[v]]
      else:
        paths.append((self.order[u],self.order[v]+1))
        return paths

n=int(input())
edge=[[] for i in range(n)]
for _ in range(n-1):
  a,b=map(lambda x:int(x)-1,input().split())
  edge[a].append(b)
  edge[b].append(a)

HLD=HeavyLightDecomposition(n,edge)
seg=lazy_segtree([(1<<20)+1 for i in range(n)],op,e,mapping,composition,id)

ans=0
q=int(input())
for _ in range(q):
  x,y=map(lambda x:int(x)-1,input().split())
  for l,r in HLD.for_each_vertex(x,y):
    ans+=seg.prod(l,r)>>20
    seg.apply(l,r,1)
print(ans)
0