Cod sursa(job #165166)

Utilizator th3whu2Breta Ionut th3whu2 Data 25 martie 2008 17:01:39
Problema Marbles Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.86 kb
var f,g:text;
mx,max,sv,t,t2,k,n,m,i,j,pi,pj,o,poz:longint;
b,c:array[1..100000] of qword;
col:array[0..64] of longint;
procedure QuickSort(Lo, Hi: Integer);
procedure Sort(l, r: Integer);
var
  i,j,x,y:integer;
begin
  i:=l;j:=r;x:=b[(l+r) DIV 2];
  repeat
    while b[i]<x do i:=i+1;
    while x<b[j] do j:=j-1;
    if i<=j then
    begin
      y:=b[i];b[i]:=b[j];b[j]:=y;
      y:=c[i];c[i]:=c[j];c[j]:=y;
      i:=i+1;j:=j-1;
    end;
  until i>j;
  if l<j then Sort(l,j);
  if i<r then Sort(i,r);
end;
begin
 sort(Lo,Hi);
end;
function cbinara0(i,st,dr:longint):longint;
begin
  poz:=(st+dr) div 2;
  if b[poz]=i then cbinara0:=poz
  else if st<dr then if i<b[poz] then cbinara0:=cbinara0(i,st,poz-1)
  else cbinara0:=cbinara0(i,poz+1,dr);
end;
function cbinara1(i,st,dr:longint):longint;
begin
 poz:=(st+dr) div 2;
 if (i<=b[poz]) and ((i>b[poz-1]) or (poz=1)) then cbinara1:=poz
 else if i<b[poz] then cbinara1:=cbinara1(i,st,poz-1)
                  else cbinara1:=cbinara1(i,poz+1,dr);
end;
function cbinara2(i,st,dr:longint):longint;
begin
 poz:=(st+dr) div 2;
 if (i>b[poz]) and ((i<=b[poz+1]) or (poz=n)) then cbinara2:=poz
 else if i<b[poz] then cbinara2:=cbinara2(i,st,poz-1)
                  else cbinara2:=cbinara2(i,poz+1,dr);
end;
begin
assign(f,'marbles.in');reset(f);
assign(g,'marbles.out');rewrite(g);
read(f,n,m);
for i:=1 to n do
 read(f,b[i],c[i]);
quicksort(1,n);
for i:=1 to m do
 begin
  read(f,o,pi,pj);
  if o=0 then
   begin
    sv:=cbinara0(pi,1,n);
    b[sv]:=pi+pj;
   end
  else if o=1 then
   begin
    t:=cbinara1(pi,1,n);
    t2:=cbinara2(pj,1,n);
    for k:=0 to 64 do
     col[k]:=0;
    for k:=t to t2 do
     inc(col[c[k]]);
    max:=0;
    for k:=0 to 64 do
     if col[k]>=max then begin mx:=k; max:=col[k]; end;
    writeln(g,max);
   end;
 end;
close(f);
close(g);
end.