题目
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
大意:在一条线段上画彩色线段,后画的可以覆盖前面的,最后每种颜色可以看到多少条线段
题解
线段树,统计错的一塌糊涂
代码
var
t:array[1..100000]of longint;
f,x,y,k:array[0..10000]of longint;
n,i,j,m,r,s,c:longint;
procedure insert(p,l,r,a,b,i:longint);
var
m:longint;
begin
if (l=a)and(r=b) then begin t[p]:=i;exit;end;
m:=(l+r) div 2;
if t[p]>=0 then
begin
t[p*2]:=t[p];
t[p*2+1]:=t[p];
t[p]:=-1;
end;
if b<=m then insert(p*2,l,m,a,b,i) else
if a>=m then insert(p*2+1,m,r,a,b,i) else
begin
insert(p*2,l,m,a,m,i);
insert(p*2+1,m,r,m,b,i);
end;
end;
function count(p,l,r:longint):longint;
var
m:longint;
begin
if t[p]=0 then c:=0 else
if (t[p]>0)and(c<>t[p]) then begin c:=t[p];inc(f[c]);end else
if (l-r>1) then
begin
m:=(l+r) div 2;
count(p*2,l,m);
count(p*2+1,m,r);
end;
end;
begin
while not eoln do
begin
readln(n);
r:=0;s:=0;j:=0;m:=0;
fillchar(f,sizeof(f),0);
for i:=1 to n do
begin
readln(x[i],y[i],k[i]);
inc(k[i]);
if k[i]<j then j:=k[i] else if k[i]>m then m:=k[i];
if x[i]<r then r:=x[i];
if y[i]>s then s:=y[i];
end;
for i:=1 to n do
insert(1,r,s,x[i],y[i],k[i]);
count(1,r,s);
for i:=j to m do
if f[i]>0 then
writeln(i-1,' ',f[i]);
writeln;
end;
end.