  program rconfexp;
  label progend;
  const cellimin=-100;cellimax=100;
        celljmin=-100;celljmax=100;
        cellspacesize=50000;
        cellx=50; celly=50;
        crand=452807053;
  type  point=record x,y:integer end;
        segment=record p1,p2:point end;
        rectangle=record x1,x2,y1,y2:integer end;
  var celltop:array[cellimin..cellimax,
                     celljmin..celljmax] of integer;
      cellspace:array[1..cellspacesize] of
                      record info,link:integer end;
      cellfreespace:integer;
      rconf:array[1..1000] of rectangle;
      rnumb:integer;
      irand:integer;
      n,m,dx,dy,sx,sy,rx,ry,ax,ay:integer;
      nr,virz,att,paz1,paz2:integer;
      p:point;

  function rand:real;
    begin
    irand:=abs(irand*crand);
    rand:=irand*0.465661e-9;
    end;

  procedure initiate_cellstr;
    var i,j:integer;
    begin
    for i:=cellimin to cellimax
     do for j:=celljmin to celljmax
      do celltop[i,j]:=0;
    for i:=1 to cellspacesize
      do cellspace[i].link:=i+1;
    cellspace[cellspacesize].link:=0;
    cellfreespace:=1
    end;

  procedure print_cellstr;
    var i,j,l:integer;
    begin
    writeln('print_cellstr:');
    for i:=cellimin to cellimax
     do for j:=celljmin to celljmax
      do begin
         l:=celltop[i,j];
         if l > 0
            then begin
                 writeln('(',i:3,j:4,'):');
                 while l > 0
                   do begin
                      write(cellspace[l].info:10);
                      l:=cellspace[l].link
                      end;
                 writeln
                 end
         end
    end;

  procedure insert(i,j,k:integer);
    var nextfreespace:integer;
    begin
    if cellfreespace = 0
       then begin writeln('insert: cellspace is full');
                  goto progend end;
    if (i < cellimin) or (cellimax < i) or
       (j < celljmin) or (celljmax < j)
       then begin
            writeln('insert: (i,j) is not a cell');
            goto progend end;
    nextfreespace:=cellspace[cellfreespace].link;
    cellspace[cellfreespace].info:=k;
    cellspace[cellfreespace].link:=celltop[i,j];
    celltop[i,j]:=cellfreespace;
    cellfreespace:=nextfreespace
    end;

  procedure delete(i,j,k:integer);
    label procend,loopend;
    var l,lp:integer;
    begin
    if (i < cellimin) or (cellimax < i) or
       (j < celljmin) or (celljmax < j)
       then begin
            writeln('delete: (i,j) is not a cell');
            goto progend end;
    l:=celltop[i,j];
    if l = 0 then goto procend;
    if cellspace[l].info = k
       then celltop[i,j]:=cellspace[l].link
       else begin
            lp:=l;
            l:=cellspace[l].link;
            while l > 0
              do if cellspace[l].info = k
                   then begin
                        cellspace[lp].link:=
                                   cellspace[l].link;
                        goto loopend
                        end
                   else begin
                        lp:=l;
                        l:=cellspace[l].link;
                        end;
            loopend:
            end;
    procend:
    end;

  function icell(x:integer):integer;
    begin
    if (x = cellx * (x div cellx)) or (x > 0)
       then icell:=x div cellx
       else icell:=(x div cellx) - 1
    end;

  function jcell(y:integer):integer;
    begin
    if (y = celly * (y div celly)) or (y > 0)
       then jcell:=y div celly
       else jcell:=(y div celly) - 1
    end;

  procedure r_insert(nr:integer);
    var i,j:integer;
    begin
    for i:=icell(rconf[nr].x1) to
           icell(rconf[nr].x2)
    do for j:=jcell(rconf[nr].y1) to
              jcell(rconf[nr].y2)
      do insert(i,j,nr)
    end;

  procedure r_delete(nr:integer);
    var i,j:integer;
    begin
    for i:=icell(rconf[nr].x1) to
           icell(rconf[nr].x2)
    do for j:=jcell(rconf[nr].y1) to
              jcell(rconf[nr].y2)
      do delete(i,j,nr)
    end;

  procedure rconfprint;
    var i,xmin,xmax,ymin,ymax:integer;
    begin
    xmin:=rconf[1].x1;
    xmax:=rconf[1].x2;
    ymin:=rconf[1].y1;
    ymax:=rconf[1].y2;
    for i:=1 to rnumb
      do begin
         if rconf[i].x1 < xmin
            then xmin:=rconf[i].x1;
         if rconf[i].x2 > xmax
            then xmax:=rconf[i].x2;
         if rconf[i].y1 < ymin
            then ymin:=rconf[i].y1;
         if rconf[i].y2 > ymax
            then ymax:=rconf[i].y2
         end;
    writeln(xmin:7,xmax:7,ymin:7,ymax:7,
            '  <---xmin,xmax,ymin,ymax');
    for i:=1 to rnumb
      do begin
         writeln(rconf[i].x1,rconf[i].y1,
                 rconf[i].x2,rconf[i].y1);
         writeln(rconf[i].x1,rconf[i].y2,
                 rconf[i].x2,rconf[i].y2);
         writeln(rconf[i].x1,rconf[i].y1,
                 rconf[i].x1,rconf[i].y2);
         writeln(rconf[i].x2,rconf[i].y1,
                 rconf[i].x2,rconf[i].y2)
         end
    end;

  procedure make_segment(var s:segment;
                                  x1,y1,x2,y2:integer);
    begin
    s.p1.x:=x1; s.p1.y:=y1;
    s.p2.x:=x2; s.p2.y:=y2
    end;

  function p_in_r(p:point;r:rectangle):boolean;
    begin
    p_in_r:=(r.x1 < p.x) and (p.x < r.x2) and
            (r.y1 < p.y) and (p.y < r.y2)
    end;

  function s_r_intersec(s:segment;r:rectangle):boolean;
    begin
    s_r_intersec:=p_in_r(s.p1,r) or p_in_r(s.p2,r) or
             ((s.p1.y <= r.y1) and (r.y2 <= s.p2.y) and
              (r.x1 < s.p1.x) and (s.p2.x < r.x2)) or
             ((s.p1.x <= r.x1) and (r.x2 <= s.p2.x) and
              (r.y1 < s.p1.y) and (s.p2.y < r.y2))
    end;

  function eq_rectangle(r1,r2:rectangle):boolean;
    begin
    eq_rectangle:=(r1.x1 = r2.x1) and (r1.x2 = r2.x2)
              and (r1.y1 = r2.y1) and (r1.y2 = r2.y2)
    end;

  function r_r_intersec(r1,r2:rectangle):boolean;
    var s11,s12,s13,s14,s21,s22,s23,s24:segment;
    begin
    make_segment(s11,r1.x1,r1.y1,r1.x2,r1.y1);
    make_segment(s12,r1.x1,r1.y2,r1.x2,r1.y2);
    make_segment(s13,r1.x1,r1.y1,r1.x1,r1.y2);
    make_segment(s14,r1.x2,r1.y1,r1.x2,r1.y2);
    make_segment(s21,r2.x1,r2.y1,r2.x2,r2.y1);
    make_segment(s22,r2.x1,r2.y2,r2.x2,r2.y2);
    make_segment(s23,r2.x1,r2.y1,r2.x1,r2.y2);
    make_segment(s24,r2.x2,r2.y1,r2.x2,r2.y2);
    r_r_intersec:=eq_rectangle(r1,r2)  or
                  s_r_intersec(s11,r2) or
                  s_r_intersec(s12,r2) or
                  s_r_intersec(s13,r2) or
                  s_r_intersec(s14,r2) or
                  s_r_intersec(s21,r1) or
                  s_r_intersec(s22,r1) or
                  s_r_intersec(s23,r1) or
                  s_r_intersec(s24,r1)
    end;

  procedure regular_rconf(n,m,dx,dy,sx,sy:integer);
    var k,i,j:integer;
    begin
    k:=0;
    for j:=1 to m
     do for i:=1 to n
      do begin
         k:=k+1;
         rconf[k].x1:=(i-1)*sx;
         rconf[k].x2:=(i-1)*sx+dx;
         rconf[k].y1:=(j-1)*sy;
         rconf[k].y2:=(j-1)*sy+dy;
         end;
    rnumb:=k
    end;

  procedure r_shift(nr,virz,att:integer);
    label procend,loopend;
    var imi,ima,jmi,jma,frontlength,frontdepth:integer;
        front:rectangle;
        i,j,kd,kl,l,n,jvirz,jatt:integer; flag:boolean;
    begin
    if att <= 0 then goto procend;
    if virz = 1 then
       begin
       front.x1:=rconf[nr].x2;
       front.x2:=rconf[nr].x2+att;
       front.y1:=rconf[nr].y1;
       front.y2:=rconf[nr].y2;
       imi:=icell(front.x1);
       ima:=icell(front.x2);
       jmi:=jcell(front.y1);
       jma:=jcell(front.y2);
       frontlength:=jma-jmi+1;
       frontdepth :=ima-imi+1
       end;
    if virz = 2 then
       begin
       front.x1:=rconf[nr].x1;
       front.x2:=rconf[nr].x2;
       front.y1:=rconf[nr].y2;
       front.y2:=rconf[nr].y2+att;
       imi:=icell(front.x1);
       ima:=icell(front.x2);
       jmi:=jcell(front.y1);
       jma:=jcell(front.y2);
       frontlength:=ima-imi+1;
       frontdepth :=jma-jmi+1
       end;
    if virz = 3 then
       begin
       front.x1:=rconf[nr].x1-att;
       front.x2:=rconf[nr].x1;
       front.y1:=rconf[nr].y1;
       front.y2:=rconf[nr].y2;
       imi:=icell(front.x1);
       ima:=icell(front.x2);
       jmi:=jcell(front.y1);
       jma:=jcell(front.y2);
       frontlength:=jma-jmi+1;
       frontdepth :=ima-imi+1
       end;
    if virz = 4 then
       begin
       front.x1:=rconf[nr].x1;
       front.x2:=rconf[nr].x2;
       front.y1:=rconf[nr].y1-att;
       front.y2:=rconf[nr].y1;
       imi:=icell(front.x1);
       ima:=icell(front.x2);
       jmi:=jcell(front.y1);
       jma:=jcell(front.y2);
       frontlength:=ima-imi+1;
       frontdepth :=jma-jmi+1
       end;
    for kd:=1 to frontdepth
     do for kl:=1 to frontlength
      do begin
         if virz = 1 then
            begin i:=imi+kd-1;
                  j:=jmi+kl-1
            end;
         if virz = 2 then
            begin i:=imi+kl-1;
                  j:=jmi+kd-1
            end;
         if virz = 3 then
            begin i:=ima-kd+1;
                  j:=jmi+kl-1
            end;
         if virz = 4 then
            begin i:=imi+kl-1;
                  j:=jma-kd+1
            end;
         repeat
           begin
           flag:=true;
           l:=celltop[i,j];
           if l > 0 then
              while l > 0
                do begin
                   n:=cellspace[l].info;
                   if r_r_intersec(rconf[n],front)
                      then
                        begin
                        flag:=false;
                        if virz = 1 then
                           begin
                           jvirz:=1;
                           jatt:=front.x2-rconf[n].x1
                           end;
                        if virz = 2 then
                           begin
                           jvirz:=2;
                           jatt:=front.y2-rconf[n].y1
                           end;
                        if virz = 3 then
                           begin
                           jvirz:=3;
                           jatt:=rconf[n].x2-front.x1
                           end;
                        if virz = 4 then
                           begin
                           jvirz:=4;
                           jatt:=rconf[n].y2-front.y1
                           end;
                           r_shift(n,jvirz,jatt);
                           goto loopend
                           end;
                   l:=cellspace[l].link
                   end; loopend:
           end
         until flag;
         end;
    r_delete(nr);
    if virz = 1 then
       begin
       rconf[nr].x1:=rconf[nr].x1+att;
       rconf[nr].x2:=rconf[nr].x2+att
       end;
    if virz = 2 then
       begin
       rconf[nr].y1:=rconf[nr].y1+att;
       rconf[nr].y2:=rconf[nr].y2+att
       end;
    if virz = 3 then
       begin
       rconf[nr].x1:=rconf[nr].x1-att;
       rconf[nr].x2:=rconf[nr].x2-att
       end;
    if virz = 4 then
       begin
       rconf[nr].y1:=rconf[nr].y1-att;
       rconf[nr].y2:=rconf[nr].y2-att
       end;
    r_insert(nr);
    procend:
    end;

  procedure r_place(dx,dy:integer; p:point);
    begin
    rnumb:=rnumb+1;
    rconf[rnumb].x1:=p.x;
    rconf[rnumb].x2:=p.x;
    rconf[rnumb].y1:=p.y;
    rconf[rnumb].y2:=p.y;
    r_insert(rnumb);
    r_shift(rnumb,1,dx div 2);
    r_shift(rnumb,3,dx);
    r_delete(rnumb);
    rconf[rnumb].x2:=rconf[rnumb].x1+dx;
    r_insert(rnumb);
    r_shift(rnumb,2,dy div 2);
    r_shift(rnumb,4,dy);
    r_delete(rnumb);
    rconf[rnumb].y2:=rconf[rnumb].y1+dy;
    r_insert(rnumb)
    end;

  procedure rand_rconf(n,rx,ry,ax,ay:integer);
    var i,dx,dy:integer; p:point;
    begin
    initiate_cellstr;
    rnumb:=0;
    for i:=1 to n
      do begin
         p.x:=trunc(ax*rand)+1;
         p.y:=trunc(ay*rand)+1;
         dx:=trunc(rx*rand)+1;
         dy:=trunc(ry*rand)+1;
         r_place(dx,dy,p)
         end
    end;

  begin
  read(paz1,paz2);
  if paz1 = 1 then begin
  read(n,m,dx,dy,sx,sy);
  regular_rconf(n,m,dx,dy,sx,sy);
  rnumb:=rnumb+1;
  read(rconf[rnumb].x1,rconf[rnumb].x2,
       rconf[rnumb].y1,rconf[rnumb].y2);
  initiate_cellstr;
  for nr:=1 to rnumb
    do begin
       r_insert(nr);
       end;
  read(virz,att);
  r_shift(rnumb,virz,att)
                   end;
  if paz1 = 2 then begin
  read(n,m,dx,dy,sx,sy);
  regular_rconf(n,m,dx,dy,sx,sy);
  initiate_cellstr;
  for nr:=1 to rnumb
    do begin
       r_insert(nr);
       end;
  read(dx,dy,p.x,p.y);
  r_place(dx,dy,p)
                   end;
  if paz1 = 3 then begin
  irand:=crand;
  read(n,rx,ry,ax,ay);
  rand_rconf(n,rx,ry,ax,ay)
                   end;
  if paz2 > 0 then rconfprint;
  progend:
  end.

