Cod sursa(job #393287)

Utilizator mimarcelMoldovan Marcel mimarcel Data 9 februarie 2010 10:09:06
Problema Hashuri Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 1.97 kb
// tu Fana metoda asta nu e buna:
const maxn=1000000;
type vector=array[0..maxn]of longint;
var n,m,i:longint;
    v:vector;
    op:byte;
    x,a,b:longint;

function cautare(var v:vector;p,q:longint;var x:longint):byte;
var r,s:longint;
begin
cautare:=1;
while p<=q do
  begin
  r:=(p+q)div 2;
  s:=v[r];
  if x=s then exit
         else if x<s then q:=r-1
                     else p:=r+1;
  end;
cautare:=0;
end;

function poz(var v:vector;p,q:longint;var x:longint):longint;
var r,s:longint;
begin
while p<=q do
  begin
  r:=(p+q)div 2;
  s:=v[r];
  if x=s then begin
              poz:=r;
              exit
              end
         else if x<s then q:=r-1
                     else p:=r+1;
  end;
poz:=0;
end;

function poz2(var v:vector;p,q:longint;var x:longint):longint;
var r,s:longint;
begin
poz2:=0;
if x>v[q] then poz2:=q+1
          else
if x<v[p]then poz2:=p
         else
while p<=q do
  begin
  r:=(p+q)div 2;
  s:=v[r];
  if(s>x)and((x>v[r-1])or(r=0))then begin
                                      poz2:=r;
                                      exit;
                                      end
         else if x<s then q:=r-1
                     else p:=r+1;
  end;
end;

begin
assign(input,'hashuri.in');
reset(input);
assign(output,'hashuri.out');
rewrite(output);
readln(n);
m:=0;
for i:=1 to n do
  begin
  readln(op,x);
  case op of
    1:begin
      if m=0 then begin
                  m:=1;
                  v[1]:=x
                  end
             else
        begin
        a:=poz2(v,1,m,x);
        if a<>0 then
          begin
          inc(m);
          for b:=m downto a+1 do v[b]:=v[b-1];
          v[a]:=x;
          end;
        end;
      end;
    2:begin
      a:=poz(v,1,m,x);
      if v[a]=x then
        begin
        dec(m);
        for b:=a to m do v[b]:=v[b+1];
        end;
      end;
    3:writeln(cautare(v,1,m,x));
    end;
  end;
close(output);
close(input);
end.