Pagini recente » Cod sursa (job #348363) | Cod sursa (job #2328186) | Cod sursa (job #2812118) | Cod sursa (job #1002515) | Cod sursa (job #408622)
Cod sursa(job #408622)
{FP}
{$M 64000000,0}
{$MODE OBJFPC}
{$IFDEF ANHDQ}
{$INLINE OFF}
{$H-,I+,Q+,R+,S+}
{$ELSE}
{$INLINE ON}
{$H+,I-,Q-,R-,S-}
{$ENDIF}
// Result:
program heapuri_AnhDQ;
const
FI_NAME = 'heapuri.in';
FO_NAME = 'heapuri.out';
MaxN = 200000;
oo = 2000000000;
type
HeapMin = object
nItems : LongInt;
Items, Pos: array[1..MaxN] of LongInt;
procedure Init (); inline;
function IsEmpty(): Boolean; inline;
procedure Up (node: LongInt); inline;
procedure Down (node: LongInt); inline;
procedure Push (x: LongInt); inline;
function Pop (): LongInt;
procedure Delete (x: LongInt); inline;
end;
var
Fi, Fo : Text;
n, i, t, x, cnt: LongInt;
H : HeapMin;
V : array[1..MaxN] of LongInt;
(*------------------------------*)
procedure HeapMin.Init(); inline;
begin
nItems := 0;
FillChar(Pos, SizeOf(Pos), 0);
end;
(*------------------------------*)
function HeapMin.IsEmpty(): Boolean; inline;
begin
exit(nItems = 0);
end;
(*------------------------------*)
procedure HeapMin.Up(node: LongInt); inline;
var x, root: LongInt;
begin
x := Items[node];
repeat
root := node shr 1;
if (root = 0) or (V[Items[root]] <= V[x]) then break;
Items[node] := Items[root];
Pos[Items[node]] := node;
node := root;
until false;
Items[node] := x;
Pos[x] := node;
end;
(*------------------------------*)
procedure HeapMin.Down(node: LongInt); inline;
var x, child: LongInt;
begin
x := Items[node];
repeat
child := node shl 1;
if (child < nItems) and (V[Items[child]] > V[Items[child + 1]]) then Inc(child);
if (child > nItems) or (V[x] <= V[Items[child]]) then break;
Items[node] := Items[child];
Pos[Items[node]] := node;
node := child;
until false;
Items[node] := x;
Pos[x] := node;
end;
(*------------------------------*)
procedure HeapMin.Push(x: LongInt); inline;
begin
Inc(nItems);
Items[nItems] := x;
Up(nItems);
end;
(*------------------------------*)
function HeapMin.Pop(): LongInt; inline;
begin
result := Items[1];
Pos[result] := 0;
Items[1] := Items[nItems];
Dec(nItems);
if nItems > 0 then Down(1);
end;
(*------------------------------*)
procedure HeapMin.Delete(x: LongInt); inline;
begin
if Pos[x] = 0 then exit;
V[x] := -oo;
Up(Pos[x]);
Pop();
end;
(*------------------------------*)
begin
Assign(Fi, FI_NAME); Reset(Fi);
Assign(Fo, FO_NAME); Rewrite(Fo);
read(Fi, n);
for i := 1 to n do
begin
read(Fi, t);
case t of
1: begin
Inc(cnt);
read(Fi, V[cnt]);
H.Push(cnt);
end;
2: begin
read(Fi, x);
H.Delete(x);
end;
3: WriteLn(Fo, V[H.Items[1]]);
end;
end;
Close(Fi); Close(Fo);
end.