結果
問題 | No.1030 だんしんぐぱーりない |
ユーザー | convexineq |
提出日時 | 2020-04-17 22:52:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 720 ms / 2,000 ms |
コード長 | 4,979 bytes |
コンパイル時間 | 256 ms |
コンパイル使用メモリ | 82,336 KB |
実行使用メモリ | 109,148 KB |
最終ジャッジ日時 | 2024-10-03 14:44:07 |
合計ジャッジ時間 | 20,834 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
55,256 KB |
testcase_01 | AC | 39 ms
55,404 KB |
testcase_02 | AC | 38 ms
53,784 KB |
testcase_03 | AC | 37 ms
53,632 KB |
testcase_04 | AC | 39 ms
54,924 KB |
testcase_05 | AC | 524 ms
100,956 KB |
testcase_06 | AC | 508 ms
96,528 KB |
testcase_07 | AC | 417 ms
87,192 KB |
testcase_08 | AC | 407 ms
91,944 KB |
testcase_09 | AC | 503 ms
102,904 KB |
testcase_10 | AC | 352 ms
83,184 KB |
testcase_11 | AC | 527 ms
93,952 KB |
testcase_12 | AC | 488 ms
97,628 KB |
testcase_13 | AC | 472 ms
98,144 KB |
testcase_14 | AC | 556 ms
96,360 KB |
testcase_15 | AC | 398 ms
84,084 KB |
testcase_16 | AC | 486 ms
92,536 KB |
testcase_17 | AC | 487 ms
102,004 KB |
testcase_18 | AC | 609 ms
101,188 KB |
testcase_19 | AC | 437 ms
85,940 KB |
testcase_20 | AC | 466 ms
89,884 KB |
testcase_21 | AC | 478 ms
93,240 KB |
testcase_22 | AC | 448 ms
90,988 KB |
testcase_23 | AC | 527 ms
91,036 KB |
testcase_24 | AC | 471 ms
87,036 KB |
testcase_25 | AC | 498 ms
93,280 KB |
testcase_26 | AC | 395 ms
81,928 KB |
testcase_27 | AC | 438 ms
82,160 KB |
testcase_28 | AC | 533 ms
93,872 KB |
testcase_29 | AC | 429 ms
98,000 KB |
testcase_30 | AC | 412 ms
90,736 KB |
testcase_31 | AC | 429 ms
90,756 KB |
testcase_32 | AC | 537 ms
96,268 KB |
testcase_33 | AC | 526 ms
99,964 KB |
testcase_34 | AC | 356 ms
84,728 KB |
testcase_35 | AC | 681 ms
109,148 KB |
testcase_36 | AC | 705 ms
104,508 KB |
testcase_37 | AC | 720 ms
104,752 KB |
testcase_38 | AC | 696 ms
104,800 KB |
testcase_39 | AC | 701 ms
109,048 KB |
testcase_40 | AC | 39 ms
52,736 KB |
testcase_41 | AC | 38 ms
52,736 KB |
ソースコード
# coding: utf-8 # Your code here! def Euler_tour_vertex(g,root): n = len(g) parent = [-1]*n ls = [0]*n rs = [0]*n cnt = 0 q = [root] while q: v = q.pop() if v >= 0: #行きがけ ls[v] = cnt; cnt += 1 q.append(-v-1) for c in g[v]: if c != parent[v]: parent[c] = v q.append(c) else: #帰りがけ rs[-v-1] = cnt return ls,rs class segment_tree: def __init__(self, N, operator_M, e_M): self.op_M = operator_M self.e_M = e_M self.N0 = 1<<(N-1).bit_length() self.dat = [self.e_M]*(2*self.N0) # 長さNの配列 initial で初期化 def build(self, initial): self.dat[self.N0:self.N0+len(initial)] = initial[:] for k in range(self.N0-1,0,-1): self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1]) # a_k の値を x に更新 def update(self,k,x): k += self.N0 self.dat[k] = x k //= 2 while k: self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1]) k //= 2 # 区間[L,R]をopでまとめる def query(self,L,R): L += self.N0; R += self.N0 + 1 sl = sr = self.e_M while L < R: if R & 1: R -= 1 sr = self.op_M(self.dat[R],sr) if L & 1: sl = self.op_M(sl,self.dat[L]) L += 1 L >>= 1; R >>= 1 return self.op_M(sl,sr) def get(self, k): #k番目の値を取得。query[k,k]と同じ return self.dat[k+self.N0] class segment_tree_dual: def __init__(self, N, compose, funcval, ID_M=None): self.compose = compose self.ID_M = ID_M self.funcval = funcval self.height = (N-1).bit_length() #木の段数 self.N0 = 1<<self.height #木の横幅 >= N self.laz = [self.ID_M]*(2*self.N0) #作用素の木 self.val = None #値の配列 #初期値の配列を作る def build(self,initial): self.val = initial[:] #laz[k] を子に伝える、k が一番下の場合は laz[k] を val に反映する def propagate(self,k): if self.laz[k] == self.ID_M: return; if self.N0 <= k: self.val[k-self.N0] = self.funcval(self.val[k-self.N0], self.laz[k]) self.laz[k] = self.ID_M else: self.laz[(k<<1) ] = self.compose(self.laz[(k<<1) ],self.laz[k]); self.laz[(k<<1)+1] = self.compose(self.laz[(k<<1)+1],self.laz[k]); self.laz[k] = self.ID_M; # 遅延をすべて解消する def propagate_all(self): upto = self.N0 + len(self.val) for i in range(1,upto): self.propagate(i) # laz[k]およびその上に位置する作用素をすべて伝播 def thrust(self,k): for i in range(self.height,-1,-1): self.propagate(k>>i) # 区間[l,r]に関数 f を作用 def update(self, L,R,f): L += self.N0; R += self.N0+1 #登りながら関数 f を合成 while L < R: if R & 1: R -= 1 self.laz[R] = self.compose(self.laz[R],f) if L & 1: self.laz[L] = self.compose(self.laz[L],f) L += 1 L >>= 1; R >>= 1 # values[k] を取得。 def point_get(self, k): res = self.val[k] k += self.N0 while k: if self.laz[k] != self.ID_M: res = self.funcval(res, self.laz[k]) k //= 2 return res # values[k] = x 代入する def point_set(self, k): self.thrust(k+self.N0) self.val[k] = x import sys readline = sys.stdin.readline read = sys.stdin.read n,k,q = [int(i) for i in readline().split()] c = [int(i) for i in readline().split()] a = [int(i) for i in readline().split()] g = [[] for _ in range(n)] for i in range(n-1): e,f = [int(i)-1 for i in readline().split()] #g[e].append(f) g[f].append(e) ls,rs = Euler_tour_vertex(g,0) for i in range(n): rs[i] -= 1 compose = max funcval = max ID_M = 0 fun = segment_tree_dual(2*n, compose, funcval, ID_M) fun.build([0]*2*n) for lsi,rsi,ci in zip(ls,rs,c): fun.update(2*lsi,2*rsi,ci) fun.propagate_all() INF = 10**9 posl = segment_tree(k,min,INF) posr = segment_tree(k,max,0) res = segment_tree(2*n,min,INF) res.build(fun.val) for i,ai in enumerate(a): posl.update(i,2*ls[ai-1]) posr.update(i,2*rs[ai-1]) #print(g) #print(ls,rs) #print(res.dat) for _ in range(q): t,x,y = [int(i) for i in readline().split()] x -= 1 y -= 1 if t==1: #print("move",x,y,ls[y],rs[y]) posl.update(x,2*ls[y]) posr.update(x,2*rs[y]) else: #print(posl.dat,posr.dat) L = posl.query(x,y) R = posr.query(x,y) #print(L,R,x,y) print(res.query(L,R))