Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用 treap wrong 了 怎么回事 help help!!!!! (附代码)

Posted by yllw at 2007-09-02 00:15:46 on Problem 2761
用 treap wrong 了 怎么回事  help help!!!!!
Var
        i,j,n,m,st,en,root,sonnum:Longint;
        x,y,k,a,ran,lson,rson,lnum,rnum: array[0..300000] of Longint;
        ans,mark:array[1..300000] of Longint;
        function max(c,d:Longint):Longint;
        begin
                if c>d then max:=c
                else max:=d;
        end;
        function min(c,d:Longint):Longint;
        begin
                if c>d then min:=d
                else min:=c;
        end;
        procedure swap(var c,d:Longint);
        var
                t:Longint;
        begin
                t:=c;c:=d;d:=t;
        end;
        procedure fast(f,l:Longint);
        var
                f1,l1,mid:Longint;
        begin
                f1:=f;l1:=l;mid:=x[(f+l) div 2];
                repeat
                        while x[f1]<mid do
                                inc(f1);
                        while x[l1]>mid do
                                dec(l1);
                        if f1<=l1 then
                        begin
                                swap(x[f1],x[l1]);
                                swap(y[f1],y[l1]);
                                swap(k[f1],k[l1]);
                                swap(mark[f1],mark[l1]);
                                inc(f1);dec(l1);
                        end;
                until   f1>l1;
                if f1<l then fast(f1,l);
                if f<l1 then fast(f,l1);
        end;
        Function left(point:Longint):Longint;
        var
                t:Longint;
        begin
                left:=lson[point];
                t:=rson[lson[point]];lnum[point]:=rnum[lson[point]];
                rson[lson[point]]:=point;
                rnum[lson[point]]:=lnum[point]+rnum[point]+1;
                lson[point]:=t;

        end;
        Function right(point:Longint):Longint;
        var
                t:Longint;
        begin
                right:=rson[point];
                t:=lson[rson[point]];rnum[point]:=lnum[rson[point]];
                lson[rson[point]]:=point;
                lnum[rson[point]]:=rnum[point]+lnum[point]+1;
                rson[point]:=t;
        end;
        function ins(point:Longint):Longint;
        begin
                if point=0 then
                begin
                        ins:=j;exit;
                end;
                ins:=point;
                if a[j]<a[point] then
                begin
                        inc(lnum[point]);
                        lson[point]:=ins(lson[point]);
                        if ran[point]<ran[lson[point]] then
                                ins:=left(point);
                end
                else
                begin
                        inc(rnum[point]);
                        rson[point]:=ins(rson[point]);
                        if ran[point]<ran[rson[point]] then
                                ins:=right(point);
                end;
        end;
        function del(point:Longint):Longint;
        begin
                if (lson[point]=0) and (rson[point]=0)
                then begin del:=0;exit; end;
                if a[j]<a[point] then
                begin
                        lson[point]:=del(lson[point]);del:=point;dec(lnum[point]);
                end
                else
                if (a[j]>=a[point]) and (j<>point) then
                begin
                        rson[point]:=del(rson[point]);del:=point;dec(rnum[point]);
                end
                else
                begin
                        if ran[lson[point]]>ran[rson[point]]
                        then
                        begin
                                point:=left(point);dec(rnum[point]);
                                rson[point]:=del(rson[point]);del:=point;
                        end
                        else
                        begin
                                point:=right(point);dec(lnum[point]);
                                lson[point]:=del(lson[point]);del:=point;
                        end;
                end;
        end;
	procedure find(point:Longint);
	begin
                if sonnum+lnum[point]=k[i] then
                begin
                        ans[mark[i]]:=a[point];exit;
                end;
                if sonnum+lnum[point]>k[i] then
                        find(lson[point])
                else
                begin
                        sonnum:=sonnum+lnum[point]+1;
                        find(rson[point]);
                end;
	end;
Begin
       
        randomize;
        readln(n,m);
        for i:=1 to n do
                read(a[i]);
        readln;
        for i:=1 to m do
        begin
                readln(x[i],y[i],k[i]);mark[i]:=i;
        end;
        fast(1,m);
        x[0]:=x[1];y[0]:=0;root:=0;
        for i:=1 to m do
        begin
		dec(k[i]);
                en:=min(y[i-1],x[i]-1);
                for j:=x[i-1] to en do
                        root:=del(root);
                st:=max(x[i],y[i-1]+1);
                for j:=st to y[i] do
                begin
                        ran[j]:=random(1990724);
                        if root=0 then root:=j
                        else root:=ins(root);
                end;
                sonnum:=0;
                find(root);
        end;
        for i:=1 to m do
                writeln(ans[i]);
End.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator