ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1116. Piecewise Constant Function

help!!why I got wang answer!!!!!!!
Posted by qwt 31 Mar 2002 15:07
const
  maxn=15000;
var
  a,b,c:array[0..maxn,1..3] of integer;
  aa,bb,na,nb,nc,i,j,k,p,q:integer;
procedure sortb(x,y:integer);
var
  i,j,k:integer;
begin
  if x>=y then exit;
  i:=x;j:=y;
  k:=b[i,1];
  b[0]:=b[i];
  while i<j do begin
    while (i<j)and(b[j,1]>k) do dec(j);
    if i<j then begin b[i]:=b[j];inc(i);end;
    while (i<j)and(b[i,1]<k) do inc(i);
    if i<j then begin b[j]:=b[i];dec(j);end;
  end;
  b[i]:=b[0];
  sortb(x,i-1);sortb(i+1,y);
end;

begin

  fillchar(b,sizeof(b),0);c:=b;
  read(na);for i:=1 to na do for j:=1 to 3 do read(b[i,j]);
  sortb(1,na);a:=b;
  fillchar(b,sizeof(b),0);
  read(nb);for i:=1 to nb do for j:=1 to 3 do read(b[i,j]);
  sortb(1,nb);
  i:=1;
  j:=1;
  nc:=0;
  while (i<=na)and(j<=nb) do begin
    while (j<=nb)and(b[j,2]<=a[i,1]) do inc(j);
    if j>nb then break;
    while (i<=na)and(a[i,2]<=b[j,1]) do begin
      inc(nc);
      c[nc]:=a[i];
      inc(i);
    end;
    if i>na then break;
    if b[j,1]<=a[i,1] then begin
      if b[j,2]<a[i,2] then begin a[i,1]:=b[j,2];inc(j);end else inc
(i);
    end else begin
      if b[j,2]<a[i,2] then begin
        inc(nc);c[nc,1]:=a[i,1];c[nc,2]:=b[j,1];c[nc,3]:=a[i,3];
        a[i,1]:=b[j,2];inc(j);
      end else begin
        inc(nc);c[nc,1]:=a[i,1];c[nc,2]:=b[j,1];c[nc,3]:=a[i,3];
        inc(i);
      end;
    end;
  end;
  for j:=i to na do begin
    inc(nc);c[nc]:=a[j];
  end;
  write(nc);for i:=1 to nc do for j:=1 to 3 do write(' ',c[i,j]);
end.