Cod sursa(job #12556)

Utilizator fogabFodor Gabor fogab Data 4 februarie 2007 12:34:43
Problema Cutii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.22 kb
var f,f2:text;
    n,t,i,j,max,x,y,l,sz,m2:integer;
    big:longint;
    a:array[0..3500,1..3] of integer;
    b:array[1..23000000] of integer;
    c:array[0..12] of integer;

function find(k:longint;x,y:integer;sz:integer):integer;
var m:longint;
begin
if sz=-1 then find:=b[k]
        else begin
             if (x<=c[sz]) then if (y<=c[sz]) then m:=find(4*k-2,x,y,sz-1)
                                              else m:=find(4*k,x,y-c[sz],sz-1)
                           else if (y<=c[sz]) then m:=find(4*k-1,x-c[sz],y,sz-1)
                                              else m:=find(4*k+1,x-c[sz],y-c[sz],sz-1);
             if m>b[k] then find:=m
                       else find:=b[k];
             end;

end;

procedure go(k:longint;x,y,val:integer;sz:integer);
begin
if (x=1) and (y=1) then b[k]:=val
                   else begin
                        if (x>c[sz]) then if (y>c[sz]) then go(4*k+1,x-c[sz],y-c[sz],val,sz-1)
                                                       else begin
                                                            go(4*k-1,x-c[sz],y,val,sz-1);
                                                            go(4*k+1,x-c[sz],1,val,sz-1);
                                                            end
                                     else if (y>c[sz]) then begin
                                                            go(4*k,x,y-c[sz],val,sz-1);
                                                            go(4*k+1,1,y-c[sz],val,sz-1);
                                                            end
                                                       else begin
                                                            go(4*k-2,x,y,val,sz-1);
                                                            go(4*k,x,1,val,sz-1);
                                                            go(4*k-1,1,y,val,sz-1);
                                                            go(4*k+1,1,1,val,sz-1);
                                                            end;
                        end;
end;
procedure quicksort(l,r:dword);
var i,j,x:dword;
begin
 i:=l;j:=r;x:=a[(l+r) div 2,1];
 repeat
   while a[i,1]<x do i:=i+1;
   while x<a[j,1] do j:=j-1;
   if i<=j then begin
                a[0]:=a[i];
                a[i]:=a[j];
                a[j]:=a[0];
                i:=i+1;
                j:=j-1;
                end;
 until i>j;
 if l<j then quicksort(l,j);
 if i<r then quicksort(i,r);
end;

begin
c[0]:=1;
for i:=1 to 12 do c[i]:=c[i-1]*2;
assign(f2,'cutii.out');
rewrite(f2);
assign(f,'cutii.in');
reset(f);
readln(f,n,t);
sz:=0;
while c[sz+1]<=n do inc(sz);
for l:=1 to t do begin
                 max:=0;
                 for big:=1 to 23000000 do b[big]:=0;
                 for j:=1 to n do readln(f,a[j,1],a[j,2],a[j,3]);
                 quicksort(1,n);
                 for i:=1 to n do begin
                                  m2:=find(1,a[i,2],a[i,3],sz);
                                  if m2+1>max then max:=m2+1;
                                  go(1,a[i,2]+1,a[i,3]+1,m2+1,sz);
                                  end;
                 writeln(f2,max);
                 writeln(l);
                 end;
close(f);
close(f2);
end.