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) cumWeight:seq[int] fp = newseq[int]() segTree:SegTree for i in 0..