{
ID:xpycc1
PROG:rect1
LANG:PASCAL
}
program rect1;
const
maxcolor=2500;
maxq=100000;
type
squr=record
x1,x2,y1,y2,color:integer;
end;
var
res:array[1..maxcolor]of longint;
q:array[0..maxq]of squr;
n,rx2,ry2,wx1,wx2,wy1,wy2,c,kx1,ky1,kx2,ky2:integer;
s,e,ep,i:longint;
f:boolean;
//
function min(x,y:integer):integer;
begin
if x>y then min:=y
else min:=x;
end;
//
function max(x,y:integer):integer;
begin
if x>y then max:=x
else max:=y;
end;
//
begin
s:=0; e:=1;
assign(input,'rect1.in'); reset(input);
readln(rx2,ry2,n);
with q[s] do
begin
x1:=0;
x2:=rx2;
y1:=0;
y2:=ry2;
color:=1;
end;
while n>0 do
begin
readln(kx1,ky1,kx2,ky2,c);
i:=s; ep:=e; //ep是这次应该扫的矩形多1的位置,因为和新生成的矩形是不用比的
while i<ep do
if(q[i].x2<kx1)or(q[i].x1>kx2)or
(q[i].y2<ky1)or(q[i].y1>ky2) //矩形相离
then inc(i)
else begin
f:=false;
wx1:=max(kx1,q[i].x1);
wy1:=max(ky1,q[i].y1);
wx2:=min(kx2,q[i].x2);
wy2:=min(ky2,q[i].y2);
if wy1>q[i].y1 then
begin
q[e].x1:=q[i].x1;
q[e].y1:=q[i].y1;
q[e].x2:=wx2;
q[e].y2:=wy1;
q[e].color:=q[i].color;
inc(e); f:=true;
end;
if wx1>q[i].x1 then
begin
q[e].x1:=q[i].x1;
q[e].y1:=wy1;
q[e].x2:=wx1;
q[e].y2:=q[i].y2;
q[e].color:=q[i].color;
inc(e); f:=true;
end;
if wy2<q[i].y2 then
begin
q[e].x1:=wx1;
q[e].y1:=wy2;
q[e].x2:=q[i].x2;
q[e].y2:=q[i].y2;
q[e].color:=q[i].color;
inc(e); f:=true;
end;
if wx2<q[i].x2 then
begin
q[e].x1:=wx2;
q[e].y1:=q[i].y1;
q[e].x2:=q[i].x2;
q[e].y2:=wy2;
q[e].color:=q[i].color;
inc(e); f:=true;
end;
if not f then //矩形出队就是把后面的矩形放上来
begin
dec(e); dec(ep);
q[i]:=q[ep]; q[ep]:=q[e];
dec(i); //新矩形覆盖原矩形的情况
end else begin
dec(e); q[i]:=q[e]; //没有覆盖的情况
end;
inc(i);
end;
q[e].x1:=kx1;
q[e].x2:=kx2;
q[e].y1:=ky1;
q[e].y2:=ky2;
q[e].color:=c;
inc(e); dec(n);
end;
close(input);
for i:=s to e-1 do
with q[i] do
inc(res[color],(x2-x1)*(y2-y1));
assign(output,'rect1.out'); rewrite(output);
for i:=1 to maxcolor do
if res[i]>0 then
writeln(i,' ',res[i]);
close(output);
end.