結果
問題 | No.898 tri-βutree |
ユーザー | dot_haraai |
提出日時 | 2021-05-30 20:57:03 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 674 ms / 4,000 ms |
コード長 | 5,217 bytes |
コンパイル時間 | 5,031 ms |
コンパイル使用メモリ | 88,192 KB |
実行使用メモリ | 57,344 KB |
最終ジャッジ日時 | 2024-11-08 23:57:06 |
合計ジャッジ時間 | 18,399 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 384 ms
57,344 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 662 ms
36,224 KB |
testcase_08 | AC | 672 ms
38,272 KB |
testcase_09 | AC | 661 ms
41,088 KB |
testcase_10 | AC | 665 ms
41,088 KB |
testcase_11 | AC | 670 ms
37,888 KB |
testcase_12 | AC | 666 ms
39,680 KB |
testcase_13 | AC | 668 ms
38,272 KB |
testcase_14 | AC | 672 ms
36,224 KB |
testcase_15 | AC | 667 ms
36,224 KB |
testcase_16 | AC | 669 ms
36,736 KB |
testcase_17 | AC | 669 ms
36,992 KB |
testcase_18 | AC | 668 ms
37,760 KB |
testcase_19 | AC | 674 ms
39,552 KB |
testcase_20 | AC | 663 ms
38,400 KB |
testcase_21 | AC | 673 ms
35,968 KB |
コンパイルメッセージ
/home/judge/data/code/Main.nim(2, 18) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated] /home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'times' [UnusedImport] /home/judge/data/code/Main.nim(2, 26) Warning: imported and not used: 'strformat' [UnusedImport] /home/judge/data/code/Main.nim(2, 18) Warning: imported and not used: 'future' [UnusedImport] /home/judge/data/code/Main.nim(2, 37) Warning: imported and not used: 'deques' [UnusedImport] /home/judge/data/code/Main.nim(1, 41) Warning: imported and not used: 'algorithm' [UnusedImport] /home/judge/data/code/Main.nim(1, 52) Warning: imported and not used: 'tables' [UnusedImport] /home/judge/data/code/Main.nim(1, 60) Warning: imported and not used: 'sets' [UnusedImport] /home/judge/data/code/Main.nim(1, 66) Warning: imported and not used: 'lists' [UnusedImport] /home/judge/data/code/Main.nim(1, 73) Warning: imported and not used: 'intsets' [UnusedImport] /home/judge/data/code/Main.nim(2, 8) Warning: imported and not used: 'critbits' [UnusedImport]
ソースコード
import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets import critbits, future, strformat, deques template `max=`(x,y) = x = max(x,y) template `min=`(x,y) = x = min(x,y) template `mod=`(x,y) = x = x mod y template scan2 = (scan(), scan()) template scan3 = (scan(), scan()) let read* = iterator: string {.closure.} = while true: (for s in stdin.readLine.split: yield s) proc scan(): int = read().parseInt proc scanf(): float = read().parseFloat proc toInt(c:char): int = return int(c) - int('0') type SegTree = object tree :seq[int] index:seq[int] n:int proc initSegTree(baseArr:seq[int]):SegTree= var n = baseArr.len var m = 1 while m < n: m*=2 result = SegTree() result.n = m result.tree = newseqwith(2*m-1,int.high.div(4)) result.index = newseqwith(2*m-1,int.high.div(4)) for i, v in baseArr: result.tree[m-1+i]=v result.index[m-1+i]=i for idx in countdown(m-2,0): if result.tree[idx*2+1] <= result.tree[idx*2+2]: result.tree[idx] = result.tree[idx*2+1] result.index[idx] = result.index[idx*2+1] else: result.tree[idx] = result.tree[idx*2+2] result.index[idx] = result.index[idx*2+2] proc get(segTree:SegTree,ql,qr:int,k:int=0,left:int=0,right:int= -1):(int,int)= var right=right if right<0: right = segTree.n #echo ql,", ", qr, ", ",left,", ", right if ql>=right or qr<=left: return (int.high,-1) if ql <= left and right <= qr: return (segTree.tree[k],segTree.index[k]) var vl = segTree.get(ql,qr,2*k+1,left,(left+right).div(2)) vr = segTree.get(ql,qr,2*k+2,(left+right).div(2),right) return min(vl,vr) proc set(segTree:var SegTree,idx,value:int)= var k = segTree.n - 1 + idx segTree.tree[k] = value while k>=1: k = (k-1).div(2) if segTree.tree[k] == min(segTree.tree[2*k+1],segTree.tree[2*k+2]): break else: if segTree.tree[2*k+1] <= segTree.tree[2*k+2]: segTree.tree[k] = segTree.tree[2*k+1] segTree.index[k] = segTree.index[2*k+1] else: segTree.tree[k] = segTree.tree[2*k+2] segTree.index[k] = segTree.index[2*k+2] proc solve()= var n = scan() es = newseqwith(n,newseq[int]()) cs = newseqwith(n,newseq[int]()) depth = newseq[int]() inter = newseqwith(n,int.high) outer = newseqwith(n,0) weights = newseqwith(1,0) dist = newseqwith(n,0) fp = newseq[int]() segTree:SegTree for i in 0..<n-1: var (a,b,c) = (scan(),scan(),scan()) es[a].add(b) es[b].add(a) cs[a].add(c) cs[b].add(c) var q = scan() xyz = newseqwith(q,(scan(),scan(),scan())) proc LCA(a,b:int):int= var left = min(inter[a],inter[b]) right = max(inter[a],inter[b]) (dpt, index) = segTree.get(left,right+1) return index proc pathLength(a,b:int):int= var left = min(inter[a],inter[b]) right = max(inter[a],inter[b]) return weights[right]-weights[left] proc greedyLCA(a,b:int):int= var minidx=0 minv = int.high left = min(inter[a],inter[b]) right = max(inter[a],inter[b]) #echo depth[left..right] for i in left..right: if minv>depth[i]: minv = depth[i] minidx = i return minidx # proc eulTour(par:int,p:int,dpt:int)= inter[p].min=depth.len depth.add(dpt) fp.add(p) for (nxt,cost) in zip(es[p],cs[p]): if par==nxt:continue weights.add(cost) dist[nxt] = dist[p]+cost eulTour(p,nxt,dpt+1) fp.add(p) depth.add(dpt) weights.add(-cost) outer[p].max=depth.len eulTour(-1,0,0) #echo fp #echo depth #echo weights for i in 1..<weights.len: weights[i]+=weights[i-1] #echo weights #echo inter #echo outer segTree = depth.initSegTree() #echo fp[LCA(4,6)] #echo fp[LCA(4,2)] #echo fp[LCA(7,6)] #echo pathLength(0,4) #echo pathLength(0,2) #echo pathLength(0,8) #echo pathLength(0,7) for (x,y,z) in xyz: var res = 0 var res2 = 0 res = (pathLength(0,x) + pathLength(0,y) - 2*pathLength(0,fp[LCA(x,y)]) + pathLength(0,y) + pathLength(0,z) - 2*pathLength(0,fp[LCA(y,z)]) + pathLength(0,z) + pathLength(0,x) - 2*pathLength(0,fp[LCA(z,x)])).div(2) #res2 = (dist[x] + dist[y] - 2*dist[fp[LCA(x,y)]] + #dist[y] + dist[z] - 2*dist[fp[LCA(y,z)]] + #dist[z] + dist[x] - 2*dist[fp[LCA(z,x)]]).div(2) #res2 = (dist[x] + dist[y] - 2*dist[fp[greedyLCA(x,y)]] + #dist[y] + dist[z] - 2*dist[fp[greedyLCA(y,z)]] + #dist[z] + dist[x] - 2*dist[fp[greedyLCA(z,x)]]).div(2) echo res#, ", ", res==res2 #echo "=== debug ===" proc compress(s:int):string= if s > 1000000: return "inf" else: return $s #for i in 0..<n-2: #echo 2^i-1," " ,2^i-1+2^i-1 #echo segTree.tree[2^i-1..<2^i-1+2^i].mapIt(it.compress).join(",") #if 2^i-1+2^i==segTree.tree.len: #break #for i in 0..<n-2: #echo segTree.index[2^i-1..<2^i-1+2^i].mapIt(it.compress).join(",") #if 2^i-1+2^i==segTree.tree.len: #break #for a in 0..<n-1: #for b in a+1..<n: #var #lc = LCA(a,b) #glc = greedyLCA(a,b) #if lc != glc: #echo a,", ", b solve()