From b5fe8a03b2f565d98e51536dcb48640c5f65c7a3 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Sun, 1 May 2016 15:37:09 +0800 Subject: [PATCH] From https://vijos.org/p/1081/solution --- QUSTOJ/1034_williamking5.pas | 99 ++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 QUSTOJ/1034_williamking5.pas diff --git a/QUSTOJ/1034_williamking5.pas b/QUSTOJ/1034_williamking5.pas new file mode 100644 index 0000000..2f00ca9 --- /dev/null +++ b/QUSTOJ/1034_williamking5.pas @@ -0,0 +1,99 @@ +var + x,y,z,len,i,j,k,m,n:longint; + sum,tree:array[0..20,0..100000] of longint; + he,left,right,l,r:array[1..300000] of longint; + a:array[1..100000,0..1] of longint; + v:array[1..100000] of 0..1; +procedure sort(h,t:longint); +var + i,j,x,y,k:longint; +begin + x:=a[(h+t)div 2,0]; + y:=a[(h+t)div 2,1]; + i:=h;j:=t; + repeat + while (a[i,0]x) or ((a[j,0]=x)and(a[j,1]>y)) do + dec(j); + if i<=j then begin + k:=a[i,0];a[i,0]:=a[j,0];a[j,0]:=k; + k:=a[i,1];a[i,1]:=a[j,1];a[j,1]:=k; + inc(i); + dec(j); + end; + until i>j; + if ih then sort(h,j); +end; +procedure build(root,ll,rr,h:longint); +begin + l[root]:=ll; + r[root]:=rr; + he[root]:=h; + if ll<>rr then begin + inc(len); + left[root]:=len; + build(len,ll,(ll+rr)div 2,h+1); + inc(len); + right[root]:=len; + build(len,(ll+rr)div 2+1,rr,h+1); + end; +end; +procedure divide_h(root:longint); +var + i,j,s:longint; +begin + if l[root]=r[root] then exit; + for i:=l[root] to r[root] do v[i]:=0; + for i:=l[root] to r[root] do + begin + a[i,0]:=tree[he[root],i]; + a[i,1]:=i; + end; + sort(l[root],r[root]); + for i:=l[root] to (l[root]+r[root])div 2 do + v[a[i,1]]:=1; + s:=0;j:=0; + for i:=l[root] to r[root] do + if v[i]=1 then begin + tree[he[root]+1,l[root]+s]:=tree[he[root],i]; + inc(s);sum[he[root],i]:=s; + end + else begin + tree[he[root]+1,(l[root]+r[root])div 2+1+j]:=tree[he[root],i]; + inc(j);sum[he[root],i]:=s; + end; + if left[root]<>0 then divide_h(left[root]); + if right[root]<>0 then divide_h(right[root]); +end; +function kthnum(root,ll,rr,k:longint):longint; +var + i,j,x:longint; +begin + if l[root]=r[root] then exit(tree[he[root],l[root]]); + if ll-1