Cod sursa(job #163680)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 22 martie 2008 14:54:53
Problema Marbles Scor 90
Compilator fpc Status done
Runda preONI 2008, Runda Finala, Clasele 5-8 Marime 2.34 kb
var v:array[-2..100010,0..64]of longint;
    b,poz,a,c:array[-2..100010]of longint;
    n,i,j,k,m,x,y,z,l,p1,max,p2:longint;
    f1,f2:text;
procedure merge(p,r:longint);
var q,e,f,g:longint;
begin
   q:=(p+r)div 2;
   if p<q then merge(p,q);
   if q+1<r then merge(q+1,r);
   for i:=p to r do
   begin
   a[i]:=b[i];
   c[i]:=poz[i];
   end;
   e:=p;
   f:=p;
   g:=q+1;
   while(f<=q)and(g<=r)do
   if a[f]<a[g] then begin b[e]:=a[f];
                           poz[e]:=c[f];
                           e:=e+1;
                           f:=f+1;
                     end
                else begin b[e]:=a[g];
                           poz[e]:=c[g];
                           e:=e+1;
                           g:=g+1;
                     end;
   for i:=f to q do
   begin
   b[e]:=a[i];
   poz[e]:=c[i];
   e:=e+1;
   end;
   for i:=g to r do
   begin
   b[e]:=a[i];
   poz[e]:=c[i];
   e:=e+1;
   end;
end;
begin
   b[0]:=-200000010;
   b[-1]:=-200000010;
   assign(f1,'marbles.in');
   reset(f1);
   assign(f2,'marbles.out');
   rewrite(f2);
   read(f1,n,m);
   b[n+1]:=2000000010;
   b[n+2]:=2000000010;
   for i:=1 to n do
   read(f1,b[i],poz[i]);
   merge(1,n);
   for i:=1 to n do
   begin
   v[i]:=v[i-1];
   v[i,poz[i]]:=v[i,poz[i]]+1;
   end;
   for i:=1 to m do
   begin
   read(f1,x,y,z);
   if x=0 then b[i]:=b[i]+z
          else begin k:=0;
                     l:=n+1;
                     while l-k>4 do
                     begin
                     x:=(l+k)div 2;
                     if b[x]>y then l:=x
                               else k:=x;
                     end;
                     for j:=k-1 to l do
                     if (b[j]<y)and(b[j+1]>=y)then p1:=j;
                     k:=0;
                     l:=n+1;
                     while l-k>4 do
                     begin
                     x:=(l+k)div 2;
                     if b[x]>z then l:=x
                               else k:=x;
                     end;
                     for j:=k-1 to l do
                     if (b[j]<=z)and(b[j+1]>z)then p2:=j;
                     max:=v[p2,1]-v[p1,1];
                     for j:=2 to 64 do
                     if v[p2,j]-v[p1,j]>max then max:=v[p2,j]-v[p1,j];
                     writeln(f2,max);
               end;
   end;
   close(f1);
   close(f2);
end.